CircuitPython Cap Touch

Every CircuitPython designed M0 board has capacitive touch capabilities. This means each board has at least one pin that works as an input when you touch it! The capacitive touch is done completely in hardware, so no external resistors, capacitors or ICs required. Which is really nice!

Capacitive touch is not supported on the M4 Express boards.

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

Copy and paste the code into code.py using your favorite editor, and save the file.

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.

# CircuitPython Demo - Cap Touch Multiple Pins
# Example does NOT work with 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.

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 Jul 26, 2017. It was last updated on Nov 14, 2018. This page (CircuitPython Cap Touch) was last updated on Jul 13, 2018.