How to wire a WS2812 LED strip to an ESP32
To wire a WS2812 LED strip to an ESP32, connect VDD to VIN (5 V from USB), VSS to GND and DIN to GPIO 4 (D4). The strip is a 5 V part and its data line comes from a 3.3 V pin, which works on many strips and flickers on others, so use a level shifter on DIN for anything permanent. The sketch below uses the Adafruit NeoPixel (Adafruit_NeoPixel.h) library. It runs a moving rainbow across eight pixels.
- 3.3 V logic
- Library: Adafruit_NeoPixel.h
- 3 wires
WS2812 LED strip to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| WS2812 LED strip pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VDD (5V) | VIN (5 V from USB) | red | WS2812B pixels are 5 V parts; each can draw up to 60 mA at full white. |
| VSS (GND) | GND | blue | Common ground, or the data line has no reference. |
| DIN | GPIO 4 (D4) | green | The data line into the first pixel. Each pixel keeps 24 bits and passes the rest on. |
Power and logic levels for WS2812 LED strip and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The WS2812 LED strip takes 5 V from VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.
This pair
The strip runs from VIN (5 V from USB) while DIN comes from a 3.3 V pin. A WS2812B wants about 0.7 times its supply, 3.5 V, to see a high, so on a real bench a 3.3 V signal works on some strips and flickers on others. A 74AHCT125 level shifter on DIN fixes it for good. Add a 330 ohm resistor in series with DIN and a 1000 uF capacitor across the supply on any strip longer than a few pixels.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: Adafruit NeoPixel (Adafruit_NeoPixel.h). This is the sketch the circuit above runs.
#include <Adafruit_NeoPixel.h>
const int PIXEL_PIN = 4; // DIN, GPIO 4 (D4)
const int PIXELS = 8;
Adafruit_NeoPixel strip(PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
strip.begin();
strip.setBrightness(40); // of 255: easy on the eyes and on the supply
strip.show(); // all off
Serial.println("Rainbow");
}
void loop() {
static uint16_t hue = 0;
for (int i = 0; i < PIXELS; i++) {
uint16_t h = hue + i * (65536L / PIXELS);
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(h)));
}
strip.show();
hue += 512;
delay(20);
}
What you should see
Eight pixels show a rainbow that slides along the strip, a little further every 20 ms, at low brightness. The serial monitor, at 115200 baud, prints Rainbow once.
If one color is wrong, the pixels are wired in a different order from the sketch: the strip type NEO_GRB covers most WS2812B strips.
The serial monitor prints this, recorded from the circuit above running:
RainbowCommon mistakes wiring a WS2812 LED strip to an ESP32
Forgetting show()
setPixelColor() writes to memory only. Nothing changes on the strip until show() sends the buffer, so a sketch without it leaves every pixel dark.
Wiring the wrong end
Data flows one way. DIN is the input end, marked with an arrow on most strips; wired to DOUT at the far end, nothing lights. In the editor the part’s DIN pin is the one on the left.
Expecting 3.3 V data to always work
It works on the simulated strip and on many real ones. When a real strip flickers or shows random colors on the first pixel, the 3.3 V data line is the first suspect: add a 74AHCT125 level shifter.
Build your own WS2812 LED strip project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.