Nearly all CircuitPython boards provide capacitive touch capabilities. This means each board has at least one pin that works as an input when you touch it! For SAMD21 (M0) boards, the capacitive touch is done in hardware, so no external resistors, capacitors or ICs required. On SAMD51 (M4), nRF52840, and some other boards, Adafruit uses a software solution: you will need to add a 1M (1 megaohm) resistor from the pin to ground.

On the Circuit Playground Bluefruit (nrf52840) board, the necessary resistors are already on the board, so you don't need to add them.

This example will show you how to use a capacitive touch pin on your board.

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 CircuitPython_Essentials/CircuitPython_CapTouch/ 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:

CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials Capacitive Touch example"""
import time
import board
import touchio

touch_pad = board.A0  # Will not work for Circuit Playground Express!
# touch_pad = board.A1  # For Circuit Playground Express

touch = touchio.TouchIn(touch_pad)

while True:
    if touch.value:
        print("Touched!")
    time.sleep(0.05)

Create the Touch Input

First, we assign the variable touch_pad to a pin. The example uses A0, so we assign touch_pad = board.A0. You can choose any touch capable pin from the list below if you'd like to use a different pin. Then we create the touch object, name it touch and attach it to touch_pad.

To use with Circuit Playground Express, comment out touch_pad = board.A0 and uncomment touch_pad = board.A1.

Main Loop

Next, we create a loop that checks to see if the pin is touched. If it is, it prints to the serial console. Connect to the serial console to see the printed results when you touch the pin!

Remember: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # + space from the beginning of the line.

No extra hardware is required, because you can touch the pin directly. However, you may want to attach alligator clips or copper tape to metallic or conductive objects. Try metal flatware, fruit or other foods, liquids, aluminum foil, or other items lying around your desk!

You may need to reload your code or restart your board after changing the attached item because the capacitive touch code "calibrates" based on what it sees when it first starts up. So if you get too many touch responses or not enough, reload your code through the serial console or eject the board and tap the reset button!

Find the Pin(s)

Your board may have more touch capable pins beyond A0. We've included a list below that helps you find A0 (or A1 in the case of CPX) for this example, identified by the magenta arrow. This list also includes information about any other pins that work for touch on each board!

To use the other pins, simply change the number in A0 to the pin you want to use. For example, if you want to use A3 instead, your code would start with touch_pad = board.A3.

If you would like to use more than one pin at the same time, your code may look like the following. If needed, you can modify this code to include pins that work for your board.

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 CircuitPython_Essentials/CircuitPython_CapTouch_2Pins/ 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:

CIRCUITPY
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials Capacitive Touch on two pins example. Does not work on Trinket M0!"""
import time
import board
import touchio

touch_A1 = touchio.TouchIn(board.A1)  # Not a touch pin on Trinket M0!
touch_A2 = touchio.TouchIn(board.A2)  # Not a touch pin on Trinket M0!

while True:
    if touch_A1.value:
        print("Touched A1!")
    if touch_A2.value:
        print("Touched A2!")
    time.sleep(0.05)
This example does NOT work for Trinket M0! You must change the pins to use with this board. This example only works with Gemma, Circuit Playground Express, Feather M0 Express, Metro M0 Express and ItsyBitsy M0 Express.

Use the list below to find out what pins you can use with your board. Then, try adding them to your code and have fun!

Trinket M0

 

There are three touch capable pins on Trinket: A0, A3, and A4.

 

Remember, A0 is labeled "1~" on Trinket M0!

 

Gemma M0

 

There are three pins on Gemma, in the form of alligator-clip-friendly pads, that work for touch input: A0, A1 and A2.

QT Py M0

 

There are six pins on QT Py that work for touch input: A0 - A3, TX, and RX.

Feather M0 Express

 

There are 6 pins on the Feather that have touch capability: A0 - A5.

ItsyBitsy M0 Express

 

There are 6 pins on the ItsyBitsy that have touch capability: A0 - A5.

Metro M0 Express

 

There are 6 pins on the Metro that have touch capability: A0 - A5.

Circuit Playground Express

 

Circuit Playground Express has seven touch capable pins!  You have A1 - A7 available, in the form of alligator-clip-friendly pads. See the CPX guide Cap Touch section for more information on using these pads for touch!

 

Remember: A0 does NOT have touch capabilities on CPX.

This guide was first published on Sep 30, 2020. It was last updated on Mar 19, 2024.

This page (CircuitPython Cap Touch) was last updated on Mar 18, 2024.

Text editor powered by tinymce.