Once you have CircuitPython installed on your board, it's time to add the project code and libraries.
Source Code and Example Images
CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this 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. Extract the contents of the zip file, open the directory Rapunzel_Hair/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.
Your CIRCUITPY
# SPDX-FileCopyrightText: Erin St Blaine and ChatGPT for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from adafruit_circuitplayground import cp import neopixel # Constants NUM_PIXELS_STRIP = 20 NUM_PIXELS_FACE = 10 # Number of NeoPixels on the face PIXEL_PIN_STRIP = board.A1 SOUND_THRESHOLD = 50 # Adjust this threshold based on your environment # Variables DELAY_AFTER_LIGHT_UP = 2.0 COLOR = (255, 100, 0) # Warm yellow color SPEED = 0.3 # Animation speed (adjust as needed, higher number moves more slowly) # Initialize NeoPixels on face pixels_face = cp.pixels pixels_face.brightness = 0.5 # Initialize NeoPixels on strip (A1) pixels_strip = neopixel.NeoPixel(PIXEL_PIN_STRIP, NUM_PIXELS_STRIP, brightness=0.5) # Main loop while True: # Read sound level sound_level = cp.sound_level # Debugging: Print sound level to the serial monitor print("Sound Level:", sound_level) # Check if sound threshold is reached if sound_level > SOUND_THRESHOLD: # Sequentially light up NeoPixels on the face for i in range(NUM_PIXELS_FACE): if i < len(pixels_face): pixels_face[i] = COLOR time.sleep(SPEED) # Adjust speed if needed pixels_face.show() # Show all pixels at once # Sequentially light up NeoPixels on strip (A1) for i in range(NUM_PIXELS_STRIP): if i < len(pixels_strip): pixels_strip[i] = COLOR time.sleep(SPEED) # Adjust speed if needed pixels_strip.show() # Show all pixels at once # Delay for the specified duration after lighting up all pixels time.sleep(DELAY_AFTER_LIGHT_UP) # Turn off all pixels on strip pixels_strip.fill((0, 0, 0)) pixels_strip.show() # Turn off all pixels on face pixels_face.fill((0, 0, 0)) pixels_face.show() # Add a delay to avoid rapid detection time.sleep(0.1)
Code Walkthrough
Let's take a look at what's happening in the code.
First we import all the necessary libraries.
import time import board from adafruit_circuitplayground import cp import neopixel
The next section is where you can do all your customization.
-
NUM_PIXELS_STRIP
: The total number of pixels in one strand. -
SOUND_THRESHOLD
: Adjust this number until the sound sensitivity is right for your environment. A higher number means you need to sing louder to trigger the lights. -
DELAY_AFTER_LIGHT_UP
: How long the lights stay on before turning off, in seconds and milliseconds. -
COLOR
: An RGB tuple specifying how much red, green, and blue make up the light color. Choose a number between0
-255
for (red, green, blue) lights. -
SPEED
: This number represents the delay between turning on one light and turning on the next light. A higher number means more delay, so the animation moves more slowly.
# Constants NUM_PIXELS_STRIP = 20 NUM_PIXELS_FACE = 10 # Number of NeoPixels on the face PIXEL_PIN_STRIP = board.A1 SOUND_THRESHOLD = 50 # Adjust this threshold based on your environment # Variables DELAY_AFTER_LIGHT_UP = 2.0 COLOR = (255, 100, 0) # Warm yellow color SPEED = 0.3 # Animation speed (adjust as needed, higher number moves more slowly)
Next we initialize the pixels:
# Initialize NeoPixels on face pixels_face = cp.pixels pixels_face.brightness = 0.5 # Initialize NeoPixels on strip (A1) pixels_strip = neopixel.NeoPixel(PIXEL_PIN_STRIP, NUM_PIXELS_STRIP, brightness=0.5)
And finally the main loop, where the action happens. Since most of the controls have been broken out into variables, you shouldn't need to change much here.
# Main loop while True: # Read sound level sound_level = cp.sound_level # Debugging: Print sound level to the serial monitor print("Sound Level:", sound_level) # Check if sound threshold is reached if sound_level > SOUND_THRESHOLD: # Sequentially light up NeoPixels on the face for i in range(NUM_PIXELS_FACE): if i < len(pixels_face): pixels_face[i] = COLOR time.sleep(SPEED) # Adjust speed if needed pixels_face.show() # Show all pixels at once # Sequentially light up NeoPixels on strip (A1) for i in range(NUM_PIXELS_STRIP): if i < len(pixels_strip): pixels_strip[i] = COLOR time.sleep(SPEED) # Adjust speed if needed pixels_strip.show() # Show all pixels at once # Delay for the specified duration after lighting up all pixels time.sleep(DELAY_AFTER_LIGHT_UP) # Turn off all pixels on strip pixels_strip.fill((0, 0, 0)) pixels_strip.show() # Turn off all pixels on face pixels_face.fill((0, 0, 0)) pixels_face.show() # Add a delay to avoid rapid detection time.sleep(0.1)
Troubleshooting
If your code doesn't run, here are a few things to try:
- Be sure your CIRCUITPY drive looks just like the image above, with the library files inside the /lib folder and the code.py file at the root of the drive.
- Some older Circuit Playground Bluefruit boards may need their bootloader upgraded before the newest version of CircuitPython will run on them. Here's a guide showing how to update the bootloader.
- If the lights on the face of the Circuit Playground come on, but your LED dot strand doesn't, you may have it hooked up incorrectly. These strands are tricky in that they really don't have any markings at all. If you followed the wiring diagram but aren't getting lights, try hooking up to the other end of your strand, or experiment with different arrangements using alligator clips.
Text editor powered by tinymce.