Python Usage

Python Installation of NeoPixel Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require 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 rpi_ws281x adafruit-circuitpython-neopixel

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!

Python Usage

To demonstrate the usage of this library with NeoPixel LEDs, we'll use the Python REPL.

For NeoPixels to work on Raspberry Pi, you must run the code as root! Root access is required to access the RPi peripherals.

Run the following code to import the necessary modules and initialise a NeoPixel strip with 30 LEDs. Don't forget to change the pin if your NeoPixels are connected to a different pin, and change the number of pixels if you have a different number.

Download: file
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)

Now you're ready to light up your NeoPixel LEDs using the following properties:

  • brightness - The overall brightness of the LED
  • fill - Color all pixels a given color.
  • show - Update the LED colors if auto_write is set to False.

For example, to light up the first NeoPixel red:

Download: file
pixels[0] = (255, 0, 0)

To light up all the NeoPixels green:

Download: file
pixels.fill((0, 255, 0))

That's all there is to getting started with NeoPixel LEDs on Raspberry Pi!

Below is an example program that repeatedly turns all the LEDs red, then green, then blue, and then goes through a single rainbow cycle. If you chose a pin other than D18 for your NeoPixels, change the pin to match the pin you chose to use to connect the NeoPixels to your Raspberry Pi. If you're using a different number of NeoPixels, change num_pixels to match.

Full Example Code

Temporarily unable to load content:
This guide was first published on Sep 12, 2014. It was last updated on Sep 12, 2014. This page (Python Usage) was last updated on Mar 28, 2020.