Code
Setup
If you've never used the Circuit Playground Express with CircuitPython before, you'll need to do some setup. Plug in the Circuit Playground Express to your computer via a known good data+power USB cable.
If you see a new flash drive on your computer named CIRCUITPY, all is good.
If you don't see CIRCUITPY, then double click the little reset button in the center to put it into bootloader mode. When the on-board NeoPixels all turn green and a USB storage drive called CPLAYBOOT shows up, we'll need to load a fresh copy of CircuitPython.
You’ll need to follow the below guide to set up CircuitPython and then come back to continue.
Getting the Program's Files
To use the application, you need to obtain code.py with the program and the library files that make the code run.
Thankfully, this can be done in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file.
Connect your board to your computer via a known good data+power USB cable. The board should show up in your File Explorer/Finder (depending on your operating system) as a flash drive named CIRCUITPY.
Extract the contents of the zip file, copy the lib directory files to CIRCUITPY/lib. Copy the code.py to the CIRCUITPY drive on the Circuit Playground Express via your computer. The program should self start.
# SPDX-FileCopyrightText: Erin St. Blaine for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Circuit Playground Express shake trigger.
Reads accelerometer magnitude and, when it exceeds SHAKE_THRESHOLD,
pulses pin A1 HIGH for TRIGGER_DURATION seconds, then enforces a cooldown.
Also sets a fixed NeoPixel pattern at startup.
"""
import math
import time
import board
import digitalio
from adafruit_circuitplayground.express import cpx
SHAKE_THRESHOLD = 11.0
TRIGGER_DURATION = 3.0 # seconds
COOLDOWN = 1.0 # seconds to wait after trigger
LOOP_DELAY = 0.01 # seconds
def setup_a1_output() -> digitalio.DigitalInOut:
"""Configure A1 as a digital output, default LOW."""
a1_output = digitalio.DigitalInOut(board.A1)
a1_output.direction = digitalio.Direction.OUTPUT
a1_output.value = False
return a1_output
def set_pixel_pattern() -> None:
"""Set the NeoPixel ring to a red/green pattern."""
red = (255, 0, 0)
green = (0, 255, 0)
pattern = (
red, red, red,
green, green,
red, red, red,
green, green,
)
cpx.pixels.auto_write = False
cpx.pixels.brightness = 0.2
for index, color in enumerate(pattern):
cpx.pixels[index] = color
cpx.pixels.show()
def acceleration_magnitude() -> float:
"""Return the magnitude of the CPX acceleration vector."""
accel_x, accel_y, accel_z = cpx.acceleration
return math.sqrt((accel_x ** 2) + (accel_y ** 2) + (accel_z ** 2))
def main() -> None:
"""Main loop."""
a1_pin = setup_a1_output()
set_pixel_pattern()
while True:
magnitude = acceleration_magnitude()
print(
f"Magnitude: {magnitude:.2f} | "
f"Threshold: {SHAKE_THRESHOLD:.2f} | "
f"A1 value: {a1_pin.value}"
)
if magnitude > SHAKE_THRESHOLD:
print(">>> SHAKE DETECTED! Pulsing A1 HIGH")
a1_pin.value = True
print(f"A1 is now: {a1_pin.value}")
time.sleep(TRIGGER_DURATION)
a1_pin.value = False
print(f"A1 is now: {a1_pin.value}")
time.sleep(COOLDOWN)
time.sleep(LOOP_DELAY)
main()
The code should automatically run once you save the file. You'll see red and green lights on your Circuit Playground Express. Tap or shake your board and the air pump will blow for 3 seconds.
If you want to edit the code, Mu is a good choice for a code editor. Mu is written in Python, and works on all 3 major platforms: MacOS, Windows and Linux (including the Raspberry Pi). It has an awesome CircuitPython mode, and has a built-in serial console, so you can easily see text output from your code too. You can follow the below instructions to install Mu:
Code Walkthrough
Near the top of the code are some variables you can change to dial in the sensitivity and timing of your project.
SHAKE_THRESHOLD = 11.0
- Raise this number to make the tap less sensitive or lower it to make it more sensitive
TRIGGER_DURATION = 3.0 # seconds
- The length of time the air pump stays on for - change as desired
COOLDOWN = 1.0 # seconds to wait after trigger
- Change this to adjust the time between triggers
To change the light colors or pattern, look for this section of code:
def set_pixel_pattern() -> None:
"""Set the NeoPixel ring to a red/green pattern."""
red = (255, 0, 0)
green = (0, 255, 0)
pattern = (
red, red, red,
green, green,
red, red, red,
green, green,
)
To use different colors, input the RGB values (here's a handy color calculator) of the color you want and insert the color into the pattern list.
Page last edited February 17, 2026
Text editor powered by tinymce.