Your microcontroller board has capacitive touch capabilities in the form of capacitive touch pads. The CircuitPython touchio module makes it simple to detect when you touch a pad, enabling you to use it as an input.

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

Capacitive Touch Pad

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

Touch Pad Location

The capacitive touch pad is located on the end of the board opposite the USB connector. It can be touched on the top or bottom of the board.

Reading the Touch Pad

In the example below, click the Download Project Bundle button below to download the necessary libraries and the file in a zip file. Extract the contents of the zip file, open the directory Rotary_Trinkey/CircuitPython_Cap_Touch_Example/ 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:

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""CircuitPython Capacitive Touch Example for Rotary Trinkey"""
import time
import board
import touchio

touch = touchio.TouchIn(board.TOUCH)

while True:
    if touch.value:
        print("Pad touched!")

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

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 touch pad pin name using the board module. You save that to the touch variable.

Inside the loop, you check to see if the touch pad 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 a single touch pad using touchio in CircuitPython!

Touch Pad Available

The capacitive touch pad is on the end of the board opposite the USB connector. In CircuitPython, it is usable at board.TOUCH.

This guide was first published on Jun 02, 2021. It was last updated on Jun 12, 2024.

This page (Capacitive Touch) was last updated on Jun 12, 2024.

Text editor powered by tinymce.