Exploring Touch Sensors in ESP32: A Comprehensive Guide

2 min read


The ESP32 is a versatile microcontroller that offers a wide range of functionalities, including built-in touch sensors. These touch sensors can be used to create interactive and responsive projects, making it an exciting component for both beginners and experienced makers. In this guide, we will dive into how to enable and work with touch sensors in the ESP32.

Understanding ESP32 Touch Sensors

The ESP32 features a touch sensor matrix that allows you to create capacitive touch buttons or sensors. This means you can turn any conductive object, like a piece of metal or even a human finger, into a touch-sensitive input for your ESP32 project. The touch sensor matrix on the ESP32 can detect touch on multiple pins simultaneously.

Enabling Touch Sensing in ESP32

To start working with touch sensors in the ESP32, you need to enable the touch sensing feature. Follow these steps:

Initialize Touch Pins:

Before using the touch pins, you need to initialize them in your setup() function. You can use the touchAttachInterrupt() function to specify the pins you want to use for touch sensing and the callback functions that will be triggered when a touch event occurs.

void setup() {

// Initialize touch pins

touchAttachInterrupt(T0, touchHandler1, threshold);

touchAttachInterrupt(T1, touchHandler2, threshold);

// Add more touch pins as needed


Implement Touch Callbacks:

Create callback functions that will be executed when a touch event is detected on the respective pins. These functions should be defined as void and take no arguments.

void touchHandler1() {

// Code to execute when touch on T0 is detected


void touchHandler2() {

// Code to execute when touch on T1 is detected


Define Thresholds (Optional):

You can set a threshold for touch sensitivity. If the touch value exceeds this threshold, the corresponding callback function will be triggered. Adjust this threshold according to your project's requirements.

int threshold = 30; // Adjust the threshold value as needed

Reading Touch Values

To read the touch values from the sensors, you can use the touchRead(pin) function. This function returns a value between 0 and 65535, representing the capacitance on the specified pin. You can use these values to implement various touch-based interactions in your project.

int touchValue = touchRead(T0); // Read touch value from T0

Example Project: LED Toggle

Let's create a simple example project to demonstrate the use of touch sensors. We will use two touch pins to toggle an LED on and off.

const int ledPin = 2;

const int touchPin1 = T0;

const int touchPin2 = T1;

int threshold = 30;

void setup() {

pinMode(ledPin, OUTPUT);

touchAttachInterrupt(touchPin1, touchHandler1, threshold);

touchAttachInterrupt(touchPin2, touchHandler2, threshold);


void loop() {

// Main loop code


void touchHandler1() {

digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle the LED state


void touchHandler2() {

digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle the LED state


The ESP32's touch sensor functionality opens up a world of possibilities for creating interactive and responsive projects. By enabling and working with touch sensors, you can build innovative applications, from touch-sensitive interfaces to capacitive proximity detectors. Experiment with different threshold values and callbacks to fine-tune your touch-based interactions and unlock the full potential of the ESP32. Happy tinkering!