Capacitive Touch

We're going to use CircuitPython, Mu and the capacitive touch pads built into the Circuit Playground Express to plot the raw capacitive touch values. We'll run this code on our Circuit Playground Express and use Mu to plot the capacitive touch data that CircuitPython prints out.

Save the following as code.py on your Circuit Playground Express board, using the Mu editor:

import time

import board
import touchio

touch_A1 = touchio.TouchIn(board.A1)
touch_A2 = touchio.TouchIn(board.A2)
touch_A5 = touchio.TouchIn(board.A5)
touch_A6 = touchio.TouchIn(board.A6)

while True:
    value_A1 = touch_A1.raw_value
    value_A2 = touch_A2.raw_value
    value_A5 = touch_A5.raw_value
    value_A6 = touch_A6.raw_value
    print((value_A1, value_A2, value_A5, value_A6))
    time.sleep(0.1)

Our code is fairly simple. First we import time, touchio and board. Next, we setup four touch pads, A1, A2, A5 and A6. Inside our while loop, we assign value_PadName to the raw_value for each pad, e.g. value_A1 = touch_A1.raw_value, etc. Then we print those values in a tuple. And last, we have a time.sleep(0.1) to slow down the reading of the values - if it's too fast, it's really hard to read!

Note that the Mu plotter looks for tuple values to print. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14).  We have four values, (value_A1, value_A2, value_A5, value_A6), and note that the tuple itself has its own parentheses. Thus the extra parentheses in print((value_A1, value_A2, value_A5, value_A6)).

Once you have everything up and running, try touching any of the four pads, A1, A2, A5 or A6 to see the plotter respond immediately! Try touching only one pad to see of the four lines in the plotter go up. Try touching more than one pad at once to see multiple lines move. The harder you touch the pad, the higher the plotter line will go!

This is a great way to test your capacitive touch pads and plot the changes as you touch different pads!

This guide was first published on Apr 09, 2018. It was last updated on Nov 14, 2018. This page (Capacitive Touch) was last updated on May 17, 2018.