Neopixels are addressable full-colour RGB LEDs that come in a variety of different arrangements. Ranging from single LEDs, to matrix arrays and a variety of sewable components that can be used on wearable products.
Neopixels were originally made available for Arduino projects, but now there are also Python libraries for Raspberry Pi’s.
In this blog I will be looking at setting up neopixels components on Raspberry Pi’s, and then I will show some “soft” neopixel layouts using the Python Tkinter graphic library.
Getting Started
To load the Raspberry Pi neopixel libary comes from the nice people at Adafruit, and it is loaded by:
sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel
It is important to note that neopixels can draw a lot of power so consider looking at using external 5V power for projects with a lot of LEDs. The LED power consumption is based on:
- How many neopixel LEDs are lit at one time, and
- What the intensity of the LEDs is.
A few other import points are:
- not all neopixel strips are the same. Different strips will vary greatly from the LED intensity, and more importantly on the RGB vs. GRB addressing.
- NeoPixels must be connected to D10, D12, D18 or D21 to work
- For NeoPixels to work on Raspberry Pi, you must run the code as root
The neopixels are wired with 3 pins : 5V (VCC), GND and signal in. The default signal in wires to Pi pin 18. Neopixel component can be connected in series with data in and out connectors.
Below is an example that will set all the LEDs to a light magenta and then it will cycle one LED to a brighter RED. The overall neopixel string has a 10% brightness.
# Python neopixel example to cycle an LEDs import board import neopixel import time ORDER = neopixel.RGB # or neopixel.GRB numpixels = 12 # Create a pixel object with 12 pixels and low intensity pixels = neopixel.NeoPixel(board.D18,numpixels , brightness=0.10, auto_write=True, pixel_order=ORDER) while True: for i in range(numpixels): pixels.fill((10, 0, 40)) # fill all pixels in light magenta pixels[i] = (80,0,0) # fill one pixel in brighter red time.sleep(1)
Depending on the type and manufacturer of the neopixels the result will look a little different. Some trial and error testing will be required to determine if the strips are RGB or GRB.
Simulated Neopixels
If you don’t have neopixels or if what to simulate neopixels then the Python Tkinter graphic library can be used to create a variety of different arrangements. For my testing I create two arrangements: a strip and a matrix.
The important things that I learned was how to create a array object that could simulate the neopixel object. To do this in Python:
import tkinter as tk root = tk.Tk() root.title("Soft NeoPixel Strip") numleds = 25 # Create an array that can be used later in Tkinter ledstrip = ['' for i in range(numleds)] for i in range(numleds): ledstrip[i] = tk.Label(root,relief='raised',width=3 ) # a label array ledstrip[i].grid(row = 0, column = i) # position the labels is a horizontal row root.mainloop()
Simulated Strip Neopixel
Below is an example of a soft “strip” neopixel application with a demo function.
# Python Neopixel Single Strip Presentation # import tkinter as tk numleds = 25 theled = 0 def stringdemo(): # move a coloured LED around the string global theled ledstrip[theled].configure(background= 'white') theled = theled + 1 if theled >= numleds: theled = 0 ledstrip[theled].configure(background= 'sky blue') root.after(500, stringdemo) root = tk.Tk() root.title("Soft NeoPixel Strip") # create an LED object ledstrip = ['' for i in range(numleds)] # put the LED object into a horizontal strip for i in range(numleds): ledstrip[i] = tk.Label(root,width=2,height=1,relief='raised',background = 'white') ledstrip[i].grid(row = 0, column = (i+1)) root.after(500, stringdemo) #start a demo root.mainloop()
Simulated Matrix Neopixels
Below is an example of a soft matrix neopixel application.
# Python Neopixel Matrix Presentation # import tkinter as tk numleds = 100 rowcnt = 10 colcnt = int (numleds/rowcnt) theled = 0 def stringdemo(): # move a coloured LED around the string global theled ledstrip[theled].configure(background= 'dark gray') theled = theled + 1 if theled >= numleds: theled = 0 ledstrip[theled].configure(background= 'red') root.after(500, stringdemo) root = tk.Tk() root.title("Soft NeoPixel Matrix") # create LED object ledstrip = ['' for i in range(numleds)] # put the LED object into a grid for i in range(rowcnt): for j in range(colcnt): ledstrip[theled] = tk.Label(root,width=4,height=2,relief='raised',background = 'dark gray') ledstrip[theled].grid(row = i, column = j) theled = theled + 1 theled = 0 #reset the led index for the demo root.after(500, stringdemo) root.mainloop()
Summary
Neopixels can be used on custom lighting applicatons, for example I used them on a water fountain project.
Given a choice I would recommend using Arduino hardware over the Raspberry Pi hardware for neopixel projects. I found that the Arduino neopixel library to be much more stable and considerably faster than the Pi version.