Addressable LEDs

An addressable LED strip has a small controller chip for each LED, or for each small group of LEDs. The chips are connected in a chain, so you can set the color of each “pixel” in a chain of hundreds with one or two GPIO pins. There are many types: 5 V and 12 V strips, RGB and RGBW, and chips with one data wire or with separate data and clock wires. Refer to Types of addressable LEDs.

This page uses the WS2812B (Adafruit calls it the “NeoPixel”) as its main example, because the makerspace uses it most. Each WS2812B has a red LED, a green LED, a blue LED and the controller chip in one 5 × 5 mm (“5050”) package. The ESP32 sends a stream of color data into the DIN of the first pixel. Each pixel keeps the first 24 bits that it receives and sends all the bits after these from its DOUT to the next pixel. Most other one-wire chips operate the same way.

One WS2812B addressable LED in a 5 by 5 mm package, standing on a US quarter for scale
One WS2812B pixel (5 × 5 mm) on a US quarter. Each pixel has its own controller chip.
Photo: SparkFun Electronics, CC BY 2.0

Virtual WS2812B strip

Click a pixel to select it. Select a color. Then push strip.show() to send the data.

Bitstream for pixel 0

The pixel with the blue box in the strip above. Click a different pixel to see its 24 bits.

How the WS2812B protocol works

The SK6812, WS2811, WS2813 and WS2815 use the same one-wire method, with small differences in timing, bit count and color order. Clock-and-data chips (APA102, SK9822) use a different method. Refer to the last card.

One wire, bits coded by time

There is no clock wire. Each bit is a ~1.25 µs slot (800 kHz). Each slot always starts with a HIGH pulse. The length of the HIGH pulse gives the value:

  • 0: ≈ 0.4 µs HIGH, then ≈ 0.85 µs LOW
  • 1: ≈ 0.8 µs HIGH, then ≈ 0.45 µs LOW

The datasheet tolerance is about ±150 ns, so digitalWrite() is too slow to make these pulses.

24 bits per pixel, GRB order

Each pixel needs 8 bits for green, 8 bits for red and 8 bits for blue. The ESP32 sends them most-significant bit first in the order G-R-B, so the libraries ask for a color order, for example NEO_GRB or GRB. RGBW parts, for example the SK6812, use 32 bits (G-R-B-W).

Send the data along, then latch

The first pixel keeps the first 24 bits. It makes the shape of the remaining bits correct again and sends them to the next pixel. The second pixel keeps the next 24 bits, and the sequence continues. If the line stays LOW for sufficient time (≥ 50 µs on early parts, ~280 µs on newer WS2812B), all pixels latch their stored colors at the same time. The show() function causes this latch.

The ESP32 uses its RMT peripheral

The RMT (remote control) peripheral of the ESP32 is designed for IR remote controls. It can output accurate pulse trains in hardware, so the CPU (and Wi-Fi) do not change the timing. Adafruit_NeoPixel and FastLED both use the RMT on the ESP32.

Speed limits

Each pixel needs 30 µs, so 100 pixels need 3 ms, and 1000 pixels need 30 ms (a maximum of ≈ 33 updates per second). Long strips update more slowly. If you need more speed, divide the strip into sections on different pins.

Clock-and-data chips (APA102, SK9822)

These chips (Adafruit calls them “DotStar”) have a data wire and a clock wire, the same as SPI. Each bit is read on a clock edge, so the timing is not critical. You can send data at several MHz, and any two GPIO pins can operate them. Each pixel uses 32 bits: a 5-bit global brightness and 8 bits each for blue, green and red. Their LED PWM is much faster, so they show less flicker on cameras and in persistence-of-vision (POV) projects.

Types of addressable LEDs

Before you buy or connect a strip, find its chip type, supply voltage and color order. The seller page or the label on the reel usually gives them. The power calculator below has a profile for each of these types.

