Addressable LEDs (NeoPixels / WS2812) with Raspberry Pi and Pico
How addressable LED strips work, how to size their power supply, the resistor, capacitor and level shifting they need, and how to drive them from a Pico in MicroPython or from a Raspberry Pi, including the Pi 5.
How they work
"NeoPixel" is Adafruit's name for LEDs built on the WS2812 family of driver chips (also WS2811 and SK6812). Each LED package has red, green and blue LEDs plus a tiny controller, and every one in a chain can be set to its own colour.
A strip has three connections: 5 V, GND and data in (DIN). Your microcontroller sends a stream of colour values down the data wire; the first LED keeps the first colour and passes the rest along, and so on down the chain. Data only flows one way, so connect to the DIN end (strips usually have arrows printed on them). The signal's timing is strict, which is why you use a library rather than toggling a pin yourself.
Power: do the sums first
Each LED can draw up to about 60 mA at full white, with all three colours on full. That's the worst case; mixed colours and animations draw far less, and about 20 mA per LED is a reasonable average to plan around.
| LEDs | Typical (20 mA each) | Worst case (60 mA each) |
|---|---|---|
| 8 | 0.16 A | 0.48 A |
| 30 | 0.6 A | 1.8 A |
| 60 | 1.2 A | 3.6 A |
| 150 | 3 A | 9 A |
Some rules that follow from those numbers:
- A few LEDs at modest brightness can run from a Pico's VBUS pin or a Pi's 5 V pin. Beyond that, give the LEDs their own 5 V supply, rated comfortably above your estimate. A bigger supply is harmless; the LEDs only take what they need.
- Join the grounds. The LED supply's GND must connect to the Pico's or Pi's GND, or the data signal has nothing to be measured against.
- Don't exceed 5 V. A slightly lower voltage just makes the LEDs dimmer, but too much voltage kills them.
- On long runs the voltage sags and the far end looks dim or off-colour. Feed power in at more than one point; for even colour, keep every LED within about a metre of a power connection.
- If you use several supplies, join all the grounds but give each supply its own section of strip. Don't tie their 5 V outputs together.
The resistor and the capacitor
Two cheap parts prevent most problems:
- A 300–500 Ω resistor in series with the data line, fitted at the LED end of the wire. Newer LEDs are less fussy, but it doesn't hurt.
- A 500–1000 µF capacitor (rated 6.3 V or more) across 5 V and GND where power reaches the strip. It absorbs the surge when lots of LEDs change at once. Mind its polarity: the negative lead is usually marked with a stripe.
Keep the data wire from the board to the first LED short, a metre or two at most. If you have to connect to a live supply, connect GND first, then 5 V, then data, and disconnect in reverse. With a separate LED supply, switch the LEDs on before the board.
3.3 V data and level shifting
The Pico and the Raspberry Pi both have 3.3 V logic, but 5 V LEDs officially want a data signal of at least 70% of their supply, which is 3.5 V. A 3.3 V signal often works anyway, especially with short wires, but it isn't guaranteed and can glitch.
The reliable fix is a level shifter between the board and the first LED: a 74AHCT125 or 74HCT245 powered from 5 V turns 3.3 V data into 5 V data. (Powering the LEDs from a single 3.7 V lithium cell also works with 3.3 V data, for small battery projects.)
From a Pico (MicroPython)
MicroPython on the Pico has the neopixel module built in, so there's nothing to install. With the strip's data on GP0:
from machine import Pin
import neopixel
np = neopixel.NeoPixel(Pin(0), 8) # 8 LEDs on GP0
np[0] = (255, 0, 0) # first LED red
np[1] = (0, 64, 0) # second LED dim green
np.write() # send the colours to the strip
np.fill((0, 0, 32)) # every LED dim blue
np.write()
Nothing changes until you call write(). For RGBW LEDs (with a separate white LED), add bpp=4 and use four values per colour. New to MicroPython? See our guide to getting started with MicroPython on Pico.
From a Raspberry Pi 4 or earlier
Adafruit's CircuitPython NeoPixel library works through the rpi_ws281x driver. The data line must go to GPIO10, GPIO12, GPIO18 or GPIO21. In a Python virtual environment:
pip3 install rpi_ws281x adafruit-circuitpython-neopixel
python3 -m pip install --force-reinstall adafruit-blinka
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30) # 30 LEDs on GPIO18
pixels.fill((0, 255, 0))
The driver needs root access. To run as root while keeping your virtual environment, use sudo -E env PATH=$PATH python3 yourscript.py.
From a Raspberry Pi 5
The Pi 5 handles its GPIO through a new chip (RP1), so the older driver above doesn't work. Two approaches do:
The SPI library. adafruit-circuitpython-neopixel-spi generates the LED signal on the SPI bus's MOSI pin (GPIO10). Enable SPI in raspi-config under Interface Options, then:
import board
import neopixel_spi as neopixel
pixels = neopixel.NeoPixel_SPI(board.SPI(), 30)
pixels.fill(0x00FF00)
It's simple and needs no system updates, but it takes over the SPI bus, so you can't use it for anything else.
The Pi 5 PIO library. Adafruit-Blinka-Raspberry-Pi5-Neopixel uses the programmable I/O block inside RP1, works on any GPIO pin and leaves SPI free. It needs recent firmware: if ls -l /dev/pio0 reports no such file, update with sudo apt update, sudo apt upgrade -y and sudo rpi-eeprom-update -a, then reboot. Install it with pip install Adafruit-Blinka-Raspberry-Pi5-Neopixel; its examples, on Adafruit's GitHub, drive LEDs on GPIO13. It was still marked as a pre-release when we checked.
Wrong colours?
Not every strip expects red, green, blue in that order. If red shows as green, set the colour order in your library (pixel_order in the Adafruit libraries, byteorder in the Pi 5 example).
Parts in this guide
Pi Australia
TrinityPixel WS2812B 5050 RGB LED Strip 5M Waterproof
Pi Australia
Little Bird Lorikeet WS2812B Rainbow Board
Pi Australia
Little Bird Macaw WS2812B Rainbow Board 3 LED
Pi Australia
Raspberry Pi 4 x 8 RGB LED Matrix Expansion HAT
Raspberry Pi
Raspberry Pi Pico 2W
Pi Australia