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.

Touch Pad Location

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.

Reading the Touch Pad

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

# 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.

Touch Pad Location

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.

Reading the Touch Pads

Download the following file by clicking the Download Project Bundle button. Drag the contents of the bundle, the code.py file, to your CIRCUITPY drive.

Then, connect to the serial console.

# 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 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 as PIN_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 as PIN_TOUCH12.

This guide was first published on Sep 29, 2021. It was last updated on 2021-09-29 11:19:56 -0400.

This page (Capacitive Touch) was last updated on Jul 03, 2022.

Text editor powered by tinymce.