- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
This guide is to how you can add NeoPixels or RGB pixelstrips to a Raspberry Pi and display a light sequence. First watch the video below and then move on to the further details.
If you find these kind of videos useful please subscribe to my channel : Click here to subscribe. Please ....
PixelStrips, also known as NeoPixels and RGB LEDs are individually addressable multicoloured LEDs. They are usually controlled by WS2811, WS2812, WS2812B or otherwise referred to as WS281x integrated circuits. In this exercise you will light up two breadboard NeoPixels. These are also available as rings, strings and as a matrix display. The Breadboard NeoPixels are supplied without connecting pins and normally need to have header pins soldered to them first. You can learn about soldering at: guide to soldering electronic circuits.
To drive NeoPixels from a Raspberry Pi then you will need to increase the output voltage from the GPIO port from 3.3V to 5V. This can be achieved using a MOSFET as a voltage level shifter. A MOSFET is a type of transistor, which is switched on by applying an input voltage to the gate (G) which will allow a current to flow between the drain (D) and source (S).
The circuit diagram is shown below:
Here the MOSFET acts as a digital switch, which is turned on when the input is high and off when the input is low. The output is taken from above the MOSFET so when it is switched on then the output goes low and when the MOSFET is switched off then the output is pulled high through resistor RL. This means that the voltage shifter is inverting (you get the opposite at the output compared with the input). This is something you can handle in the software quite easily.
In addition the Raspberry Pi the hardware components used are as follows:
You will also need a breadboard and some jumper wires, using male-to-male for the connections within the breadboard and male-to-female for the connections to the Raspberry Pi.
Wire up the circuit as shown in the diagram below. Note that this uses the power from the Raspberry Pi, which will work when using only one or two Neopixels, but an external supply is required if more NeoPixels are used.
NOTE: The circuit diagram is based on Breadboard NeoPixels from AdaFruit. Other manufacturers use a different pin layout, in which case the wires may need to be connected differently. The input to the NeoPixel may be labelled as Data In, DI or In, the output to the next stage may be labelled Data Out, DO or out. The supply voltage may be labelled as +V, +5V or 5V, the ground connection as -V, 0V, G or Gnd. Please check that you wire the appropriate pins up correctly.
The two NeoPixels are mounted the opposite way around from each other. This is so that the output from the first is close to the input for the second NeoPixel.
Before you get around to installing the software there is an issue when trying to control NeoPixels from a Raspberry Pi where sound is enabled. This is because the hardware PWM feature of the Raspberry Pi has to be manipulated by the software library, but this is also used by the sound driver. The solution is to disable the sound driver, which unfortunately means not being able to use sound. To disable the sound driver create a new blacklist file using sudo nano /etc/modprobe.d/snd-blacklist.conf Add an entry blacklist snd_bcm2835 Then save and exit (Ctrl-O Ctrl-X) Adding The next step is to install the python module (software library) that allows you to control the NeoPixels from the Raspberry Pi.
To install the Python libraries open a command window and enter sudo pip3
The following is a simple program using Python 3. I recommend you use Mu for creating the file (which includes syntax highlighting), but you should run it from the command-line rather than in Mu.
from rpi_ws281x import PixelStrip, Color
LEDCOUNT = 2 # Number of LEDs
GPIOPIN = 18
FREQ = 800000
DMA = 5
INVERT = True # Invert required when using inverting buffer
BRIGHTNESS = 255
# Intialize the library (must be called once before other functions).
strip = PixelStrip(LEDCOUNT, GPIOPIN, FREQ, DMA, INVERT, BRIGHTNESS)
# First LED white
# Second LED white
# LEDs Green
# LEDs Red
# LEDs Blue
Save this as file neopixel.py
Set executable permission using:
chmod +x neopixel.py
It needs to be run with root permission using sudo
In the code the first part of the program sets up the appropriate values for the Raspberry Pi and the NeoPixels, this may need to be updated particularly if there are more NeoPixels than the two we have specified. Note that the INVERT option is set to True which is required due to the inverting nature of the circuit we have created.
One final thing is that for some NeoPixels the red and green are handled in a different order. The above code is based on GRB (Green Red Blue) ordering which is used by the breadboard NeoPixels I used, but many NeoPixels instead a more traditional RGB (Red Green Blue) ordering in which case the colour ordering needs to be changed.
See the link below for details of how to use a graphical user interface (GUI) to control the RGB LED PixelStrip. This has been replaced by the web based version (see below), but is still available if you would like the GUI version instead.
Since creating the GUI version I've now moved on to a web based version. This can still be used locally in a similar way to the previous GUI, but has the advantage of allowing control using a smart phone or using in home automation.
For more details see the link below:
Please view the copyright information regarding use of the circuits.