CircuitPython is a fast growing programming platform based on Python, that's easy to learn and customize. This project makes use of the Circuit Python LED Animations code by Kattni Rembor to quickly and easily add gorgeous animations to your light strand. There are more pre-made animations available than I'm using for this guide, so go check it out if you need more modes on your light strand.

How To Upload the Code

Step 1: Update CircuitPython

  1. Download the latest version of the Circuit Python operating system using the button above.
  2. Plug your Circuit Playground into your computer via its USB port and double-click the "reset" button.  All the lights will turn green and your Circuit Playground will appear as a drive on your computer called CPLAYBOOT
  3. Drag the code you just download onto this drive to install CircuitPython -- like putting files on a USB stick. 

Note: If you plug in the board and you see a drive called CIRCUITPY appear, press the reset button again (double-click) to get to CPLAYBOOT.

Step 2: Install the Required Libraries

  1. Click the button above to download the latest CircuitPython Library Bundle Release. 
  2. Open the file you just downloaded and look in the lib folder. Find these files and copy them into the lib folder on your Circuit Playground's CIRCUITPY drive.
  • adafruit_bus_device
  • adafruit_circuitplayground
  • adafruit_led_animation
  • adafruit_lis3dh.mpy
  • adafruit_thermistor.mpy
  • neopixel.mpy

Step 3: Upload the Code

Copy the code from the code window below and save it as code.py at the root of your CIRCUITPY drive.

Your CIRCUITPY drive should look like this when you're done:

"""
Crystal Gem Light Strand Project By Erin St Blaine for Adafruit Industries
https://learn.adafruit.com/no-solder-papercraft-crystal-light-strand

Circuit Playground Bluetooth with LED strand attached runs 4 different variable animation modes.

Code by Rose Hooper using Adafruit's LED Animation Library:
 https://learn.adafruit.com/circuitpython-led-animations/overview
"""
# pylint: disable=attribute-defined-outside-init

#Import libraries
import board
import neopixel
from rainbowio import colorwheel
from adafruit_circuitplayground import cp
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.animation import Animation
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.sequence import AnimationSequence
from adafruit_led_animation.color import WHITE

speeds = (0.25, 0.125, 0.1, 0.08, 0.05, 0.02, 0.01)  # Customize speed levels here
# periods = (7, 6, 5, 4, 3, 2, 1)

class RainbowFade(Animation):

    _color_index = 0
    def __init__(self, pixel_object, speed, name):
        super().__init__(pixel_object, speed=speed, color=WHITE, name=name)

    def draw(self):
        self.color = colorwheel(self._color_index)
        self._color_index = (self._color_index + 1) % 256
        self.fill(self.color)


current_speed = 4

# Set your number of pixels here
pixel_num = 20

pixel_pin = board.A1
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False)

# Animation Setup

rainbow = Rainbow(pixels, speed=speeds[current_speed], period=2, name="rainbow", step=3)
sparkle = Sparkle(pixels, speed=speeds[current_speed], color=WHITE, name="sparkle")
rainbowfade = RainbowFade(pixels, speed=speeds[current_speed], name="rainbowfade")
solid = Solid(pixels, color=colorwheel(0), name="solid")

# Animation Sequence Playlist -- rearrange to change the order of animations

animations = AnimationSequence(
    rainbow,
    rainbowfade,
    solid,
    sparkle,
    auto_clear=True,
    auto_reset=True,
)

solid.speed = 0.01
solid_color = 0

while True:
    if cp.switch:  # if slide switch is in the "on" position, run animations
        animations.animate()  #play animation sequence
        if cp.button_a:
            animations.next()
            while cp.button_a:
                continue

        if cp.button_b:
            if animations.current_animation.name == "solid":
                solid_color = (solid_color + 8) % 256
                animations.current_animation.color = colorwheel(solid_color)
            else:
                current_speed += 1
                if current_speed >= len(speeds):
                    current_speed = 0
                rainbow.speed = speeds[current_speed]
                sparkle.speed = speeds[current_speed]
                rainbowfade.speed = speeds[current_speed]
                print(speeds[current_speed])
            while cp.button_b:
                continue
    else:  # If slide switch is in the "off" position, set pixels to black
        pixels.fill(0)
        pixels.show()

How It Works

On/off:

The tiny slide switch on the face of the Circuit Playground turns the light strand on or off. Slide right for on, slide left for off. (So, if you just downloaded the code and you don't see anything happening, flip this switch! You might just be in "off" mode.)

Modes

This code has four different modes. Click button A to cycle between them: 

  1. Rainbow: an animated LED rainbow. Who doesn't love rainbows?
  2. Rainbow Fade: the lights are all the same color and fade through the rainbow as a unit
  3. Solid: Pick a color, any color you want!
  4. Twinkle: Lovely glittery white twinkles

Variations

Button B is our variation button. Click it to vary the speed of the rainbow and twinkle animations, and the color (hue) of the solid animation. There are seven speed levels, from fast to slow, and the hue will just keep changing and rotating each time you press the button.

How to Customize Your Code

You can add your own modes, give yourself more speed variations, or set it to start up on your favorite mode. The possibilities are endless! Poke around and change some stuff, and if it stops working you can always come back here and start fresh. Make it Yours.

This code is written for one 20-pixel light strand. Here are a few customization hints.

current_speed = 4

# Set your number of pixels here
pixel_num = 20
pixel_pin = board.A1
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False)

Change current_speed to change the initial startup animation speed level. 

Change pixel_num to match the number of pixels in your strip.

Change brightness=1 to anything between 0.0 and 1.0 to change the overall master brightness of your strip.

Animations

# Animation Setup

rainbow = Rainbow(pixels, speed=speeds[current_speed], period=10, name="rainbow", step=3)
sparkle = Sparkle(pixels, speed=speeds[current_speed], color=WHITE, name="sparkle")
rainbowfade = RainbowFade(pixels, speed=speeds[current_speed], name="rainbowfade")
solid = Solid(pixels, color=colorwheel(0), name="solid")

# Animation Sequence Playlist -- rearrange to change the order of animations

animations = AnimationSequence(
    rainbow,
    rainbowfade,
    solid,
    sparkle,
    auto_clear=True,
    auto_reset=True,
)

You can add more animations or re-order them however you'd like. Check out the CircuitPython LED Animations Guide to see what other animations this library offers. There are several different Rainbow animations, Pulse, Chase, and some Sparkle variations that are a lot of fun to play with. 

You'll need to change the code in three places:

  1. Import the animation at the very top
  2. Create a line for each one under #Animation Setup
  3. Add it to the playlist

This code is very user-friendly and fun to work with and customize. Choose different values in the Animation Setup section to customize how each animation looks.

This guide was first published on Jun 12, 2020. It was last updated on Jun 12, 2020.

This page (Code with CircuitPython) was last updated on Oct 21, 2021.

Text editor powered by tinymce.