Skip to content

uPesy ships directly only in France !

uPesy products available on Amazon for EU

Contents Menu Expand Light mode Dark mode Auto light/dark mode

The capacitive sensors of the ESP32

(Updated at 11/28/2022)

The ESP32 has capacitive sensors that can be used as a touch button. These are the famous “TOUCH” pins found on the pinouts. Ten are available on the uPesy ESP32 boards.

complete ESP32 pin diagram

Pinout of the uPesy ESP32 Wroom Devkit board

What is it?

Capacitive sensors are widely used to detect the pressure of our fingers, especially on touch screens. We can use them on the ESP32 to replace push buttons.

Operation of the ESP32 capacitive sensor

Capacitive touch sensors

Capacitive sensors are based on the variation of the capacitance (of a capacitor) when the sensor is touched. The ADC (Analog to Digital Converter) reads and converts this variation.

Note

It is essential to remember that these capacitive sensors will not be as reliable as push buttons, especially for the use we will make of them.

Indeed, many parasitic capacitances come into play and more or less disturb the measured values.

Parasitic capacitances of the ESP32 capacitive sensor

Parasitic capacitors

It is necessary to consider all these parameters and make a much more rigorous and complex electrical circuit to have good measures. We can resume the circuit to a copper wire to show how it works!

ESP32 wiring diagram of the capacitive sensor

It’s hard to make a simpler circuit 🙂

Use on the ESP32

The code to use the capacitive sensors is straightforward. Only one function is needed touchRead() . The code to read the capacitive measurement of pin 4 is :

touchRead(4);
//or
touchRead(T0);

Note

The function parameter is either the pin number (here GPIO4) or the number of the capacitive sensor associated with the pin (here T0).

The usage is very similar to the analogRead() .

With a short code, the measured values are displayed in the serial monitor:

void setup() {
    Serial.begin(115200);
    delay(1000); // Delay to launch the serial monitor
    Serial.println("ESP32 Touch Demo");
}

void loop() {
    Serial.println(touchRead(4));
    delay(500);
}
Graph of the ESP32 capacitive sensor response

Waveform displayed in the serial plotter

The hollows correspond to the moment when the wire is touched. A threshold value must be defined to determine if the cable has been touched. If we are below this threshold value, we have touched the button.

Note

The threshold value depends on the material used (wire, length, breadboard) and should be adjusted.

The code with the threshold is :

int capacitiveValue = 100;
int threshold = 20; //Threshold to adjust

void setup() {
    Serial.begin(115200);
    delay(1000); // Delay to launch the serial monitor
    Serial.println("ESP32 Touch Demo");
}

void loop() {
    capacitiveValue = touchRead(4);
    if(capacitiveValue < threshold ){
        Serial.println("Wire touched");
        }
    delay(500);
}
Wiring on esp32 capacitive sensor prototyping plate

That’s it for the programming! You can also add aluminum foil at the end of the wire for better sensitivity.