The WS2812 (also called NeoPixel) is an RGB LED with its own controller chip in every pixel, so a single data wire drives a whole strip of dozens of pixels, each with a different colour. This project turns the ESP32 into a small web server: open the page on your phone, pick a colour, set the brightness and choose an effect. The hard part is not the code but the power and the signal: get them wrong and the strip flickers, the voltage sag resets the ESP32, or the wires get hot.
Parts
- ESP32 DevKit board
- WS2812B 5 V LED strip (for example 30 LEDs/m, 1 m long)
- A 5 V supply with enough current (see the current budget below), for example a 5 V / 3 A adapter
- A 330–470 Ω resistor (in series with the data wire)
- A 470–1000 µF electrolytic capacitor rated 6.3 V or more (across the strip's power input)
- Optional but recommended: a 74HCT125 or 74AHCT125 logic level shifter (3.3 V to 5 V)
Current budget and choosing a supply
Each WS2812B pixel has three LEDs (red, green, blue) at up to about 20 mA each. At full white one pixel draws roughly 60 mA. Quick formula:
Max current (A) = pixels × 0.06
30 pixels → 1.8 A
60 pixels → 3.6 A
150 pixels → 9 A
In practice colours rarely run at full brightness, so real current is lower, but pick a supply equal to or above the figure, or limit brightness in software (the code below uses setBrightness). On long strips, inject power at the far end too, to avoid voltage drop (a reddish or yellowish tint at the end of the strip is the classic sign).
Note: do not power the whole strip through the ESP32's 5 V pin or a computer USB port beyond a handful of pixels. The board's 5 V/VIN pin is not designed for amps of current.
Wiring
- Powering the ESP32: either use its own USB cable, or feed it from the same 5 V supply into the VIN pin. If both USB and the 5 V supply feed VIN at once, make sure the two sources cannot fight each other; check your board's schematic.
- The 330–470 Ω resistor on DIN damps spikes and protects the first pixel.
- The electrolytic capacitor absorbs the inrush at power-on that can kill the first pixel. Watch the polarity (the longer leg is positive).
- 3.3 V signal: the WS2812B data input needs a high level of at least about 0.7 × VDD, i.e. ~3.5 V on a 5 V supply. The ESP32 outputs only 3.3 V, so many strips work with a short wire, but not reliably. If you see flicker or wrong colours, use a 74HCT125 level shifter or slightly lower the LED supply voltage. When the strip has its own supply, always join the grounds.
Code
Install the Adafruit NeoPixel library from the Library Manager. The code below runs a control web page and needs no extra web library.
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_NeoPixel.h>
const char* WIFI_SSID = "your_wifi";
const char* WIFI_PASS = "your_password";
const int LED_PIN = 18;
const int LED_COUNT = 30;
const uint8_t MAX_BRIGHTNESS = 120; // software cap: 0–255, protects the supply
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
WebServer server(80);
enum Mode { SOLID, RAINBOW, BREATH } mode = SOLID;
uint8_t cr = 255, cg = 120, cb = 0; // current colour
uint8_t bright = 60;
uint32_t lastFrame = 0;
uint16_t hue = 0;
const char PAGE[] PROGMEM = R"HTML(<!doctype html><meta name=viewport content="width=device-width,initial-scale=1">
<title>LED</title>
<body style="font-family:sans-serif;max-width:420px;margin:20px auto;padding:0 16px">
<h2>ESP32 LED</h2>
<p>Colour: <input id=c type=color value="#ff7800"></p>
<p>Brightness: <input id=b type=range min=0 max=255 value=60></p>
<p>Effect:
<select id=m><option value=0>Solid</option><option value=1>Rainbow</option><option value=2>Breathing</option></select></p>
<script>
let t;
function send(){clearTimeout(t);t=setTimeout(()=>{
const h=c.value;
fetch(`/set?r=${parseInt(h.slice(1,3),16)}&g=${parseInt(h.slice(3,5),16)}&b=${parseInt(h.slice(5,7),16)}&br=${b.value}&m=${m.value}`);
},80);}
c.oninput=b.oninput=m.onchange=send;
</script>)HTML";
void handleSet() {
if (server.hasArg("r")) cr = constrain(server.arg("r").toInt(), 0, 255);
if (server.hasArg("g")) cg = constrain(server.arg("g").toInt(), 0, 255);
if (server.hasArg("b")) cb = constrain(server.arg("b").toInt(), 0, 255);
if (server.hasArg("br")) bright = constrain(server.arg("br").toInt(), 0, 255);
if (server.hasArg("m")) mode = (Mode)constrain(server.arg("m").toInt(), 0, 2);
server.send(200, "text/plain", "ok");
}
void setup() {
Serial.begin(115200);
strip.begin();
strip.setBrightness(0);
strip.show(); // everything off at boot
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
Serial.println(WiFi.localIP());
server.on("/", []() { server.send_P(200, "text/html", PAGE); });
server.on("/set", handleSet);
server.begin();
}
void loop() {
server.handleClient();
if (millis() - lastFrame < 20) return; // ~50 frames/s, no delay()
lastFrame = millis();
uint8_t level = min<int>(bright, MAX_BRIGHTNESS);
if (mode == SOLID) {
strip.setBrightness(level);
for (int i = 0; i < LED_COUNT; i++) strip.setPixelColor(i, cr, cg, cb);
} else if (mode == RAINBOW) {
strip.setBrightness(level);
hue += 256; // colour drift speed
for (int i = 0; i < LED_COUNT; i++)
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue + i * 65536L / LED_COUNT)));
} else { // BREATH: smooth fade in and out
float k = (sin(millis() / 1000.0 * 2.0) + 1.0) / 2.0;
strip.setBrightness((uint8_t)(level * k));
for (int i = 0; i < LED_COUNT; i++) strip.setPixelColor(i, cr, cg, cb);
}
strip.show();
}
Open the Serial Monitor to read the IP address and enter it in your phone's browser (same WiFi network). The page sends a request only after you stop dragging a control for about 80 ms, so it does not flood the ESP32.
Why the code is written this way
- No long
delayin the loop:server.handleClient()must be called often, otherwise the web page hangs. - MAX_BRIGHTNESS: even if the user drags the slider to 255, the software caps it so the current stays within the supply's capability. Raise it only after you are sure the supply is adequate.
- gamma32: human brightness perception is not linear; this makes the rainbow look natural.
strip.show()timing: each pixel takes about 30 µs, so 30 pixels take ~1 ms; a 1000-pixel strip takes ~30 ms and blocks the CPU for that time.
Common problems
| Symptom | Cause and fix |
|---|---|
| First pixel shows the wrong colour or the strip flickers randomly | Missing common GND, 3.3 V logic too low, or no series resistor. Use a 74HCT125, join the grounds, add a 330 Ω resistor. |
| ESP32 resets when you show bright white | Weak supply and voltage sag. Lower MAX_BRIGHTNESS, use a bigger supply, power the LEDs separately from the ESP32. |
| The end of the strip is reddish/yellowish and dimmer | Voltage drop along the strip. Inject power in the middle or at the end with thick wire. |
| Wrong colour order (you set red, you get green) | Your strip uses a different order: change NEO_GRB to NEO_RGB or NEO_BRG. |
| Part of the strip stays dark from a certain pixel onward | A pixel in the middle has failed and broken the data chain. Cut out or replace it; check solder joints and capacitor polarity. |
| The web page will not open | The phone is on a different WiFi network, or the router has "AP isolation" on. Use the same network and check the IP in Serial. |
| Effects stutter when several people use the page | The ESP32 serves the web and the LEDs on the same core. Keep requests light, or run the effects in a separate task pinned to the other core. |
Ideas to extend it
- Store colour and brightness with
Preferencesso the light resumes its last state. - Add mDNS (
MDNS.begin("led-strip")) to openhttp://led-strip.localinstead of an IP address. - Integrate with Home Assistant using ESPHome (the
light: platform: neopixelbuscomponent); the approach is similar to the relay control with ESPHome article. - Push new effects without taking the light down: OTA over WiFi.
Summary
- Each pixel draws up to 60 mA: size the supply first and cap brightness in software.
- Common GND, a 330 Ω resistor on the data line, a large capacitor at the strip's power input, and ideally a 3.3 V to 5 V level shifter.
- Avoid long
delaycalls inloop()so the web server stays responsive.