CircuitPython Test

It's easy to use the Pixie LEDs with CircuitPython and the Adafruit CircuitPython Pixie module. You can light up each LED differently, light them all up the same, or even light up a rainbow!

CircuitPython Microcontroller Wiring

First wire up the Pixie LEDs as shown in Assembly.

Pixie LEDs use UART to communicate. They only receive data. So you're only connecting two wires to your micrcontroller board. To wire them up, connect the following:

  • Connect the shared ground wire to ground on your board.
  • Connect the data wire to TX on your board.

Here is an example with a Feather M0 Express.

CircuitPython Installation of Pixie Library

You'll need to install the Adafruit CircuitPython Pixie library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--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 the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_pixie.mpy
  • adafruit_bus_device

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

CircuitPython Usage

First we import the libraries we'll be using: time, board, adafruit_pixie and busio. Then, we create the uart and pixies objects. We create the variable num_pixies and assign it the number of Pixie LEDs in our chain. We have 2 Pixie LEDs, so we've assigned it 2.

Download: file
import time
import board
import adafruit_pixie
import busio

uart = busio.UART(board.TX, rx=None, baudrate=115200)

num_pixies = 2  # Change this to the number of Pixie LEDs you have.
pixies = adafruit_pixie.Pixie(uart, num_pixies, brightness=0.2, auto_write=False)

Our example includes rainbows, so we have our wheel helper function to call when creating the rainbow.

Download: file
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return 0, 0, 0
    if pos < 85:
        return int(255 - pos * 3), int(pos * 3), 0
    if pos < 170:
        pos -= 85
        return 0, int(255 - pos * 3), int(pos * 3)
    pos -= 170
    return int(pos * 3), 0, int(255 - (pos * 3))

Since auto_write = False in the pixies object, we'll be calling pixies.show() after each LED change in our main loop to update the LEDs.

Our loop starts with a rainbow for 2 seconds. Then we light up the first Pixie LED blue and the second one green for one second. Then we light up all the Pixie LEDs red for one second. Then we use slicing to light up the first Pixie LED magenta and the second Pixie LED cyan for one second. Then the loop begins again.

Download: file
while True:
    for i in range(255):
        for pixie in range(num_pixies):
            pixies[pixie] = wheel(i)
        pixies.show()
    time.sleep(2)
    pixies[0] = (0, 255, 0)
    pixies[1] = (0, 0, 255)
    pixies.show()
    time.sleep(1)
    pixies.fill((255, 0, 0))
    pixies.show()
    time.sleep(1)
    pixies[::2] = [(255, 0, 100)] * (2 // 2)
    pixies[1::2] = [(0, 255, 255)] * (2 // 2)
    pixies.show()
    time.sleep(1)

There's all kinds of things you can do with your Pixies now. You can change the colors, change the time each LED stays a given color or even animate patterns. You can add more Pixies to your chain, but be sure to update num_pixies at the beginning of the code. Have fun with it!

Full Example Code

import time
import board
import busio
import adafruit_pixie

uart = busio.UART(board.TX, rx=None, baudrate=115200)

num_pixies = 2  # Change this to the number of Pixie LEDs you have.
pixies = adafruit_pixie.Pixie(uart, num_pixies, brightness=0.2, auto_write=False)


def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return 0, 0, 0
    if pos < 85:
        return int(255 - pos * 3), int(pos * 3), 0
    if pos < 170:
        pos -= 85
        return 0, int(255 - pos * 3), int(pos * 3)
    pos -= 170
    return int(pos * 3), 0, int(255 - (pos * 3))


while True:
    for i in range(255):
        for pixie in range(num_pixies):
            pixies[pixie] = wheel(i)
        pixies.show()
    time.sleep(2)
    pixies[0] = (0, 255, 0)
    pixies[1] = (0, 0, 255)
    pixies.show()
    time.sleep(1)
    pixies.fill((255, 0, 0))
    pixies.show()
    time.sleep(1)
    pixies[::2] = [(255, 0, 100)] * (2 // 2)
    pixies[1::2] = [(0, 255, 255)] * (2 // 2)
    pixies.show()
    time.sleep(1)
This guide was first published on Oct 20, 2015. It was last updated on Oct 20, 2015. This page (CircuitPython Test) was last updated on Aug 23, 2019.