- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
28 August 2018
I've recently been having fun with some NeoPixels on the Raspberry Pi. NeoPixel is the name that Adafruit uses to refer to "Smart RGB LEDs with WS2811 / WS2812 integrated controllers". I'm sure you'll agree that NeoPixel has a better ring to it and as that's the name of the Python library that I'm using.
NeoPixels are essential three LEDs combined into a single package. One LED is Red, one Green and the other Blue. When the LEDs are turned on together then the LED appears to take the combined colour. For example turn on Red and Blue and you will see purple, or turn on all 3 LEDs and it will appear to be white.
The special thing about the NeoPixels is the WS281x (eg. WS2811 / WS2812) integrated circuit that is included in the same compact package. The integrated circuit allows the LEDs to be individually controlled using a simple serial signal. The LEDs can then be daisy chained together with each LED individually controllable. Note that some manufacturers and suppliers use the IC names interchangably, but it appears that WS2811 refers to the IC without an LED, WS2812 refers to an IC mounted underneath the integrated LEDs. There is also WS2812B which is a newer version of the WS2812.
There are also some older ICs such as the WS2801 which uses a two connection data bus. These have the advantage that timing is less critical (it includes a separate clock signal), but with an extra wire needed. These appear to be less popular with manufacturers so this will mainly concentrate on the WS2812 and WS2812B which are the most common. I will refer to WS2812 and NeoPixels throughout this guide.
More details are available from RGB LED (NeoPixel) page in the Electronics guides.
The information used for this guide is based on the data sheets for the WS2812 and WS2812B. Note that different manufacturers / suppliers may differ and some of my actual measurements different betwen suppliers.
The LEDs require between 3.5V and 5V for the power supply. The higher voltage being required to achieve full brightness of the LEDs so it is assumed most will be looking to run from a 5V power supply. See later for more about suitable power supplies and current requirements for NeoPixels.
The data signal is sent as a serial signal into the Data In of the Smart LED, and after stripping off it's information that is passed out of the Data Out pin where it normally goes to the Data In of the next RGB LED. Each controller uses 24 bits of data which consists of the values for each of the three LEDs (Red, Green and Blue). So if there are two LEDs being controlled the data will be 48 bits long of which the first LED controller will strip off it's 24 bit signal passing another 24 bit signal to the next LED. This allows 8 bits for each colour component which is a value of 0 to 255, where 0 is off and 255 is full on.
The timing of the signal is critical and any interuption in the signal will prevent the message from being understood. With a full operating system such as Linux on the Raspberry Pi then the operating system may use processing time for it's own housekeeping which can disrupt the timing of the signal. This can be overcome using a micro-controller (eg. Arduino) or using the PWM pin of the Raspberry Pi as explained later.
The LED controllers requires an input signal 0.7 x supply voltage for a high signal. In the standard case running the LEDs at 5V then the minimum input for a high is 3.5V. As the Raspberry Pi runs at 3.3V then that is not sufficient to drive the LEDs directly. Some Arduinos that are designed to run at 5V can be used to drive the LEDs without requiring any additional interface.
As the Smart LEDs include an IC controller which is capable of switching the LEDs the signal that needs to be provided along the data signal is minimal (micro Amps).
I had previously heard that the Raspberry Pi had a problem with communicating with the LEDs due to a timing issue and the lack of real time support for writing to the GPIO pins. When I heard about the Kickstarter for MyPiFi.net NeoPixel controller then I quickly backed the project. I was expecting some kind of special integrated circuit on the board that was able to communicate with the NeoPixels (something similar to the AdaFruit FadeCandy), but when it arrived it turned out to be a buffer IC used as a level shifter between the 3.3V of the Raspberry Pi GPIO ports and the 5V signal required for the NeoPixels.
It's when I looked at the software library I realised that a "workaround" has been found to allow the Raspberry Pi to provide the accurate timing signal. The software uses the PWM controller of the Raspberry Pi to generate the signal required rather than trying to use one of the other GPIO ports. That isn't to disparage the add-on board. The board does exactly what it says it will and it provides it in a easy to assemble format that fits nicely on the Raspberry pi. You could quite easily do something similar on a breadboard using a simple transistor circuit or an IC, but that isn't as convenient as the small PCB that is provided.
The initial version of the software library was written by Jeremy Garff WS281x library for Raspberry Pi 1, but it has since been updated to support the Raspberry Pi 2 as well as the existing versions by Richard Hirst Updated WS281x library - includes support for Raspberry Pi 2.
I have created my own software to control the Raspberry Pi using a GUI on the Raspberry Pi, which uses this WS281x library in Python 3.
The following instructions will install the drivers and python software libraries onto the Raspberry Pi. The software I use requires the Python 3 version, although you may want to install both python 2 and python 3 to also support other software.
First install the pre-requisite software required to configure and install the libraries (depending upon how much other software you have installed some of these may already been installed although there is no harm in issuing the command anyway).
sudo apt-get install build-essential python-dev python3-dev git scons swig
Download the software (using git this will download the latest version into a folder called rpi_ws281x)
git clone https://github.com/richardghirst/rpi_ws281x.git
Build the software using
Change to the python directory
Install one or both of the following depending upon the version of Python you require.
sudo python setup.py install
sudo python3 setup.py install
You can now find an example program to test that it is working as
Note that if you are using python 3 then you will need to make a change to the strandtest.py file. On line 85
add brackets () around the string in the print statement.
print ('Press Ctrl-C to quit.')
The Arduino NeoPixel Library is available to download from GitHub:
The library needs to be installed into a folder called "Adafruit_NeoPixel" in the Arduino Library folder. Restart the Arduino IDE and look for the example code in the Library Sketchbook.
The RGB LEDs can be supplied with
The most convenient and safest way to power the LEDs is using a 5v DC "power brick". These are similar to the power supplies used for laptop computers, but you must ensure that you have a 5v output as laptops normally use much higher voltages. The power brick must also be able to supply at least the amount of current (measured in Amps) that the LEDs require to prevent damage.
According to the data sheet for the ws281x LEDs each device can consume up to 60mA of power when in the white full on mode. Whilst this doesn't sound much if you connect to a 5 meter 150 LED strip this would be a total power requirement of 9A, which needs a powerful power supply. Whilst you can buy 10A power supplies the powerbricks are very expensive. An alternative is a metal framed power suppy, but these are normally designed to be integrated into another case and do not offer the same electrical safety protection afforded by a powerbrick.
Using the safe 60mA rating means that you can power a maximum of 66 LEDs from the 4A supply listed above. This would allow for 1 meter high density strip, or 2 x 1m strips with wider spacing.
To see if the power requirements were really as high as stated then I took some of my own readings.
I used three different NeoPixel / RGB LED strips and these are example current draws that I measured (per pixel).
AdaFruit 16 Neopixel Circle
Standby current 2mA
Full on current 51mA
60-way 1m RGB LED strip
Standby current 1mA
Full on current 30mA
150-way 5m RGB LED strip
Standby current 1mA
Full on current 30mA
I also found that when powering all the LEDs on the 5m strip that the current was even lower at around 22mA.
The reason for this is likely to be related to the resistance in the strip which results in a lower voltage supply at the LEDs furthest away from the power source. This was also evident as connecting power to only one end of the strip resulted in those at the furthest end appearing to be more of a light yellow colour compared to the bright white at the end nearest the power supply. To avoid this yellowing then I suggest connecting the 5V supply (but not the data signal) to both ends of the strip, or running the strip at a lower brightness.
These values are the maximum power that I saw on these particular strips. The power can be reduced by reducing the brightness of the LEDs or by not turning all the LEDs on at the same time. I recommend sticking to the 60mA draw unless you are able to measure the actual current requirements for your particular LEDs (including a more powerful power to supply to make sure it's not just the power supply reducing the load due to being overloaded).
To avoid the yellowing I mentioned earlier then for longer strips I suggest that you connect the 5v supply (but not the data) to both ends of the strip.
It is also possible to subdivide the strips and power them from different power supplies. To achieve this the supplies will need to share a common gnd (so that the data signal still has a ground supply), but have isolation between the +5V supply. To achive this you will need to leave the ground (0v) connections connected between both sections of the strips, but break the connection between the +5v supplies.
If you buy a long RGB LED strip then it may come with 3-pin JST connectors at the end. Others (including the AdaFruit NeoPixel range) may just have connection pads where you can solder your own wires on the end. JST connectors are normally used for connecting power supplies or motors within model cars / aircraft (although often different number of pins) and as such are not really designed for frequent plugging / unplugging. Despite this I decided to stick with JST connectors for my setup. The ones I bought had completely different colours to the ones soldered onto the strips. Instead of using the colours it was important to follow which pin connected to which supply and to connect appropriately. Note that there are male and female connectors to allow these to be daisy chained (as long as you remain within the maximum current of the power supply).