ESP32 Capacitive Touch

 

// ESP32_CapacitiveTouch_1.ino

/*Great feature of the ESP32 is that it has the ability to detect
touch on various pins by having capacitive touch sensors,
in fact the ESP32 has 10 of these

If you look at the pin mapping you will see the following
– these are the pins that support touch

static const uint8_t T0 = 4;
static const uint8_t T1 = 0;
static const uint8_t T2 = 2;
static const uint8_t T3 = 15;
static const uint8_t T4 = 13;
static const uint8_t T5 = 12;
static const uint8_t T6 = 14;
static const uint8_t T7 = 27;
static const uint8_t T8 = 33;
static const uint8_t T9 = 32;

Its easy to read the touch sensors by using the function:
touchRead(Touch Pin #);

We will create an example where when we touch a pin an led will light,
you may need to adjust the value called touch_value

These could be used for touch buttons */

#define TOUCH_PIN T0 //connected to pin 4
#define LED_PIN 2
byte touch_value;

void setup()
{
Serial.begin(115200);
Serial.println(“ESP32 Touch Test”);

pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}

void loop(){
touch_value = touchRead(TOUCH_PIN);
Serial.println(touch_value); // get value using T0

if (touch_value < 50) {
digitalWrite (LED_PIN, HIGH);
}
else {
digitalWrite (LED_PIN, LOW);
}

delay(1000);
}

// ########## END ESP32_CapacitiveTouch_1.ino ##########

 

// ESP32_CapacitiveTouch_2.ino

/*Great feature of the ESP32 is that it has the ability to detect
touch on various pins by having capacitive touch sensors,
in fact the ESP32 has 10 of these

If you look at the pin mapping you will see the following
– these are the pins that support touch

static const uint8_t T0 = 4;
static const uint8_t T1 = 0;
static const uint8_t T2 = 2;
static const uint8_t T3 = 15;
static const uint8_t T4 = 13;
static const uint8_t T5 = 12;
static const uint8_t T6 = 14;
static const uint8_t T7 = 27;
static const uint8_t T8 = 33;
static const uint8_t T9 = 32;

Its easy to read the touch sensors by using the function:
touchRead(Touch Pin #);

We will create an example where when we touch a pin an led will light,
you may need to adjust the value called touch_value

These could be used for touch buttons */

#define TOUCH_PIN0 T0 //connected to pin 4
#define TOUCH_PIN3 T3 //connected to pin 15

#define LED_PIN0 2
#define LED_PIN3 16

byte touch_value0;
byte touch_value3;

void setup()
{
Serial.begin(115200);
Serial.println(“ESP32 Touch Test”);

pinMode(LED_PIN0, OUTPUT);
digitalWrite(LED_PIN0, LOW);

pinMode(LED_PIN3, OUTPUT);
digitalWrite(LED_PIN3, HIGH); //LED Builtin
}

void loop(){
touch_value0 = touchRead(TOUCH_PIN0);
touch_value3 = touchRead(TOUCH_PIN3);

Serial.print(“T0 = “);
Serial.println(touch_value0); // get value using T0

Serial.print(“T3 = “);
Serial.println(touch_value3); // get value using T3
Serial.println(” “);

if (touch_value0 < 50) {
digitalWrite(LED_PIN0, HIGH);
}
else {
digitalWrite(LED_PIN0, LOW);
}

if (touch_value3 < 50) {
digitalWrite(LED_PIN3, LOW);
}
else {
digitalWrite(LED_PIN3, HIGH);
}

delay(1000);
}

//########## END ESP32_CapacitiveTouch_2.ino ######