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

On the end of the board, opposite the USB connector, is a single capacitive touch pad that spans the width 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 code.py file in a zip file. Extract the contents of the zip file, open the directory NeoKey_Trinkey/CircuitPython_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:

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

"""NeoKey Trinkey Capacitive Touch Example"""
import time
import board
import touchio

touch = touchio.TouchIn(board.TOUCH)

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

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

There is one touch pad available on the NeoKey Trinkey, called board.TOUCH in CircuitPython.

This guide was first published on May 26, 2021. It was last updated on Jun 15, 2024.

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

Text editor powered by tinymce.