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!
One Capacitive Touch Pad
The first example covered here will show you how to read touches on one touch pad.
The first capacitive touch pad (indicated in green in the image) is located in the corner of the board opposite the USB connector, next to the "1" label.
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 Adafruit_Proximity_Trinkey/Capacitive_Touch_One_Pad/ 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 Pad Example - Print to the serial console when one pad is touched. """ import time import board import touchio touch = touchio.TouchIn(board.TOUCH1) 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!
Multiple Capacitive Touch Pads
The next example shows you how to read touches on multiple touch pads in a single program.
The two capacitive touch pads (indicated in green in the image) are located on the end of the board opposite the USB connector. The first one is next to the "1" label, and the second one is next to the "2" label.
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 Adafruit_Proximity_Trinkey/Capacitive_Touch_Two_Pad/ 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 Two Touch Pad Example - Print to the serial console when a pad is touched. """ import time import board import touchio touch_one = touchio.TouchIn(board.TOUCH1) touch_two = touchio.TouchIn(board.TOUCH2) while True: if touch_one.value: print("Pad one touched!") if touch_two.value: print("Pad two touched!") time.sleep(0.1)
Touch the pads to see the messages printed to the serial console!
This example builds on the first. The imports remain the same.
The touchio.TouchIn()
object is created, but is instead saved to touch_one
. A second touchio.TouchIn()
object is also created, the second pad pin is provided to it using the board
module, and is saved to touch_two
.
Inside the loop, we check to see if pad one and pad two are touched, and if so, print to the serial console Pad one touched!
and Pad two touched!
, respectively. The same time.sleep()
is included.
If more touch pads are available on your board, you can easily add them by expanding on this example!
Touch Pads Available
The following list was obtained using this Python script.
The capacitive touch pads are on the opposite end of the board from the USB connector.
-
1 - The first touch pad is labeled "1" on the board. It is available in CircuitPython as
board.TOUCH1
. It is available in Arduino asPIN_TOUCH1
. -
2 - The second touch pad is labeled "2" on the board. It is available in CircuitPython as
board.TOUCH2
. It is available in Arduino asPIN_TOUCH12
.
Text editor powered by tinymce.