ESP32 - Capacitive Touch Sensor Pins

We are going to learn how to use the ESP32 touch pins with Arduino IDE. The ESP32 touch pins can sense variations in anything that holds an electrical charge. They are often used to wake up the ESP32 from deep sleep.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Jumper Wires

About the ESP32 Touch Sensor

The ESP32 has 10 capacitive touch GPIOs. These GPIOs can sense variations in anything that holds an electrical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger.

These pins can be easily integrated into capacitive pads, and replace mechanical buttons. Additionally, the touch pins can also be used as a wake up source when the ESP32 is in deep sleep.

Take a look at your board pinout to locate the 10 different touch sensors – the touch sensitive pins are highlighted in pink color.

You can see that touch sensor 0 corresponds to GPIO 4, touch sensor 2 to GPIO 2, and so on.

※ NOTE THAT:

Touch sensor 1 is GPIO 0. However, it’s not available as a pin in this particular ESP32 development board (version with 30 GPIOs). GPIO 0 is available on the version with 36 pins.

ESP32 Code

touchRead()

Reading the touch sensor is straightforward. In the Arduino IDE, you use the touchRead() function, that accepts as argument, the GPIO you want to read.

We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding

Let’s see how that function works by using an example from the library. In the Arduino IDE, go to File > Examples > ESP32 > Touch and open the TouchRead sketch.

Copy

// ESP32 Touch Test
// Just test touch pin - Touch0 is T0 which is on GPIO 4.

void setup() {
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}

void loop() {
  Serial.println(touchRead(4));  // get value of Touch 0 pin = GPIO 4
  delay(1000);
}              

Code Explanation

  • This example reads the touch pin 0 and displays the results in the Serial Monitor.
  • The T0 pin (touch pin 0), corresponds to GPIO 4, as we’ve seen previously in the pinout.
  • In this code, in the setup(), you start by initializing the Serial Monitor to display the sensor readings.
  • Serial.begin(115200);
  • In the loop() is where you read the sensor.
  • Serial.println(touchRead(4));
  • Use the touchRead() function, and pass as an argument the pin you want to read. In this case, the example uses T0, which is the touch sensor 0, in GPIO 4. You can either pass the touch sensor number (T0) or the GPIO number (4).
  • Now, upload the code to your ESP32 board. Make sure you have the right board and COM port selected.
  • Connect a jumper wire to GPIO 4. You will touch the metal part of this wire so that it senses the touch.
  • In the Arduino IDE window, go to Tools and open the Serial Monitor at a baud rate of 115200. You’ll see the new values being displayed every second.
  • Touch the wire connected to GPIO 4 and you’ll see the values decreasing.
ESP32 Button LED Wiring Diagram

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!