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.
# 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!
Text editor powered by tinymce.