Class 2 · Light¶
The clock works. Now the lamp starts: an ESP32 drives the WS2812B strip that will light the wall. Each LED on the strip has its own tiny chip, so you can set every LED to any colour with one data wire. Today you wire it safely, work out how much current it can pull, and make it fade like a sunrise.
By the end of class
The strip plays a 60-second sunrise, from dark through red and orange to warm white, with its brightness capped by your power budget.
New parts today¶
| Part | What it's for |
|---|---|
| ESP32 DevKit | The lamp's board |
| WS2812B strip | The light |
| 74AHCT125 level shifter | Turns the 3.3 V data signal into 5 V |
| 330 Ω resistor | Protects the first LED's data input |
| 1000 µF capacitor | Smooths the strip's supply |
| 5 V 4 A supply + barrel-jack adapter | Power for the strip |
Build it¶
The ESP32 is a different board from the Uno. Two differences matter today:
- It runs at 3.3 V. Its pins put out 3.3 V, not 5 V. Never connect 5 V to one of its GPIO pins.
- Its pins are called GPIO numbers. "GPIO 25" is the pin labelled 25 (or D25, or G25) on the DevKit.
The strip runs at 5 V and expects a 5 V data signal. 3.3 V is right at the edge of what it accepts: it may work on the bench and fail on the wall. The 74AHCT125 is a buffer that runs on 5 V and accepts a 3.3 V input, so its output is a clean 5 V copy of the ESP32's signal.
While you're developing, the ESP32 is powered by USB from the laptop, and the strip by the 5 V supply. Their grounds are connected. The ESP32's 5V pin stays unconnected until Class 5.
- Unplug everything.
- Find the input end of the strip: the arrows printed on it point away from it. The wires there are 5 V, DIN (data in) and GND.
- Connect the strip's 5 V and GND leads directly to the barrel-jack adapter's + and − terminals.
- Put the 1000 µF capacitor across the same terminals: the stripe (−) side to −. Get this backwards and the capacitor can burst.
- Build the rest on the breadboard:
| From | To |
|---|---|
| Supply − | Breadboard GND rail (a jumper from the adapter terminal) |
| Supply + | Breadboard 5 V rail (a jumper; this rail only powers the level shifter) |
| ESP32 GND | Breadboard GND rail |
| 74AHCT125 pin 14 (VCC) | 5 V rail |
| 74AHCT125 pin 7 (GND) | GND rail |
| 74AHCT125 pin 1 (1OE) | GND rail (turns this buffer on) |
| ESP32 GPIO 25 | 74AHCT125 pin 2 (1A, input) |
| 74AHCT125 pin 3 (1Y, output) | 330 Ω resistor → strip DIN |
| 74AHCT125 pins 4, 10, 13 (unused OE) | 5 V rail (turns the unused buffers off) |
| 74AHCT125 pins 5, 9, 12 (unused inputs) | GND rail |
Pin 1 of the chip is the one next to the notch or dot. Pins count down that side and back up the other.
Warning
The strip's current goes straight from the supply to the strip, never through the breadboard. Breadboard strips and jumpers are made for about 1 A, and a full strip pulls more than three. Until the lamp is wired permanently in Class 5, keep the brightness at 60 or below in your code.
Tool: a new board in PlatformIO
New project, board Espressif ESP32 Dev Module. This is the whole platformio.ini:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
adafruit/Adafruit NeoPixel
The first build downloads the ESP32 tools; give it a few minutes. If the upload stops at Connecting..., hold the BOOT button on the ESP32 until it starts writing. If no port shows up at all, tell Dr. Alvear: the laptop may need a USB driver for your board.
Tool: Adafruit NeoPixel and the for loop
The library treats the strip as a numbered row of LEDs, starting at 0. You set colours in memory, then show() sends them all down the data wire at once.
A for loop runs the same lines once for each number in a range. for (int i = 0; i < 8; i++) means: start i at 0, keep going while i is less than 8, add 1 each time. So the body runs with i = 0, 1, 2 … 7.
Example: a "volume bar" on the first 8 LEDs that grows by one LED every half second, green then red at the top.
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
const int DATA_PIN = 25;
const int NUM_LEDS = 8;
Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(40); // 0-255, scales every colour
strip.clear(); // all off (in memory)
strip.show(); // send it to the strip
}
void loop() {
for (int level = 0; level <= NUM_LEDS; level++) {
strip.clear();
for (int i = 0; i < level; i++) {
if (i < 6) {
strip.setPixelColor(i, strip.Color(0, 255, 0)); // red, green, blue
} else {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
}
strip.show();
delay(500);
}
}
Useful extras: strip.fill(color) sets every LED to one colour, and strip.Color(r, g, b) packs three numbers from 0 to 255 into one colour.
Mission 1 · Every colour
Set NUM_LEDS to the number of LEDs on your strip. Light the whole strip in red, then green, then blue, then a warm white of your choice, two seconds each, at brightness 60 or less.
Done when
Every LED on the strip shows each colour, and none of them is wrong or flickering. (If the colours come out in the wrong order, your strip may be RGB instead of GRB: try NEO_RGB.)
Tool: a power budget
Every part on a supply draws current, and the supply has a limit. A power budget adds up the worst case and checks it against the limit.
Example, not your project: a robot on a 5 V 2 A supply with an Uno (about 0.05 A) and four servos that can each pull about 0.5 A when they stall.
| Part | Worst case |
|---|---|
| Uno | 0.05 A |
| 4 servos × 0.5 A | 2.0 A |
| Total | 2.05 A |
2.05 A is more than 2 A, so either the servos can't all stall at once, or the robot needs a bigger supply. Leave some margin too: run a supply at no more than about 80 % of its rating.
For WS2812B LEDs, the rule of thumb is about 60 mA per LED at full white (20 mA for each of red, green and blue at 255). Current scales with brightness, so setBrightness(128) draws about half.
Mission 2 · Your power budget
Write the budget for your one plug: the strip at full white, the ESP32 (about 0.25 A with Wi-Fi on), and the clock (measure it, or about 0.25 A). Then:
- Check the 60 mA number. Put the multimeter in series with the strip's 5 V lead (10 A jack, A range). Light exactly 10 LEDs full white at brightness 255, and read the current. Then go back to brightness 60 or less.
- Work out the highest brightness that keeps the whole lamp under 80 % of 4 A. Put it in your code as
const int MAX_BRIGHTNESS = ...;and use it everywhere from now on.
Done when
Your build log has the budget table, the measured current for 10 LEDs, and the MAX_BRIGHTNESS you chose with the calculation behind it.
Tool: fading between colours over time
map() can turn time into colour. Measure how far along you are, from 0 to 1000, then map that to each colour channel.
Example: a 10-second "cooling down" light that fades from red to blue, then starts over.
// strip set up as in the volume-bar example
unsigned long start = 0;
const unsigned long FADE_MS = 10000;
void loop() {
unsigned long t = millis() - start;
if (t > FADE_MS) {
start = millis(); // start over
t = 0;
}
long progress = map(t, 0, FADE_MS, 0, 1000); // 0 = start, 1000 = end
int r = map(progress, 0, 1000, 255, 0);
int b = map(progress, 0, 1000, 0, 255);
strip.fill(strip.Color(r, 0, b));
strip.show();
}
To fade through more than two colours, split the time into stages: the first half fades from colour A to B, the second half from B to C. Use if to decide which stage you're in, then map() within that stage. No delay(): the fade keeps running because loop() keeps checking the time.
Why do fades look jumpy at the dark end?
Your eye is much more sensitive to changes in dim light than bright light. Going from 1 to 2 looks like a big step; 200 to 201 looks like nothing. The library has strip.gamma32(color), which bends the numbers to match your eye. Try strip.fill(strip.gamma32(strip.Color(r, g, b))) and compare.
Mission 3 · A 60-second sunrise
Write a sunrise that runs in 60 seconds (a real one will take 20 minutes; this is the fast test version). It starts dark, goes through deep red, then orange, and ends at a warm white at MAX_BRIGHTNESS (or 60 while you're on the breadboard). Choose the colours by eye. Put the length in one constant, so 60 seconds can become 20 minutes by changing one number.
Done when
You can film the strip going from off to warm white in 60 seconds with no visible jumps, and changing one constant changes how long it takes.
If you finish early¶
- Make the sunrise start at one end of the strip and spread to the other.
- Add a "candle" mode: a warm orange with small random flicker using
random(). - Find the colour that looks like real morning sun on the white wall, not just on the strip.
Sign-off with Dr. Alvear
- Wiring checked: level shifter, 330 Ω, 1000 µF the right way round, strip power not through the breadboard
- Power budget and
MAX_BRIGHTNESSin the build log - 60-second sunrise running
- Build log updated (photo + two sentences)