ChipSupplyData wiresData per pixelLEDs per chipNotes
WS2812B (NeoPixel)5 V1 (800 kHz)24 bits, GRB1 RGB LED The makerspace standard. Very common and low cost. Library: NEO_GRB + NEO_KHZ800 or FastLED WS2812B.
SK68125 V1 (800 kHz)24 bits (RGB) or 32 bits (GRBW)1 RGB or RGBW LED Compatible with the WS2812B. The RGBW version has a white LED that gives a better white. Use NEO_GRBW for RGBW.
WS28135 V1 + backup (BIN)24 bits, GRB1 RGB LED Has a second “backup” data input. If one pixel fails, the next pixels still get data. Use the WS2812B settings.
WS281512 V1 + backup (BIN)24 bits, GRB1 RGB LED The 12 V version of the WS2813. Each pixel is addressable. Less voltage drop on long runs. Use the WS2812B settings.
WS281112 V (strips) or 5 V (strings)1 (800 kHz)24 bits, order varies (RGB, BRG…)3 LEDs on 12 V strips A separate driver chip. On 12 V strips, one chip controls a segment of 3 LEDs, so the 3 LEDs always show the same color.
APA102 / SK9822 (DotStar)5 V2 (data + clock)32 bits: brightness + BGR1 RGB LED Fast data rate and fast PWM. Higher cost. Library: Adafruit_DotStar or FastLED APA102 / SK9822.

The data input of 12 V chips still uses 5 V logic levels, so a level shifter is still a good idea. The color order on low-cost strips is frequently different from the datasheet. If red shows as green or blue, change the color order in your code.

Wiring

Addressable strips need a stable supply at the correct voltage (5 V for the WS2812B, 12 V for WS2811 and WS2815 strips), a protected data line and a common ground. The data moves in one direction only. Look at the arrows on the strip, and connect to the DIN end. Clock-and-data strips (APA102) have one more wire: connect CI (clock in) to a second GPIO pin.

5 V power supply sized for the strip (refer to calculator) ESP32 GPIO 16 VIN / 5V GND 74AHCT125 3.3 V → 5 V 330 Ω LED strip +5V DIN GND DOUT → 1000 µF across +5V/GND at the strip input all grounds connected 5 V → ESP32 VIN only when not on USB*
*Many DevKits have a diode between USB 5 V and VIN, but some do not. During development, the easiest method is to supply the ESP32 from USB and the strip from its own supply. Connect the grounds together.
This diagram shows a 5 V WS2812B strip. For a 12 V strip, connect the 12 V supply to the strip only. Use a 12 V → 5 V buck converter for the ESP32 VIN and for the level shifter. Do not connect 12 V to the ESP32.

Connections

Strip padTypical wireConnect to
+5V / +12V / VCCRedStrip supply + (at the 1000 µF capacitor)
DINGreen (frequently)Level shifter output → 330 Ω → DIN
GNDWhite / blackSupply − and ESP32 GND
BIN(WS2813 / WS2815)Backup data input. Connect to GND at the first pixel (refer to the strip datasheet).
CI / CKI(APA102 / SK9822)Clock input. Level shifter output → second GPIO pin.

The wire colors are different for each manufacturer. Always read the labels on the strip pads.

3.3 V data and the WS2812B

A WS2812B with a 5 V supply reads HIGH above about 0.7 × 5 V = 3.5 V. The ESP32 outputs only 3.3 V. This is less than the specification. It frequently works on the bench, but it can fail with longer wires or at higher temperatures. Use one of these reliable solutions:

  • A 74AHCT125 or 74HCT245 level shifter with a 5 V supply (the “T” input accepts 3.3 V logic).
  • A “sacrificial pixel”: one pixel near the ESP32, with a ~4.3 V supply through a diode. This pixel then outputs a full-strength signal.
  • If you do not use a level shifter, keep the data wire short (< 30 cm).

Power: the strip needs more current than you think

General rule: ≈ 20 mA for each color channel, so ≈ 60 mA per pixel at full white. Real parts are different, and many use less. For large builds, measure the current with a multimeter.

Maximum current
3.6 A
Estimated current
0.45 A
Power at 5 V
2.3 W
Wire gauge (short runs)
22 AWG
Recommended supply
5 V, 2 A

Power injection

The thin copper tracks of the strip cause a voltage drop along their length. On a 5 V strip, after ~50–100 pixels at high brightness, the far end becomes orange or brown, and white becomes yellow. To correct this, connect +V and GND to both ends. For long strips, also connect them at each 50–100 pixels. Connect the data line at the start only.

A 12 V strip uses less current for the same light, so the voltage drop is smaller. Usually one injection point each ~5 m is sufficient.

Limit the current in software

