Program the Circuit Playground

If you haven't yet, visit the Circuit Playground Express introduction guide to learn about the CPX and how to install CircuitPython

Connect the Circuit Playground to your computer, copy the below code and save it as a file named on your Circuit Playground's root directory.

# Big Control Alt Delete Board
# Code is written for the Circuit Playground Express board:
# Needs the NeoPixel module installed:
# Author: Collin Cunningham
# License: MIT License (

from digitalio import DigitalInOut, Direction, Pull
import board
import time
import neopixel
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.2)

# The pins connected to each switch/button
buttonpins = [board.A7, board.A5, board.A3]
# The pins connected to each LED
ledpins = [board.A6, board.A4, board.A2]

# our array of button & LED objects
buttons = []
leds = []

# The keycode sent for each switch/button, will be paired with a control key
buttonkeys = [Keycode.CONTROL, Keycode.ALT, Keycode.DELETE]
buttonspressed = [False, False, False]
buttonspressedlast = [False, False, False]

# the keyboard object!
kbd = Keyboard()
# we're americans :)
layout = KeyboardLayoutUS(kbd)

# make all button pin objects, make them inputs w/pullups
for pin in buttonpins:
    button = DigitalInOut(pin)
    button.direction = Direction.INPUT
    button.pull = Pull.UP

# make all LED objects, make them inputs w/pullups
for pin in ledpins:
    led = DigitalInOut(pin)
    led.direction = Direction.OUTPUT

# set up the status LED
statusled = DigitalInOut(board.D13)
statusled.direction = Direction.OUTPUT

print("Waiting for button presses")

def pressbutton(i):
    l = leds[i]          # find the switch LED
    k = buttonkeys[i]    # get the corresp. keycode/str
    l.value = True       # turn on LED         # send keycode
def releasebutton(i):
    l = leds[i]          # find the switch LED
    k = buttonkeys[i]    # get the corresp. keycode/str
    l.value = False      # turn on LED
    kbd.release(k)       # send keycode
def lightneopixels():
    vals = [0, 0, 0]
    # if switch 0 pressed, show blue
    if buttonspressed[0]:
        vals[2] = 255
    # if switch 1 pressed, show yellow
    if buttonspressed[1]:
        vals[0] = 127
        vals[1] = 64
    # if switch 2 pressed, show red
    if buttonspressed[2]:
        vals[0] = 255
    # if all pressed, show white
    if buttonspressed[0] and buttonspressed[1] and buttonspressed[2]:
        vals = [255,255,255]
    # if 0 & 1 pressed, show green
    if buttonspressed[0] and buttonspressed[1] and not buttonspressed[2]:
        vals = [0,255,0]

while True:
    # check each button
    for button in buttons:
        i = buttons.index(button)
        if button.value == False:    # button is pressed?
            buttonspressed[i] = True # save pressed button
            if buttonspressedlast[i] == False: # was button not pressed last time?
                print("Pressed #%d" % i)
            buttonspressed[i] = False # button was not pressed
            if buttonspressedlast[i] == True: # was button pressed last time?
                print("Released #%d" % i)
    # save pressed buttons as pressed last
    buttonspressedlast = list(buttonspressed)

If everything worked correctly, pressing a switch should light its LED, change the color of the Circuit Playground's neopixels, and send a keyboard command out over USB to your computer. Additionally, pressing multiple switches simultaneously should mix each switch's color and display it on the Circuit Playground.

Last updated on 2018-01-23 at 02.18.57 PM Published on 2018-01-23 at 11.33.40 AM