ESP32 capacitive touch example

Another 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

Code


#define TOUCH_PIN T0 //connected to 4
#define LED_PIN A13 //connected to 15
int touch_value = 100;

void setup()
{
Serial.begin(9600);
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);
}

 

Demonstration

 

SHARE
Previous articlefade an LED using an ESP32
Next articleESP32 and MAX44009 ambient light sensor example

1 COMMENT