ESP32 Capacitive Touch Sensor Pins with Arduino IDE

This article shows 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.

ESP32 Touch Pins with Arduino IDE

To read the value of the ESP32 touch pins, use the touchRead(GPIO) function, that accepts as argument, the GPIO you want to read.

Watch the Video Tutorial

You can watch the video tutorial or keep reading this page for the written instructions.

Introducing 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.

ESP32 Touch Sensitive Pins GPIOs

Learn more about the ESP32 GPIOs: ESP32 Pinout Reference.

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

Note: 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.

Note: at the time of writing this tutorial, there is an issue with touch pin assignment in Arduino IDE. GPIO 33 is swapped with GPIO 32 in the assignment. This means that if you want to refer to GPIO 32 you should use T8 in the code. If you want to refer to GPIO33 you should use T9. If you don’t have this issue, please ignore this note.


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.


Code – Reading the Touch Sensor

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.

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

void setup() {
  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

View raw code

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.


In the loop() is where you read the sensor.


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.

Testing the sketch example

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 Touch Pins with Arduino IDE Demonstration

You can also use the serial plotter to better see the values. Close the serial monitor, go to Tools > SerialPlotter.

ESP32 Touch Pins with Arduino IDE Demonstration Serial Plotter

Touch Sensitive LED

You can use this feature to control outputs. In this example, we’ll build a simple touch controlled LED circuit. When you touch the GPIO with your finger, the LED lights up.

For this example, you need the following parts:

You can use the preceding links or go directly to to find all the parts for your projects at the best price!

Finding the threshold value

Grab a piece of aluminium foil, cut a small square, and wrap it around the wire as shown in the following figure.

ESP32 Touch Pins with Arduino IDE threshold

With the previous code running, go back to the serial monitor.

Now, touch the aluminium foil, and you’ll see the values changing again.

ESP32 Touch Pins with Arduino IDE Demonstration with aluminium foil

In our case, when we’re not touching the pin, the normal value is above 70. And when we touch the aluminum foil it drops to some value below 10.

So, we can set a threshold value, and when the reading goes below that value, an LED lights up. A good threshold value in this case is 20, for example.


Add an LED to your circuit by following the next schematic diagram. In this case, we’re connecting the LED to GPIO 16.

Touch Sensitive LED with ESP32 Touch Pins Schematic Diagram


Copy the following code to your Arduino IDE.

// set pin numbers
const int touchPin = 4; 
const int ledPin = 16;

// change with your threshold value
const int threshold = 20;
// variable for storing the touch pin value 
int touchValue;

void setup(){
  delay(1000); // give me time to bring up serial monitor
  // initialize the LED pin as an output:
  pinMode (ledPin, OUTPUT);

void loop(){
  // read the state of the pushbutton value:
  touchValue = touchRead(touchPin);
  // check if the touchValue is below the threshold
  // if it is, set ledPin to HIGH
  if(touchValue < threshold){
    // turn LED on
    digitalWrite(ledPin, HIGH);
    Serial.println(" - LED on");
    // turn LED off
    digitalWrite(ledPin, LOW);
    Serial.println(" - LED off");

View raw code

This code reads the touch value from the pin we’ve defined, and lights up an LED when the value is below the threshold. This means that when you place your finger in the aluminium pad, the LED lights up.

Testing the Project

Upload the sketch to your ESP32. Now, test your circuit. Touch the aluminum foil and see the LED lighting up.

Touch Sensitive LED with ESP32 Touch Pins Demonstration

Wrapping Up

In this tutorial you’ve learned how to use the ESP32 touch pins. In summary:

  • The ESP32 has 10 capacitive touch GPIOs.
  • When you touch a touch-sensitive GPIO, the value read by the sensor drops.
  • You can set a threshold value to make something happen when it detects touch.
  • The ESP32 touch pins can be used to wake up the ESP32 from deep sleep.

We hope you’ve found this tutorial interesting. If you want to learn more about the ESP32, enroll in our course: Learn ESP32 with Arduino IDE.

You might also want to take a look at our free ESP32 projects and tutorials.

Thanks for reading.

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

16 thoughts on “ESP32 Capacitive Touch Sensor Pins with Arduino IDE”

  1. Hola! fantástico trabajo.
    Estoy siguiendo tus artículos y estoy aprendiendo mucho, cosa que te agradezco.
    Tengo un problema que no acierto a descifrar, cuando intento subir un sketch a mi placa (ESP32 DEVKIT V1 de, siempre de da el mismo error:
    El Sketch usa 245148 bytes (18%) del espacio de almacenamiento de programa. El máximo es 1310720 bytes.
    Las variables Globales usan 13588 bytes (4%) de la memoria dinámica, dejando 314092 bytes para las variables locales. El máximo es 327680 bytes. v2.6
    Serial port COM6
    Connecting…….._____….._____….._____….._____….._____….._____…..____Ha ocurrido un error mientras se enviaba el sketch

    A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header”
    No encuentro el motivo. Estoy usando WIN10 32Bits y el IDE Arduino.
    Pensando que se tratara de una tarjeta diferente, he probado distintas tarjetas, con los mismos resultados.
    Como puedo saber realmente que placa tengo. Hay alguna forma de saberlo.
    Agradecería que alguien me indicara como salvar este problema.
    Gracias por todo.

  2. hi everyone does anyone knows the exactlly pin to interface sim800l with esp32-nodemcuS? i have tried many pins even defined or tried the example code provided by TINYGSM but it doesn’t work
    does anyone can help me?

  3. Are the touch GPIO’s protected against ESD – static electricity – internally ? If a metal foil is connected to the pin will the ESP32 be damaged if the person touching it carries a high static charge ? Is it better to cover the metal with a thin insulator to avoid a direct conductive path to the device ?

    • All the GPIO’s have an internal pullup resistor. I am not sure if this is functional if not called in code. I have never experienced any bad ESD feedback on my units. I immerse mine in water tanks and have done everything possible to short them out. Just use the wires as they are and experiment. Lots of possibilities with this feature.!!

  4. Hi I’m making a kids robot with an oled panel and some led lights .I’m using a touch pin to start a random sequence of led palettes and it works fine. Now comes the part i dont get, when i send a message to the oled panel whether from mqtt or internally from the esp32 the leds start up by themselves after the text has rolled on for a couple of seconds. There is no programmed connection between the two so my conclusion so far is that the touch pin is somehow sensitive to the text being displayed. i have set it to go off at value below 20 , i tried using 10 but it wouldn’t sense at that level. Any ideas whats going on?

    • Hi.
      I’m not sure what’s the issue. But, it can be sensitive to the surroundings.
      However, I would suggest that you try different GPIOs to connect the LEDs and try to choose another touch pin.

  5. Hello! I was just wondering if it’s actually possible to use this to measure capacitance. Much like a multimeter would with its two probes.

  6. Hi, I am using 8 touch inputs on a ESP32 WROOM with GPIO0 accessable. Just for testing I am printing all inputs to the serial monitor.
    While all inputs in ‘high state’ (not touched), I found that just one (GPIO0) stays at a value of 1. Should I do something with initialising ?


Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.