Capacitive Touch Keyboard with CircuitPython: Adafruit Circuit Playground Express

Updated 05 May 2018

On the lower left-hand portion of every Adafruit Circuit Playground Express, there is a small onboard speaker and class D amplifier. This allows you to make some pretty loud sounds! The amplifier is also connected to the true analog output on pin A0, which is marked with a wavy line symbol. You can turn off the speaker and only output through the pin if you like. For this tutorial, we are going to use the speaker and the capacitive touchpads to make a capacitive touch keyboard! 


Adafruit Circuit Playground Express Speaker and AmpAdafruit Circuit Playground Express Audio Out

There are seven capacitive touchpads on the Circuit Playground Express which means we have a pad for each note in a scale! You can either touch the pads directly or use alligator clips or jumpers to connect to any capacitive/conductive item you like. You can make the keys out of tools, fruit, or even liquid! I have used small amounts of copper tape connected with alligator clips. If you decide to connect to something with jumpers, be aware that allowing the wires to touch each other may give you false readings. Either route them in a way that none touch each other, or I found that if the wires are not moved after the board is powered up everything works even if they touch.

Adafruit Circuit Playground Express Capacitive Touchpads

The Code

Here is the complete code in CircuitPython.

from adafruit_circuitplayground.express import cpx

while True:
    if cpx.switch:
        print("Slide switch off!")
        cpx.pixels.fill((0, 0, 0))
        cpx.stop_tone()
        continue
    if cpx.touch_A4:
        print('Touched A4!')
        cpx.pixels.fill((15, 0, 0))
        cpx.start_tone(262)
    elif cpx.touch_A5:
        print('Touched A5!')
        cpx.pixels.fill((15, 5, 0))
        cpx.start_tone(294)
    elif cpx.touch_A6:
        print('Touched A6!')
        cpx.pixels.fill((15, 15, 0))
        cpx.start_tone(330)
    elif cpx.touch_A7:
        print('Touched A7!')
        cpx.pixels.fill((0, 15, 0))
        cpx.start_tone(349)
    elif cpx.touch_A1:
        print('Touched A1!')
        cpx.pixels.fill((0, 15, 15))
        cpx.start_tone(392)
    elif cpx.touch_A2 and not cpx.touch_A3:
        print('Touched A2!')
        cpx.pixels.fill((0, 0, 15))
        cpx.start_tone(440)
    elif cpx.touch_A3 and not cpx.touch_A2:
        print('Touched A3!')
        cpx.pixels.fill((5, 0, 15))
        cpx.start_tone(494)
    elif cpx.touch_A2 and cpx.touch_A3:
        print('Touched "8"!')
        cpx.pixels.fill((15, 0, 15))
        cpx.start_tone(523)
    else:
        cpx.pixels.fill((0, 0, 0))
        cpx.stop_tone()

Programming capacitive touch sensitivity on the Circuit Playground Express and CircuitPython is easy because it is supported within the “cpx” library. In the main body of code, we have some logic that deactivates the capacitive touch if the slide switch is on. This is kind of like an on/off switch, but it doesn’t power down the board. We then have an if/else if series that checks for any detected touch on each of the capacitive touch sensitive pins and lights the NeoPixels and plays a tone when a touch is detected. If no touch is detected, then the lights are turned off and the tones are stopped.

Now you have a functional capacitive touch keyboard on your Circuit Playground Express. Try playing around with the code to make multiple tones play at once! Another element that you could add is some sharp/flat or minor notes by touching two keys at once. If you want to learn how to make the same project using CircuitPython, check out our Circuit Playground Tutorials!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.