An LED strip where each LED is a different colour, with animations, all on one data wire. That's the WS2812B (or NeoPixel). One of the most enjoyable projects, and one of the projects most likely to burn out a power supply if the calculations are wrong.
How does it work?
Each LED contains a small chip + 3 LEDs (red, green and blue). The microcontroller sends a sequence of colours:
- The first LED takes the first colour and passes the rest on to the next, and so on.
- Only 3 wires: 5V, GND and DIN.
- Watch for the direction arrow on the strip, and start from the DIN end.
Each colour takes 24 bits (8 per colour, so 0 to 255), and the signal runs at 800 kilobits per second with very precise timing. That's why the library disables interrupts during sending, which can affect other things that depend on timing (such as receiving an IR remote).
Wiring
The most important calculation: current
Each LED can draw up to about 60 mA at white on full brightness.
| Strip | Approximate maximum current |
|---|---|
| 30 LEDs | 1.8 A |
| 60 LEDs (one metre) | 3.6 A |
| 300 LEDs (5 metres) | 18 A! |
- Don't run more than a few LEDs from the 5V pin on the Arduino or from USB.
- A separate 5 V power supply of suitable capacity. See choosing a power supply.
- Limit the brightness in code (for example 30–50%). The difference to the eye is small, and the difference in current is large.
- Ground is shared between the supply and the microcontroller. See ground.
An example: choosing the supply
A 120-LED strip (2 m at 60 LEDs/m):
- The worst case: 120 × 60 mA = 7.2 A (full white, which will rarely happen).
- Brightness at 50% in code → about 3.6 A maximum.
- Add a 20–30% margin → a 5 V 5 A supply.
And if the effects are changing colours rather than white, the actual consumption is usually a quarter or a third of the maximum. Work it out quickly with the WS2812 power calculator. And FastLED has setMaxPowerInVoltsAndMilliamps(5, 3000), which lowers the brightness automatically if the current would exceed the limit.
3 things that protect the strip
- A large capacitor (about 1000 µF, 6.3 V or more) across the supply terminals at the start of the strip.
- A 300–500 Ω resistor on the data wire close to the first LED.
- Connect ground before power, and don't connect the data while the strip is unpowered.
Why? At the moment of connection, the supply can produce a voltage spike, and the capacitor absorbs it. And the resistor protects the first LED from sharp edges and from current if the strip is unpowered while data is running. The first LED is the most exposed to damage, and when it fails the whole strip stops.
A long strip: colours fade at the end
The strip's thin copper causes a voltage drop, so the end of the strip turns yellow or dim. The fix: feed power at several points (every metre or two) with thick wires.
Not the data: DIN is connected only once, at the start. The extra feed points are 5V and GND only, and the data travels through the strip by itself. And if you feed from more than one power supply, connect their grounds together, but don't connect their positives together.
With an ESP32 (3.3 V)
A strip on 5 V may not reliably understand a 3.3 V signal. Sometimes it works and sometimes it flickers randomly. The fix is a fast level shifter chip such as the 74AHCT125.
An alternative trick: power only the first LED from 5 V through a diode (so it gets about 4.3 V), so 3.3 V is enough for it, and it re-transmits a clean 5 V signal to the rest. It often works, but a level shifter is more reliable.
The code (FastLED)
#include <FastLED.h>
#define NUM 60
CRGB leds[NUM];
void setup() {
FastLED.addLeds<WS2812B, 6, GRB>(leds, NUM);
FastLED.setBrightness(80);
}
void loop() {
static uint8_t hue = 0;
fill_rainbow(leds, NUM, hue++, 7);
FastLED.show();
delay(20);
}
Or the Adafruit NeoPixel library, which is a little simpler to start with.
A "running dot" effect
void loop() {
static int pos = 0;
fadeToBlackBy(leds, NUM, 40); // all the LEDs fade a little
leds[pos] = CRGB::Cyan; // one LED lights up
FastLED.show();
pos = (pos + 1) % NUM;
delay(30);
}
fadeToBlackBy makes a tail behind the dot. Change the colour and speed as you like.
GRB or RGB? Most WS2812B strips use GRB order. If you ask for red and get green, change the order in addLeds.
Well-known faults
| Symptom | Usual cause |
|---|---|
| Not a single LED lights | The data enters at the DOUT end (wrong direction) or the ground isn't shared |
| The first LED is faulty and the rest are off | The first LED burnt out; cut it off and connect to the second |
| Random colours and flicker | A 3.3 V signal, a long data wire, or no resistor |
| The colours are dim at the end of the strip | Voltage drop; feed from several points |
| Red comes out green | The GRB/RGB colour order is wrong |
| The Arduino resets on white | The supply is weak, or the strip is powered from the Arduino |
FAQ
What's the difference between the WS2812B, WS2811 and SK6812? The WS2811 is a separate chip that drives 12 V LEDs (one chip per 3 LEDs), so control is less fine-grained but the drop is smaller. The SK6812 is similar to and compatible with the WS2812, and comes in an RGBW version with a real white LED.
How long can the data wire be? Keep it as short as possible, under a metre if you can. If the distance is longer, put the microcontroller next to the strip, or use a level shifter close to the strip and a resistor on the data.