Lesson 6: Capacitive touch sensing

Table of Contents

  1. Materials
  2. ESP32’s touch sensor system
    1. Touch sensing vs. physical buttons
    2. The ESP32’s touch sensing API
      1. The ESP32 touch sensing Arduino API
      2. ESP32 touch sensing Arduino examples
  3. Let’s make stuff
    1. The circuit
    2. The code
    3. Workbench video
    4. Touch sensing interrupts
  4. 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.

Materials

You’ll need the following materials:

Breadboard ESP32 LED Resistor
Breadboard Huzzah32 Red LED 220 Ohm Resistor
Breadboard Huzzah32 Red LED 220Ω Resistor

ESP32’s touch sensor system

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:

Huzzah32 pin diagram See the Adafruit Huzzah32 docs for details. Right-click and open image in a new tab to zoom in. For more details on the capacitive touch pins, see the Espressif docs.

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.

Touch sensing vs. physical 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.

The ESP32’s touch sensing API

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

The ESP32 touch sensing Arduino API has three methods:

/*
 * Set cycles that measurement operation takes
 * The result from touchRead, threshold and detection
 * accuracy depend on these values. Defaults are
 * 0x1000 for measure and 0x1000 for sleep.
 * With default values touchRead takes 0.5ms
 * */
void touchSetCycles(uint16_t measure, uint16_t sleep);

/*
 * Read touch pad (values close to 0 mean touch detected)
 * You can use this method to chose a good threshold value
 * to use as value for touchAttachInterrupt
 * */
uint16_t touchRead(uint8_t pin);

/*
 * Set function to be called if touch pad value falls
 * below the given threshold. Use touchRead to determine
 * a proper threshold between touched and untouched state
 * */
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold);

ESP32 touch sensing Arduino examples

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 stuff

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.

The circuit

We use TOUCH6 (T6), which is GPIO Pin 14.

Circuit diagram with a wire hooked to T6

The code

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.

Workbench video

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!

Touch sensing interrupts

We also created two touch sensing examples using interrupts: TouchInterrupt uses touchAttachInterrupt on 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.

Next Lesson

In the next lesson, we will connect our ESP32 to the cloud over WiFi and use an IoT dashboard to view our data.

Previous: Playing tones Next: Making an IoT device


This website was developed by Professor Jon E. Froehlich and the Makeability Lab using Just the Docs. If you found the website useful or use it in your teaching, we'd love to hear from you: jonf@cs.uw.edu. This website and all code is open source (website GitHub, Arduino GitHub, p5js GitHub). You can find the MakeabilityLab_Arduino_Library here. Found an error? File a GitHub Issue.