Python & CircuitPython

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

You can use the pixels with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up the Pixie LEDs as shown in Assembly.

Pixie LEDs use UART (serial) to communicate. They only receive data. So you're only connecting two wires to your microcontroller board.

Here is an example with a Feather M0 Express:

  • Feather TX to Pixie data in
  • Feather Gnd to Pixie shared ground

Pixies require an external power source and sufficient gauge power wire! Follow the previous pages in this guide to ensure you have wired them properly.

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with UART:

  • Pi TX to Pixie data in
  • Pi Gnd to Pixie shared ground

Pixies require an external power source and sufficient gauge power wire! Follow the previous pages in this guide to ensure you have wired them properly.

Here's the Pixie LEDs wired up to a USB to serial converter:

  • USB serial TX to Pixie data in
  • USB serial ground to Pixie shared ground

Pixies require an external power source and sufficient gauge power wire! Follow the previous pages in this guide to ensure you have wired them properly.

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.

Copy the necessary libraries from the bundle to the lib folder on your CIRCUITPY drive:

  • adafruit_pixie.mpy
  • adafruit_bus_device

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

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Single Board Computer Python Installation of the Pixie Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-pixie

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate usage, we'll initialise the LEDs and then change the color, brightness and more using the board's Python REPL.

For CircuitPython, run the following code to import the necessary modules and initialise the UART object:

Download: file
import board
import adafruit_pixie
import busio
uart = busio.UART(board.TX, rx=None, baudrate=115200)

For use with Linux and Python, ensure you have UART properly enabled on your computer. This guide shows how to do this on Raspberry Pi. Once enabled, run the following code to import the necessary modules and initialise the UART object:

Note that on single board computers other than Raspberry Pi, the serial port may not be available or coded to be the console. Check the board documentation for serial port use.
Download: file
import time
import adafruit_pixie
import serial
uart = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=3000)

Run the following to set the number of Pixies to 2, and initialise the pixies object. If you have more or less than two Pixies connected, change num_pixies to match. Note that we set the brightness to 0.2 - Pixie LEDs are super bright! Even at 20% brightness, you may want to point them away from you or place a diffuser over them.

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

Now you're ready to change your LEDs using any of the following:

  • fill - Color all Pixies a given color.
  • brightness - Change the brightness of the LEDs by setting to a number between 0 and 1 representing a percentage from 0 to 100%.
  • show - Update the LED colors if auto_write is set to False.
Download: file
pixies[0] = (0, 255, 0)
pixies[0] = (0, 0, 0)
pixies[1] = (0, 0, 255)
pixies[1] = (0, 0, 0)
pixies.fill((255, 0, 0))
pixies.fill((0, 0, 0))

This code lights up the first Pixie LED blue and turns it off, then lights up the second one green and turns it off. Then it lights up all the Pixie LEDs red, and then turns all of them off.

That's all there is to using Pixie LEDs with CircuitPython! You can add more Pixies to your string, create rainbow animations and more. Check out the example below!

Full Example Code

For running the following code on Raspberry Pi or other Linux computer, you must make some changes. Comment out the CircuitPython-specific code, and uncomment the Linux/Python code.
import time
import board
import busio
import adafruit_pixie

# For use with CircuitPython:
uart = busio.UART(board.TX, rx=None, baudrate=115200)

# For use on Raspberry Pi/Linux with Adafruit_Blinka:
# import serial
# uart = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=3000)

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 (Python & CircuitPython) was last updated on Nov 20, 2019.