CircuitPython Cap Touch

This quick-start example shows how you can read the capacitive touch sensors built into three of the Trinket M0 pins.

Note that because we are using the built in hardware capacitive touch support, you can only use pins D1, D3 and D4

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

# Trinket IO demo - captouch

import touchio
import board
import time

touch0 = touchio.TouchIn(board.D1)
touch1 = touchio.TouchIn(board.D3)
touch2 = touchio.TouchIn(board.D4)

while True:
    if touch0.value:
        print("D1 touched!")
    if touch1.value:
        print("D3 touched!")
    if touch2.value:
        print("D4 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

All three pads can be used as capacitive TouchIn devices:

touch0 = touchio.TouchIn(D1)
touch1 = touchio.TouchIn(D3)
touch2 = touchio.TouchIn(D4)

Creates three objects, one connected to each pin that has hardware capacitive touch support

Main Loop

The main loop checks each sensor one after the other, to determine if it has been touched. If touch0.value returns True, that means that that pin D1, detected a touch. For each pin, 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 pins directly, but you may want to attach wires to foil tape, 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 to Male Jumper Wire Bundle - 12 Pieces

PRODUCT ID: 3255
For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little...
$7.95
IN STOCK
Last updated on 2017-12-01 at 04.45.54 PM Published on 2017-08-23 at 05.57.02 PM