What is ESP32 Touch Sensor? how to use?

The ESP32 is equipped with 10 capacitive touch GPIOs. These touch GPIOs can detect changes in electrical charge, such as those caused by touching the GPIOs with a finger. This functionality allows these pins to be integrated into capacitive pads, serving as a replacement for mechanical buttons.

Before you get started with this, you also might want to read the previous tutorial on ESP32.

- ESP WROOM 32: LED Blink Tutorial

- ESP32 Tutorial on Controlling an LED with a Pushbutton 

How the ESP32 Touch Sensor Works

The ESP32 touch sensors are capacitive sensors that detect changes in capacitance caused by the proximity or touch of a finger. Each touch sensor is connected to a specific GPIO pin on the ESP32. When a finger approaches or touches the sensor, it changes the electrical field around the sensor, altering its capacitance. This change is detected and measured by the ESP32.

esp32 touch sensor
 The touchRead(GPIO) function in the Arduino IDE is used to read the value from a touch-sensitive GPIO pin. The value read by this function decreases when the sensor is touched, indicating a change in capacitance. By monitoring these values, you can determine when a touch event occurs and use this information to trigger actions in your ESP32 projects, such as turning on an LED or sending a signal.

The 10 touch-sensitive pins are highlighted in pink on the following board's pinout diagram. 

ESP32 Touch Sensor pins

Here is the mapping of touch sensors to GPIO pins:
- Touch sensor 0: GPIO 4
- Touch sensor 2: GPIO 2
- and so on.

Note that touch sensor 1 corresponds to GPIO 0, which is not available on the ESP32 development board version with 30 GPIOs but is present on the 36-pin version.

Also, there is a known issue with touch pin assignment in the Arduino IDE: GPIO 33 is swapped with GPIO 32. Therefore, if you want to refer to GPIO 32, use T8 in your code, and for GPIO 33, use T9. Ignore this note if your board does not have this issue.

Using the touchRead() Function

To read the touch sensor values, use the touchRead() function in the Arduino IDE. This function takes the GPIO number you want to read as an argument:

touchRead(GPIO)

Code Example

Reading the Touch Sensor & Controlling an LED with a Touch Sensor

In this ESP32 touch sensor demonstration, we will use the touch sensor to control an LED. When the touch sensor is touched, the LED will turn on; when released, the LED will turn off.

Components Needed

- ESP32 development board(ESP32 Dev Kit/ ESP-Wroom-32)
- LED
- Resistor (220 ohms)
- Breadboard and jumper wires

Circuit Diagram

Following is the circuit diagram wherein, the ESP32 board GPIO 4 or Touch 0 pin is used as the touch sensor pin and a LED is connected to GPIO14 through a 220Ohm resistor. For the LED, connect the positive leg of the LED to GPIO 14 of the ESP32 through a 220-ohm resistor. And connect the negative leg of the LED to a GND pin of the ESP32.

esp32 touch sensor circuit diagram

Program Code

Here's the code to control the LED with a touch sensor:

// Controlling an LED with a Touch Sensor

const int touchPin = 4;    // Touch pin T0 (GPIO 4)
const int ledPin = 14;     // GPIO where the LED is connected

void setup() {
  pinMode(ledPin, OUTPUT); // Set LED pin as an output
  Serial.begin(115200);    // Initialize serial communication
}

void loop() {
  int touchValue = touchRead(touchPin); // Read the touch sensor value
  Serial.println(touchValue);           // Print the touch sensor value to the Serial Monitor

  // If touch is detected (value is below a certain threshold), turn on the LED
  if(touchValue < 20) {
    digitalWrite(ledPin, HIGH);  // Turn LED on
  } else {
    digitalWrite(ledPin, LOW);   // Turn LED off
  }
  delay(100);
}

Uploading and Testing

1. Connect your ESP32 to your computer and open the Arduino IDE.
2. Select your ESP32 board and the correct COM port from the `Tools` menu.
3. Click the upload button.
4. Once the code is uploaded, open the Serial Monitor.
5. Touch the touch sensor (GPIO 4) and observe the LED turning on and off.

Wrapping Up

In this unit, you learned about the ESP32 touch sensor and how to use it to control an LED. This basic concept can be extended to various touch-based interfaces and controls in your ESP32 projects.

Post a Comment

Previous Post Next Post