Capacitive Touch

The Circuit Playground Express and Bluefruit have seven capacitive touch pads around the outside, labeled A1 - A6 and TX. Though the images are of the Circuit Playground Express, the touch pads are in the same location on the Bluefruit. These pads return True if you touch them. So you can use them as inputs to do all sorts of fun stuff!

Since the pads are capacitive, you can also attach alligator clips to them and any number of capacitive items and touch those to activate them as well! For example, you could attach one end of an alligator clip to one of the pads and the other end to an apple or a lime. Or place the other end in a glass of water. Then touch the fruit or the glass of water. You'll activate the pad!

Add the following code to your code.py. Remember, if you need help with this, check here.

"""This example prints to the serial console when you touch capacitive touch pad A1."""
from adafruit_circuitplayground import cp

while True:
    if cp.touch_A1:
        print("Touched pad A1")

Open the serial console. Now, touch the pad labeled A1 on your Circuit Playground. Touched pad A1!

Let's look at the code. First we import time and cp.

Inside our loop, we check to see if pad A1 is touched with if cp.touch_A1:. If it is, we print Touched pad A1 to the serial console. Then we have a time.sleep(0.1) to slow down the speed of the printing.

Nice! But what about the rest of the touch pads? Add the following code to your code.py.

"""This example prints to the serial console when you touch the capacitive touch pads."""
from adafruit_circuitplayground import cp

while True:
    if cp.touch_A1:
        print("Touched pad A1")
    if cp.touch_A2:
        print("Touched pad A2")
    if cp.touch_A3:
        print("Touched pad A3")
    if cp.touch_A4:
        print("Touched pad A4")
    if cp.touch_A5:
        print("Touched pad A5")
    if cp.touch_A6:
        print("Touched pad A6")
    if cp.touch_TX:
        print("Touched pad TX")

Now look at the serial console and touch any of the touch pads. Touched pad...!

The code begins the same way. But, we've added in another two lines for each touch pad. We check if each pad is touched, and if it is, we print Touched pad and the pad number to the serial console.

Now we've included all of the touch pads. Let's do something with them!

Touch the Rainbow

Add the following code to your code.py.

"""This example uses the capacitive touch pads on the Circuit Playground. They are located around
the outer edge of the board and are labeled A1-A6 and TX. (A0 is not a touch pad.) This example
lights up the nearest NeoPixel to that pad a different color of the rainbow!"""
import time
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.3

while True:
    if cp.touch_A1:
        print("Touched A1!")
        cp.pixels[6] = (255, 0, 0)
    if cp.touch_A2:
        print("Touched A2!")
        cp.pixels[8] = (210, 45, 0)
    if cp.touch_A3:
        print("Touched A3!")
        cp.pixels[9] = (155, 100, 0)
    if cp.touch_A4:
        print("Touched A4!")
        cp.pixels[0] = (0, 255, 0)
    if cp.touch_A5:
        print("Touched A5!")
        cp.pixels[1] = (0, 135, 125)
    if cp.touch_A6:
        print("Touched A6!")
        cp.pixels[3] = (0, 0, 255)
    if cp.touch_TX:
        print("Touched TX!")
        cp.pixels[4] = (100, 0, 155)
    time.sleep(0.1)

Now touch each touch pad. You get an LED in one color of the rainbow for each of them!

Now let's look at the code. We import time and cp. We set the LED brightness to 30%. We check to see if each pad is touched, and if it is, we print to the serial console. This time, though, we also light up a specific LED with each pad using cp.pixels[#] = (r, g, b) where # is the pixel number and r, g, b are the color values. We didn't include any code to tell the LEDs to turn off, so they will stay on once you turn them on.

Now let's add more light! Add the following code to your code.py.

"""This example uses the capacitive touch pads on the Circuit Playground. They are located around
the outer edge of the board and are labeled A1-A6 and TX. (A0 is not a touch pad.) This example
lights up all the NeoPixels a different color of the rainbow for each pad touched!"""
import time
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.3

while True:
    if cp.touch_A1:
        print("Touched A1!")
        cp.pixels.fill((255, 0, 0))
    if cp.touch_A2:
        print("Touched A2!")
        cp.pixels.fill((210, 45, 0))
    if cp.touch_A3:
        print("Touched A3!")
        cp.pixels.fill((155, 100, 0))
    if cp.touch_A4:
        print("Touched A4!")
        cp.pixels.fill((0, 255, 0))
    if cp.touch_A5:
        print("Touched A5!")
        cp.pixels.fill((0, 135, 125))
    if cp.touch_A6:
        print("Touched A6!")
        cp.pixels.fill((0, 0, 255))
    if cp.touch_TX:
        print("Touched TX!")
        cp.pixels.fill((100, 0, 155))
    time.sleep(0.1)

Touch each pad. You get every LED lit up in one color of the rainbow for each of them!

The code is almost identical, except instead of cp.pixels[#], we use cp.pixels.fill((r, g, b)) to light up every LED instead of only one.

You can change the color values to whatever you like to create your own personal rainbow. Give it a try!

This guide was first published on Jun 05, 2018. It was last updated on Jun 05, 2018. This page (Capacitive Touch) was last updated on Mar 30, 2020.