Your microcontroller board has capacitive touch capabilities on multiple pins. The CircuitPython touchio module makes it simple to detect when you touch a pin, enabling you to use it as an input.

This section first covers using the touchio module to read touches on one pin. You'll learn how to setup the pin in your program, and read the touch status. Then, you'll learn how to read touches on multiple pins in a single example. Time to get started!

One Capacitive Touch Pin

The first example covered here will show you how to read touches on one pin.

Pin Location

  • A4 - Pin A4 is labeled on the silk, and located on the longer header side of the Feather, towards the center.

Reading Touch on the Pin

In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Templates/cap_touch_one_pin_ESP32-S2/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Capacitive Touch Pin Example for ESP32-S2.
Print to the serial console when one pin is touched.
"""
import time
import board
import touchio

touch = touchio.TouchIn(board.D8)

while True:
    if touch.value:
        print("Pin touched!")
    time.sleep(0.1)

Now touch the pin indicated in the diagram above. You'll see Pin touched! printed to the serial console!

You can add a short piece of hookup wire to the breadboard to make it easier to touch only the intended pin or pins.

First you import three modules: time, board and touchio. This makes these modules available for use in your code. All three are built-in to CircuitPython, so you don't find any library files in the Project Bundle.

Next, you create the touchio.TouchIn() object, and provide it the pin name using the board module. You save that to the touch variable.

Inside the loop, you check to see if the pin is touched. If so, you print to the serial console. Finally, you include a time.sleep() to slow it down a bit so the output is readable.

That's all there is to reading touch on a single pin using touchio in CircuitPython!

Multiple Capacitive Touch Pins

The next example shows you how to read touches on multiple pins in a single program.

Pin Location

  • A4 - Pin A4 is labeled on the silk, and located on the longer header side of the Feather, towards the center.
  • A5 - Pin A5 is labeled on the silk, and located on the longer header side of the Feather, towards the center.

Reading Touch on the Pins

In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Templates/cap_touch_two_pins_ESP32-S2/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython Capacitive Two Touch Pin Example for ESP32-S2
Print to the serial console when a pin is touched.
"""
import time
import board
import touchio

touch_one = touchio.TouchIn(board.D8)
touch_two = touchio.TouchIn(board.D5)

while True:
    if touch_one.value:
        print("Pin one touched!")
    if touch_two.value:
        print("Pin two touched!")
    time.sleep(0.1)

Touch the pins to see the messages printed to the serial console!

This example builds on the first. The imports remain the same.

The touchio.TouchIn() object is created, but is instead saved to touch_one. A second touchio.TouchIn() object is also created, the second pin is provided to it using the board module, and is saved to touch_two.

Inside the loop, we check to see if pin one and pin two are touched, and if so, print to the serial console Pin one touched! and Pin two touched!, respectively. The same time.sleep() is included.

If more touch-capable pins are available on your board, you can easily add them by expanding on this example!

The Available Touch-Capable Pins

Capacitive touch on the Feather ESP32-S2 is available on a limited set of pins. The pin names in bold match the Feather silk. The pin names in CircuitPython and Arduino are listed after.

  • A4 - CircuitPython: board.A4. Arduino: T14.
  • A5 - CircuitPython: board.A5. Arduino: T8.
  • D13 - CircuitPython: board.D13. Arduino: T13.
  • D12 - CircuitPython: board.D12. Arduino: T12.
  • D11 - CircuitPython: board.D11. Arduino: T11.
  • D10 - CircuitPython: board.D10. Arduino: T10.
  • D9 - CircuitPython: board.D9. Arduino: T9.
  • D6 - CircuitPython: board.D8. Arduino: T6.
  • D5 - CircuitPython: board.D7. Arduino: T5.
There are pins other than those listed above that are touch capable on the ESP32-S2 module itself, however, the layout and circuitry on the Feather render them unusable as touch pins.

This guide was first published on Nov 25, 2021. It was last updated on Nov 25, 2021.

This page (Capacitive Touch) was last updated on Mar 31, 2023.

Text editor powered by tinymce.