Smart homeLED

Control a WS2812 LED strip with an ESP32 from a web page

Difficulty: medium · Time: ~1.5 hours · Cost: ~$8–14

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.

Safety: an LED strip draws a lot of current. Use only an enclosed 5 V DC supply rated for the load, wires of adequate gauge, and never build your own supply from 220 V mains. Do not let high-current wires get warm.

Parts

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

ESP32 DevKitcommon GND5V DC supply≥ pixels × 0.06 AWS2812B strip5VDINGNDC+5V470–1000 µF capGPIO18330 ΩESP32 GND tied to supply GND and strip GND
Figure 1: Wiring. The key points: supply, ESP32 and strip share a common GND; a large capacitor sits at the strip's power input; a series resistor on the data line. If you use a level shifter, put it between GPIO18 and the resistor.

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

Common problems

SymptomCause and fix
First pixel shows the wrong colour or the strip flickers randomlyMissing 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 whiteWeak 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 dimmerVoltage 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 onwardA 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 openThe 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 pageThe 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

Summary