CircuitPython makes using NeoPixels with a QT Py super simple. Simply load the necessary libraries and save the example to your board. This example includes some basic animations to use with a QT Py and NeoPixels, such as blinking, theatre chase, and color wipe. Each animation has some customisations you can do to fit with your project. Let's take a look!

Step 1 - Install CircuitPython

This guide requires CircuitPython be installed, click the button below to learn how to do that and install the latest version of CircuitPython

CircuitPython NeoPixel Library Installation

First make sure you are running the latest version of Adafruit CircuitPython for the QT Py.

You'll need to install the Adafruit CircuitPython NeoPixel library and its dependency on your QT Py.

Next you'll need to install the necessary libraries to use the LEDs -- carefully follow the steps to find and install these libraries from Adafruit's CircuitPython Library Bundle.  Our CircuitPython starter guide has a great page on how to install libraries from the bundle.

You'll want to manually install the following libraries by copying the files to the lib folder on your CIRCUITPY drive:

  • adafruit_pypixelbuf.mpy
  • neopixel.mpy

Before continuing make sure your board's lib folder or root filesystem has the neopixel.mpy, and adafruit_pypixelbuf.mpy files copied over.

Light It Up!

Save the following example to your CIRCUITPY drive as code.py:

"""Basic NeoPixel LED animations for the QT Py."""
import time
import board
import neopixel
import adafruit_pypixelbuf

# Update this to match the pin to which you connected the NeoPixels
pixel_pin = board.A3
# Update this to match the number of NeoPixels connected
num_pixels = 30

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False)
# Set to 0-1 to change the brightness of the NeoPixels
pixels.brightness = 0.2


def blink(color, wait):
    """Blink animation. Blinks all pixels."""
    pixels.fill(color)
    pixels.show()
    time.sleep(wait)
    pixels.fill((0, 0, 0))
    pixels.show()
    time.sleep(wait)


def chase(color, spacing=3, iteration_step=1):
    """Theatre chase animation. Chases across all pixels."""
    if spacing < 2:
        raise ValueError("Spacing must be greater than 1 to show chase pattern.")

    # Use modulo division to create the spacing between pixels.
    chase_pixel = iteration_step % spacing
    # Loop over pixels and turn on expected pixels to provided color.
    for pixel in range(0, len(pixels), spacing):
        # If the pixel is outside the total pixel range, break.
        if pixel + chase_pixel > len(pixels) - 1:
            break
        pixels[pixel + chase_pixel] = color
    pixels.show()

    # Loop over pixels and turn off expected pixels.
    for pixel in range(0, len(pixels), spacing):
        # If the pixel is outside the total pixel range, break.
        if pixel + chase_pixel > len(pixels) - 1:
            break
        pixels[pixel + chase_pixel] = (0, 0, 0)


def color_wipe(color, wait):
    """Color wipe animation. Wipes across all pixels."""
    for pixel in range(num_pixels):
        pixels[pixel] = color
        time.sleep(wait)
        pixels.show()
    time.sleep(0.5)


def rainbow_cycle(wait):
    """Rainbow cycle animation. Cycles across all pixels."""
    for color_index in range(255):
        for pixel in range(num_pixels):
            pixel_index = (pixel * 256 // num_pixels) + color_index
            pixels[pixel] = adafruit_pypixelbuf.colorwheel(pixel_index & 255)
        pixels.show()
        time.sleep(wait)


RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)

while True:
    # Blink 5 times. Increase or decrease the range for more or less blinking.
    for blinks in range(5):
        blink(RED, 0.5)  # Increase number to slow down blinking, decrease to speed up.

    # Chase. Increase or decrease the range for longer or shorter chase animation.
    for step in range(50):
        chase(PURPLE, spacing=4, iteration_step=step)
        time.sleep(0.05)

    # Fill all pixels.
    pixels.fill(RED)
    pixels.show()
    # Increase or decrease the time to change the speed of the solid color change in seconds.
    time.sleep(0.5)
    pixels.fill(GREEN)
    pixels.show()
    time.sleep(0.5)
    pixels.fill(BLUE)
    pixels.show()
    time.sleep(0.5)

    # Color wipe.
    color_wipe(YELLOW, 0.01)  # Increase the number to slow down the color chase.
    color_wipe(CYAN, 0.01)
    color_wipe(PURPLE, 0.01)

    # Rainbow cycle.
    rainbow_cycle(0)  # Increase the number to slow down the rainbow.

Your LEDs should begin blinking red! The blinking will be followed by a purple chase animation, three solid colors (red, green and blue), three color wipes (yellow, cyan and purple), and a rainbow cycle. Each of the animations is simple to use. Let's take a look!

Blink

The blink animation turns on the LEDs for a specified period of time, and turns them off for the same amount of time. To use blink, specify a color, and the wait time interval you'd like to use.

Download: file
[...]
    for blinks in range(5):
        blink(RED, 0.5)

If you use blink() on its own, it will blink only one time. So, the example includes code to blink it for a a specified number of times - in this case 5 times. If you want to increase or decrease the number of blinks, change 5 on the line for blinks in range(5): to a larger or smaller number.

To change the blinking itself, you have the option to specify a color and a time interval. For example, try changing blink(red, 0.5) to blink(blue, 1). Slower blue blinking!

Chase

The chase animation lights up a single LED spaced every x-number of pixels and creates a theatre chase animation. To use chase, specify a color, spacing, and iteration step.

Download: file
[...]
    for step in range(50):
        chase(PURPLE, spacing=4, iteration_step=step)
        time.sleep(0.05)

For chase to work, the iteration_step should be the step in a range. Increase the number in range(number) to increase the length of time the chase animation runs, decrease it to shorten the time it runs.

To change the chase itself, you have the option to specify a color and the spacing. For example, try changing chase(PURPLE, spacing=4, iteration_step=step) to chase(GREEN, spacing=2, iteration_step=step). Tighter green chase!

If you use chase() without a time.sleep(), the animation runs very quickly. So, there is a time.sleep(0.05) included to slow it down a bit. You can increase this number to slow down the chase animation further, or set it to 0 for super fast chasing!

Color Wipe

The color wipe fills the LEDs a specified color beginning with the first pixel and wiping to the last pixel. To use color wipe, specify a color and wait to determine speed.

Download: file
[...]
    color_wipe(YELLOW, 0.01)

Color wipe is used standalone. To change it, you have the option to specify a color and the speed. For example, try changing color_wipe(YELLOW, 0.01) to color_wipe(BLUE, 0.2). Slower blue color wipe!

Rainbow Cycle

The rainbow cycle spreads a rainbow across all pixels and then cycles it across all pixels for once. To use, specify a wait time to determine the speed.

Download: file
[...]
    rainbow_cycle(0)

The rainbow cycle is used standalone. To change it, you have the option to specify the speed. For example, try changing the rainbow_cycle(0) to rainbow_cycle(0.2). Slower rainbow cycle!

To cycle the rainbow more than once, you can use range in the same way you did with blink. Add the following code to cycle the rainbow three times. Change 3 to any number to cycle that number of times.

Download: file
[...]
    for cycles in range(3):
        rainbow_cycle(0)

That's all there is to using basic animations with QT Py and NeoPixels!

This guide was first published on Oct 14, 2020. It was last updated on 2020-10-14 13:46:45 -0400.
This page (Basic Animations for QT Py) was last updated on Oct 24, 2020.