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

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

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

touch = touchio.TouchIn(board.A4)

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 thepin 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

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

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

touch_one = touchio.TouchIn(board.A4)
touch_two = touchio.TouchIn(board.A5)

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 2022-01-12 17:03:12 -0500.

This page (Capacitive Touch) was last updated on Jan 16, 2022.

Text editor powered by tinymce.