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 on the end of the Slider Trinkey, on the opposite end from the USB connector. The touch pad is on both the bottom and top of the board.

Reading the Touch Pad

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

Then, connect to the serial console.

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

"""CircuitPython capacitive touch example for Slider 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 touch pad is located on the end of the board, opposite the USB connector. It can be read in CircuitPython at board.TOUCH.

This guide was first published on Jun 16, 2021. It was last updated on 2021-06-23 11:03:48 -0400.

This page (Capacitive Touch) was last updated on May 24, 2022.

Text editor powered by tinymce.