CircuitPython Cap Touch

This quick-start example shows how you can read the capacitive touch sensors built into on seven of the CIrcuit Playground Express pads (pad A0 is not a capacitive touch pad).

Copy and paste the code block into code.py using your favorite text editor, and save the file, to run the demo

#CircuitPlaygroundExpress_CapTouch

import touchio
import board
import time

touch1 = touchio.TouchIn(board.A1)
touch2 = touchio.TouchIn(board.A2)
touch3 = touchio.TouchIn(board.A3)
touch4 = touchio.TouchIn(board.A4)
touch5 = touchio.TouchIn(board.A5)
touch6 = touchio.TouchIn(board.A6)
touch7 = touchio.TouchIn(board.A7)


while True:
    if touch1.value:
        print("A1 touched!")
    if touch2.value:
        print("A2 touched!")
    if touch3.value:
        print("A3 touched!")
    if touch4.value:
        print("A4 touched!")
    if touch5.value:
        print("A5 touched!")
    if touch6.value:
        print("A6 touched!")
    if touch7.value:
        print("A7 touched!")

    time.sleep(0.01)

You can open up the serial console to see the touches detected and printed out.

Creating an capacitive touch input

Pads A1 - A7 can be used as capacitive TouchIn devices:

touch1 = touchio.TouchIn(board.A1)
touch2 = touchio.TouchIn(board.A2)
touch3 = touchio.TouchIn(board.A3)
touch4 = touchio.TouchIn(board.A4)
touch5 = touchio.TouchIn(board.A5)
touch6 = touchio.TouchIn(board.A6)
touch7 = touchio.TouchIn(board.A7)

Creates seven objects, one connected to each of the cap touch pads.

Main Loop

The main loop checks each sensor one after the other, to determine if it has been touched. If touch1.value returns True, that means that that pad, A1, detected a touch. For each pad, if it has been touched, a message will print.

A small sleep delay is added at the end so the loop doesn't run too fast. You may want to change the delay from 0.1 seconds to 0 seconds to slow it down or speed it up.

Note that no extra hardware is required, you can touch the pads directly, but you may want to attach alligator clips or foil tape to metallic or conductive objects. Try silverware, fruit or other food, liquid, aluminum foil, and items around your desk!

You may need to restart your code/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-signals or not enough, hit that reset button!

Copper Foil Tape with Conductive Adhesive - 6mm x 15 meter roll

PRODUCT ID: 1128
Copper tape can be an interesting addition to your toolbox. The tape itself is made of thin pure copper so its extremely flexible and can take on nearly any shape. You can easily solder...
$5.95
IN STOCK

Copper Foil Tape with Conductive Adhesive - 25mm x 15 meter roll

PRODUCT ID: 1127
Copper tape can be an interesting addition to your toolbox. The tape itself is made of thin pure copper so its extremely flexible and can take on nearly any shape. You can easily solder...
$19.95
IN STOCK

Small Alligator Clip Test Lead (set of 12)

PRODUCT ID: 1008
Connect this to that without soldering using these handy mini alligator clip test leads. 15" cables with alligator clip on each end, color coded. You get 12 pieces in 6 colors....
$3.95
IN STOCK
Last updated on 2017-12-01 at 04.56.58 PM Published on 2017-10-12 at 10.43.39 PM