ESP32 Capacitive Touch Sensor - How to use them as a button (original) (raw)

In this tutorial, You will learn to use capacitive touch sensor pins of ESP32 development board using the Arduino IDE framework. Additionally, we will also see how to use these capacitive touch sensors as a push button to read external events. It will eliminate the need to connect an external push button. We can use these to create touch-based light switches, etc.

ESP32 chip provides ten capacitive touch sensors. These touch sensor pins are shared with other GPIO pins of ESP32. ESP32 Pin mapping for these touch pins with GPIO pins is shown below for 36 pin ESP32 dev kit version.

This pin mapping for 30 pins ESP32 Devkit version is shown below. But you can check the pinout image of other types of ESP32 development boards. Devkit supports 9 touch pins out of a total of 10 touch pins available on the ESP32 WROOM chip. Touch1 is not available on this version of ESP32. Because Touch1 is shared with GPIO0 and it is not available on the ESP32 Devkit board.

ESP32 touch sensors pinout with Devkit DOIT

How Capacitive Touch Sensors Work?

Capacitive sensors work by detecting electrical charge value variation on GPIO pins. For example, if you touch any of these pins, the GPIO pin will produce output according to the electrical charge on your finger. Because the human body also holds some electrical charge. Therefore, these touch sensors are also known as capacitive sensors. This electrical capacitance variation produces an analog signal. Two internal successive approximation ADCs (SAR ADCs) of ESP32 sample this analog signal and outputs an equivalent digital value.

ULP processor of ESP32 manages touch detection. Hence, these pins can also be used to wake up ESP32 from deep sleep with touch pins as a wake-up source.

Now let’s see how to program touch sensor pins and read electrical changes on these pins. But before starting this tutorial, the following are the prerequisites for this tutorial:

Circuit Diagram

Now make this circuit diagram on the breadboard. Connect a cable to GPIO4 of ESP32 board and also connect any conductive material with another terminal of the wire such as aluminum foil, conductive cloth, conductive paint, etc. Now it will act as a touch sensor.

Touch sensor of ESP32

In the above circuit diagram touch0 is used which is mapped with GPIO4. Now let’s see how to write code to measure the touch sensor value and send this value on the serial monitor of Arduino IDE.

ESP32 Touch Sensor Example Code

To write code for the touch sensor, we use the touchRead() function. It is very easy to use the touchRead() function.

touchRead(touch_sensor_pin_number)

This function reads the value of the touch sensor value associated with the touch pin. You only need to pass the name of the touch pin to this function. For example, if you want to use touch pin zero, you will simply use this function like this touchRead(T0).

Now, let’s move to working part of this function. Below is a simple code for this tutorial.

int touch_sensor_value=0;
void setup()
{
Serial.begin(115200);
Serial.println("Touch sensor tutorial ESP32");
}

void loop()
{

touch_sensor_value = touchRead(T0);
Serial.print("Touch0 value is = ");
Serial.println( touch_sensor_value);
delay(1000);
}

In this code, the variable touch_sensor_value is used to store the output of touchRead function.

int touch_sensor_value=0;

In the setup function, uart communication is initialized with a baud rate of 115200 and after that Serial.println() function is used to transmit string “Touch sensor tutorial ESP32” to serial monitor of Arduino IDE.

Serial.begin(115200);
Serial.println("Touch sensor tutorial ESP32");

Now comes the main part of this tutorial loop, in the loop part, touchRead(T0) function will read the value of the touch sensor on GPIO4 and will save its value in variable touch_sensor_value. So now the reading part has done. After that serial library function first sends a string “Touch0 value is = ” to serial monitor and after that, the value of the touch sensor will be sent. This process will repeat itself after every one second.

Without touching the pin with a finger, you will see higher values of touch sensor output on a serial monitor. After you touch the wire connected with GPIO four, you will notice that the values will start decreasing.

Now you need to upload this code to the ESP32 board and you will see the output like this:

How to use the touch sensor as a button in ESP32

Now, we will see an example of using ESP32 touch pins as a digital button. Instead of using an external button, you can use any touch pin as a digital input pin for button. But to do so, you need to find the threshold of the touch pin. By threshold, we mean what is the value without touching the wire and what is the highest value of the touch sensor after touching the wire. Note down this value and we will use it in our code to use touch pins as a digital button. You can find the threshold by simply uploading the same code given above. Just measure these values on the serial monitor of Arduino IDE and note down the threshold value.

For example in our case threshold is between 20-30.

Following is a circuit diagram used to control LED with a touch pin as a button. Wire this circuit with the required components:

ESP32 touch sensor as button with Arduino IDE

Program for this tutorial is given below.

#define touch_pin_numer T0
const int LED_PIN_NUMBER = 22;
const int VALUE_THRESHOLD = 30;
int TOUCH_SENSOR_VALUE;
void setup()
{
delay(1000); 
pinMode (LED_PIN_NUMBER, OUTPUT);
}
void loop(){
TOUCH_SENSOR_VALUE = touchRead(touch_pin_numer);
Serial.print(TOUCH_SENSOR_VALUE);
if(TOUCH_SENSOR_VALUE < VALUE_THRESHOLD)
{
digitalWrite(LED_PIN_NUMBER, HIGH);
}
else{
digitalWrite(LED_PIN_NUMBER, LOW);
}
}

The code for this example is the same as we used in the first example. except for the threshold variable and LED blinking part. I have already talked about LED blinking in previous tutorials.

You may also like to read: