ESP32 Bench
Boards: ESP32, S3, C3
Flash and monitor: Web Serial
← All projects

NeoPixel effects

Drive a strip or stick of WS2812 LEDs through a color wipe, a rainbow and a theater chase.

Starter15 min115200 baud
Board
Fig. 1. Pins this project uses on the ESP32-DevKitC V4, 38 pin. USB connector at the bottom.

Wiring

FromTo boardNote
Strip 5V or VDC5VFine for a few LEDs. A long strip needs its own 5 V supply
Strip GNDGNDJoin the grounds if the strip has its own supply
Strip DIN, through 330 ΩGPIO 16The resistor goes next to the strip

Tested on this board

Not run on this board yet. Treat the wiring and code as a first draft.

Firmware, Arduino sketch for ESP32 Dev Module

#include <Adafruit_NeoPixel.h>

const int PIN = 16;
const int COUNT = 8;  // LEDs on your strip or stick

Adafruit_NeoPixel strip(COUNT, PIN, NEO_GRB + NEO_KHZ800);

void wipe(uint32_t color) {
  for (int i = 0; i < COUNT; i++) {
    strip.setPixelColor(i, color);
    strip.show();
    delay(60);
  }
}

void rainbow(unsigned long ms) {
  unsigned long start = millis();
  uint16_t hue = 0;
  while (millis() - start < ms) {
    for (int i = 0; i < COUNT; i++) {
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue + i * 65536L / COUNT)));
    }
    strip.show();
    hue += 256;
    delay(20);
  }
}

void chase(uint32_t color, unsigned long ms) {
  unsigned long start = millis();
  int step = 0;
  while (millis() - start < ms) {
    strip.clear();
    for (int i = step % 3; i < COUNT; i += 3) strip.setPixelColor(i, color);
    strip.show();
    step++;
    delay(120);
  }
}

void setup() {
  Serial.begin(115200);
  strip.begin();
  strip.setBrightness(40);  // about 15 %: easy on the eyes and on USB power
  strip.show();
  Serial.printf("%d LEDs on GPIO %d\n", COUNT, PIN);
}

void loop() {
  Serial.println("effect: color wipe");
  wipe(strip.Color(255, 0, 0));
  wipe(strip.Color(0, 255, 0));
  wipe(strip.Color(0, 0, 255));
  Serial.println("effect: rainbow");
  rainbow(5000);
  Serial.println("effect: theater chase");
  chase(strip.Color(255, 160, 0), 4000);
}

Parts

QuantityPartWhere to buy
1×ESP32 dev boardBuy at Amazon
1×NeoPixel Stick, 8 LEDs, or any WS2812B stripBuy at Adafruit
1×330 Ω resistor
1×Optional 74AHCT125 level shifterBuy at Adafruit
3×Jumper wires

Amazon buy links may include an Associates tag, and this site may earn a commission from qualifying Amazon purchases at no extra cost to you. Adafruit links are direct.

Libraries

Adafruit NeoPixel

Load it

  1. In the Arduino IDE, install the esp32 board package and pick the board named above the sketch.
  2. Install the libraries listed above from the Library Manager.
  3. Paste the sketch, choose the board's port and upload. Hold BOOT if the upload stalls at Connecting.
  4. Close the IDE's serial monitor, then open the bench here to watch the output.

Field notes

  • Each LED can draw about 60 mA at full white. Eight LEDs at the sketch's brightness are fine on USB. A metre or more needs its own 5 V supply and a 1000 µF capacitor across it.
  • WS2812B LEDs on 5 V expect a data signal near 5 V. Most accept the ESP32's 3.3 V anyway. If the first LED flickers or stays dark, put a 74AHCT125 level shifter in the data line.
  • No strip yet? The S3 and C3 DevKits have one WS2812 LED on the board: GPIO 38 (or 48 on the first S3 revision) and GPIO 8 on the C3. Set PIN to that and COUNT to 1.
  • Use a current release of Adafruit NeoPixel. Old releases do not build on the 3.x Arduino core.

Not tested yet

  • Not tested on hardware yet. Compiles for every board listed with Arduino-ESP32 core 3.3.12.