Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Raspberry Pi controlled PixelStrip / NeoPixels

Add multicolour RGB lighting effects indoors and outdoors

PixelStrip / NeoPixel RGB colour LED strip for discos and parties

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.

Voltage Level Shifter for NeoPixels

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:

MOSFET level shifter circuit for a Raspberry Pi to control NeoPixels / RGB PixelStrips

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.

Creating the Electronic Circuit

In addition the Raspberry Pi the hardware components used are as follows:

  • RL - 2.2kΩ (Red, Red, Red, Gold)
  • RB - 470 Ω (Yellow, Violet, Brown, Gold)
  • Q1 - 2N70000
  • 2 x Breadboard NeoPixel modules

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.

MOSFET level shifter circuit created on a breadboard for a Raspberry Pi to control NeoPixels / RGB PixelStrips

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.

Disabling the Raspberry Pi Audio Driver bcm2835

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.

Installing the Python Library

To install the Python libraries open a command window and enter sudo pip3

install rpi_ws281x

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.



#!/usr/bin/python3

from rpi_ws281x import PixelStrip, Color

import time



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)



strip.begin()



while True:

    # First LED white

    strip.setPixelColor(0, Color(255,255,255))

    strip.setPixelColor(1, Color(0,0,0))

    strip.show()

    time.sleep(0.5)

    # Second LED white

    strip.setPixelColor(0, Color(0,0,0))

    strip.setPixelColor(1, Color(255,255,255))

    strip.show()

    time.sleep(1)

    # LEDs Green

    strip.setPixelColor(0, Color(255,0,0))

    strip.setPixelColor(1, Color(255,0,0))

    strip.show()

    time.sleep(0.5)

    # LEDs Red

    strip.setPixelColor(0, Color(0,255,0))

    strip.setPixelColor(1, Color(0,255,0))

    strip.show()

    time.sleep(0.5)

    # LEDs Blue

    strip.setPixelColor(0, Color(0,0,255))

    strip.setPixelColor(1, Color(0,0,255))

    strip.show()

    time.sleep(1)

Save this as file neopixel.py

Set executable permission using:

chmod +x neopixel.py

It needs to be run with root permission using sudo

sudo ./neopixel.py

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.

Adding a graphical interface (GUI)

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.

Using a web based interface

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:

Related Projects

Future projects

I'm always working on new projects
To find out about the updates please:
Subscribe to the PenguinTutor YouTube Channel
and
Follow @penguintutor on Twitter

Previous Pico W (Wireless)
Pico W (Wireless)
Next Raspberry Pi Wi-Fi PixelStrips
Raspberry Pi Wi-Fi PixelStrips