By Jeremy Cook
Simple pushbutton inputs (i.e. keyboards) have provided the primary method for interfacing with computers for many decades. Today, especially with the advent of smartphones, many devices instead use capacitive touchscreens as their primary interface.
Microcontrollers can take advantage of capacitive touch functionality as well, and the ESP32 module has this capability built into ten of its GPIO pins, providing a low-cost and extremely responsive input method.
Arduino IDE Touch Interface Setup
To try out the ESP32’s touch capability via the Arduino IDE, first install the ESP32 board definition through the Arduino Boards Manager. Then select your particular device.
Open up the touch example in File > Examples > ESP32 > TouchRead. Compile* and send it to your selected ESP32 board.
*MacOS note: If you have difficulty compiling your sketch, see this post for information on a MacOS update that may cause issues. I replaced all occurrences of “python” with “python3” in platform.txt, which seems to fix the issue.
Then, attach a wire to touch pin 0 (T0, which is GPIO 4), and ground.
Serial Plotter also works well for output monitoring.
With this hardware in place, open up a serial monitor at 115200 baud. Then touch and release both wires with your fingers. The resulting output will be a value somewhere in the 80s with nothing touching it, which decreases significantly when touched. This numerical decrease is due to the addition of your body’s capacitance, which can be used as a switch.
Ten such interface pins are available, and the ESP32 can even be set up to wake from a low-power sleep mode via touch.
In one usage example, this type of input is used to control the retro television clock seen in this article. Note that GPIO pins do not generally correspond to the touch sensor pin number.
ESP32 Touch Sensing Explained
The ESP32 uses a dedicated peripheral in order to sense the change in a pin’s capacitance. As capacitance increases due to a human touch, the time it takes a capacitive sensing pin to cycle between a high and low state increases proportionally. Thus, when the input/ground is touched, tacking on a human’s capacitance, the cycle time increases, and the number of cycles counted in a given time period decreases.
Very low cycle values are seen when actual capacitor components are attached between T0/GPIO 4 and ground.
ESP32 Touch Sensing: A Wide Range of Uses
In practice, you’ll want to measure the output value with and without a human touch in order to determine a threshold for programmatic decision making. Between its easy hardware implementation, quick response time, and wakeup capability, this touch peripheral could be useful in a wide range of situations.