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

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

"""NeoKey Trinkey Capacitive Touch Example"""
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

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 2021-05-27 13:09:21 -0400.

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

Text editor powered by tinymce.