How to use touch inputs on the ESP32

Adafruit Huzzah32 ESP32 Feather dev board

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.

See related product

3405

Adafruit Industries View

Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here