- ESP32’s touch sensor system
- Let’s make stuff
- Next Lesson
The ESP32 has built-in circuitry and software for capacitive touch sensing (docs). In this lesson, we’ll use the touch sensing functionality to turn on an LED.
You’ll need the following materials:
The ESP32’s touch sensor circuit measures the total capacitance on a touch channel. When the capacitance changes and the amount of change exceeds the threshold value, the system can detect finger contact or proximity.
The ESP32 has 10 capacitive touch pins; however, only eight are exposed on the Huzzah32:
Espressif sells the “ESP32-Sense Kit” to help highlight how capacitive touch sensing may be integrated into products, including linear touch sliders, a wheel slider, and matrix buttons.
Capacitive touch sensing is now widely used in household appliances, consumer electronics, and in industrial contexts. As the ESP32 docs enumerate, compared with mechanical buttons, capacitive touch sensing offers:
- No mechanical parts that wear over time
- Completely sealed surfaces (that can be waterproofed)
- Fewer components
- A modern look
However, the lack of physical buttons can reduce accessibility, especially for blind or low-vision users.
ESP32’s touch sensing API is described here; however, Espressif also developed an Arduino wrapper library to simplify its usage (.h file, .c file). Touch sensing is part of the core ESP32 Arduino library, so if you’ve installed an ESP32 board via the Arduino IDE, then you’ll be able to use the touch sensing library.
The ESP32 touch sensing Arduino API has three methods:
Espressif has created two touch sensing Arduino examples: one using polling (TouchRead.ino) and the other using an interrupt (TouchInterrupt.ino). These are also accessible in the Arduino IDE: File -> Examples -> ESP32 -> Touch.
Let’s make a simple, touch-sensitive LED light. We will poll
touchRead and determine when a touch has occurred based on a set threshold.
We use TOUCH6 (
T6), which is GPIO Pin 14.
The code is quite simple. We use
touchRead to measure the capacitive value of the pin. Values close to zero indicate a touch. We initially wrote a quick program to print
touchRead values to Serial and found that the
touchRead returned ~60-70 when the wire was untouched and 6-15 when the wire was touched. We then used this to setup a
TOUCH_THRESHOLD. To improve our approach, we could use basic smoothing (e.g., a mean filter) to reduce transient and erroneous low reads.
Our full implementation is on github:
This source code is on GitHub.
Here’s a workbench video demonstrating our TouchRead code using both a wire and aluminum foil as a conductor.
Our circuit has a potentiometer but this was just left over from the previous lesson and is not used here!
We also created two touch sensing examples using interrupts: TouchInterrupt uses
T6 to print to serial when a touch has been detected and TouchInterruptLed extends this to also turn on an LED for a set amount of time after a touch has been detected.
In the next lesson, we will connect our ESP32 to the cloud over WiFi and use an IoT dashboard to view our data.