Your microcontroller board has capacitive touch capabilities on multiple pins. The CircuitPython touchio
module makes it simple to detect when you touch a pin, enabling you to use it as an input.
This section first covers using the touchio
module to read touches on one pin. You'll learn how to setup the pin in your program, and read the touch status. Then, you'll learn how to read touches on multiple pins in a single example. Time to get started!
One Capacitive Touch Pin
The first example covered here will show you how to read touches on one pin.
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_KB2040/Capacitive_Touch_One_Pin/ 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:

Then, .
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: Unlicense """ CircuitPython Capacitive Touch Pin Example - Print to the serial console when one pin is touched. """ import time import board import touchio touch = touchio.TouchIn(board.D9) while True: if touch.value: print("Pin touched!") time.sleep(0.1)
Now touch the pin indicated in the diagram above. You'll see Pin 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 pin name using the board
module. You save that to the touch
variable.
Inside the loop, you check to see if the pin 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 touch on a single pin using touchio
in CircuitPython!
Multiple Capacitive Touch Pins
The next example shows you how to read touches on multiple pins in a single program.
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_KB2040/Capacitive_Touch_Two_pins/ 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: Unlicense """ CircuitPython Capacitive Two Touch Pin Example - Print to the serial console when a pin is touched. """ import time import board import touchio touch_one = touchio.TouchIn(board.D9) touch_two = touchio.TouchIn(board.D10) while True: if touch_one.value: print("Pin one touched!") if touch_two.value: print("Pin two touched!") time.sleep(0.1)
Touch the pins 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 pin is provided to it using the board
module, and is saved to touch_two
.
Inside the loop, we check to see if pin one and pin two are touched, and if so, print to the serial console Pin one touched!
and Pin two touched!
, respectively. The same time.sleep()
is included.
If more touch-capable pins are available on your board, you can easily add them by expanding on this example!
-
A0 - CircuitPython
board.A0
. Arduino26
. -
A1 - CircuitPython
board.A1
. Arduino27
. -
A2 - CircuitPython
board.A2
. Arduino28
. -
A3 - CircuitPython
board.A3
. Arduino29
. -
TX - CircuitPython
board.TX
. Arduino0
. -
RX - CircuitPython
board.RX
. Arduino1
. -
D10 - CircuitPython
board.D10
. Arduino10
. -
D2 - CircuitPython
board.D2
. Arduino2
. -
D3 - CircuitPython
board.D3
. Arduino3
. -
D4 - CircuitPython
board.D4
. Arduino4
. -
D5 - CircuitPython
board.D5
. Arduino5
. -
D6 - CircuitPython
board.D6
. Arduino6
. -
D7 - CircuitPython
board.D7
. Arduino7
. -
D8 - CircuitPython
board.D8
. Arduino8
. -
D9 - CircuitPython
board.D9
. Arduino9
. -
MISO - CircuitPython
board.MISO
. Arduino20
. -
MOSI - CircuitPython
board.MOSI
. Arduino19
. -
SCK - CircuitPython
board.SCK
. Arduino18
.