setBrightness() and the FastLED function setMaxPowerInVoltsAndMilliamps(5, 2000) limit the current (use 12 for a 12 V strip), so a small supply is not damaged by a full-white frame. This is an easy and low-cost protection.

Safety with large supplies

Do not use thin wires or breadboards with a large supply. A 5 V 10 A supply can make thin wires too hot and melt breadboards. Use screw terminals and wire of the correct size. Install a fuse on large builds. Do not work on mains-powered “brick” supplies when the cover is removed.

Your first build, step by step

  1. Find DIN. Find the arrows on the strip. Solder (or clip) wires to the end that is opposite to the arrow direction.
  2. Remove power. Connect the grounds first. Connect the strip GND, the supply GND and the ESP32 GND together.
  3. Add the capacitor. Connect a 1000 µF capacitor across +V/GND at the strip input. Use a 6.3 V or higher rating for 5 V strips, and 16 V or higher for 12 V strips. The stripe shows the negative leg.
  4. Data: GPIO 16 → level shifter → 330 Ω → DIN. Do not use strapping pins (0, 2, 5, 12, 15).
  5. Start with low brightness. Upload the example below with setBrightness(50). Then increase the brightness.
  6. Apply power. Connect the strip supply. Then connect the USB cable. Disconnect the power before you change the wires.

Troubleshooting

  • No LEDs come on: The wire is on the wrong end (DOUT, not DIN), there is no common ground, or the code has the wrong pin.
  • Wrong colors (red shows as green): The color order is wrong. Try NEO_RGB and NEO_GRB.
  • Random flicker / sparkles: The cause is weak 3.3 V data, a missing resistor, a long data wire or a weak supply. Add a level shifter.
  • Only the first N pixels operate: One pixel or a solder joint is damaged. All pixels after a defective pixel stay off.
  • ESP32 resets on bright frames: The strip and the ESP32 use the same weak supply. Use separate supplies and limit the brightness.

Code

Adafruit_NeoPixel

Library Manager → Adafruit NeoPixel.

neopixel_rainbow.ino


              #include <Adafruit_NeoPixel.h>

              #define LED_PIN   16
              #define NUM_LEDS  30

              // NEO_GRB = color order, NEO_KHZ800 = WS2812B timing
              Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

              void setup() {
                strip.begin();             // prepare the pin (uses RMT on ESP32)
                strip.setBrightness(50);   // 0–255, keep it low during tests
                strip.show();              // send "all off"
              }

              void loop() {
                // Set a single pixel: index, red, green, blue
                strip.setPixelColor(0, 255, 0, 80);

                // Rainbow on the remaining pixels (hue is 0..65535)
                for (int i = 1; i < NUM_LEDS; i++) {
                  uint16_t hue = (millis() * 20 + i * 65536L / NUM_LEDS) & 0xFFFF;
                  strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue)));
                }
                strip.show();              // the LEDs do not change until show()
                delay(20);
              }
            

FastLED

Library Manager → FastLED. It has good color functions and a built-in power limit.

fastled_rainbow.ino


              #include <FastLED.h>

              #define DATA_PIN  16
              #define NUM_LEDS  30
              CRGB leds[NUM_LEDS];

              void setup() {
                FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
                FastLED.setBrightness(50);
                FastLED.setMaxPowerInVoltsAndMilliamps(5, 2000);  // do not use more than ~2 A
              }

              void loop() {
                fill_rainbow(leds, NUM_LEDS, millis() / 10, 7);   // start hue, hue step
                leds[0] = CRGB::White;
                FastLED.show();
                delay(20);
              }
            
Other LED types: Change only the setup line.
  • SK6812 RGBW: Adafruit_NeoPixel strip(N, PIN, NEO_GRBW + NEO_KHZ800);
  • WS2813 / WS2815: use the WS2812B settings above.
  • WS2811 strip: FastLED.addLeds<WS2811, DATA_PIN, BRG>(leds, N); (the color order varies; N = number of chips)
  • APA102: FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, N);
Non-blocking animations: If you also use Wi-Fi or buttons, do not use long delay() loops. Read millis() and update one animation frame each time the correct interval is complete. On boards with one built-in RGB LED (for example, the ESP32-S3 DevKit), Arduino-ESP32 3.x has rgbLedWrite(pin, r, g, b). You do not need a library.

Resources