One clock, 22 tables: keeping the lights in sync with ESP-NOW
Part 5 of the synced LED lights build: how one board broadcasts a clock, how the rest line up to it, and why the receivers spend 90% of the night with their radios off.
Some links in this post are Amazon affiliate links. As an Amazon Associate I earn from qualifying purchases, at no extra cost to you.
This is the part I’ve been promising since part 1, where it decided which board I bought. The goal is simple to say: every table in the room shows the same color, and they all change together. There’s no Wi-Fi network, and no wires run between tables.
Send a clock, not commands
My first instinct was for one board to shout “turn green!” and everyone else to obey. That works until a message gets lost. Then one table sits on the wrong color until the next command, and someone notices.
The approach Claude suggested, and the one I kept, is to send time instead. The whole show is a function of one number:
color = (show clock / 2 minutes) mod 5
If every table knows the show clock, every table can work out the color on its own, including the 10-second crossfade between colors. Nobody has to be told anything. A lost message costs nothing, because the clock keeps ticking either way.
So the job of the radio shrinks to one thing: keep everyone’s copy of the clock lined up.
ESP-NOW in one paragraph
ESP-NOW is Espressif’s peer-to-peer radio protocol, built into every ESP32-C3 Super Mini. One board can broadcast a small packet, and every other board on the same channel hears it directly. There’s no router, no pairing and no network to join. That last part matters at a venue, where I don’t control the Wi-Fi and don’t want to depend on it. It’s also why I passed on the RP2040-Zero: no radio at all.
The lead unit
One board runs a different sketch and acts as the lead unit. It’s a normal centerpiece with its own ring, but its clock (millis(), the milliseconds since it powered on) is the show clock.
Every 5 seconds it broadcasts a tiny packet, 12 bytes:
- a “magic number,” so the tables ignore any other ESP-NOW traffic in the building,
- a sequence number that counts up with each broadcast,
- the show clock at the moment of sending,
- a color offset (more on that below),
- a brightness, so one board could dim the whole room.
Each broadcast goes out three times, about 30 ms apart. Radio packets get dropped. Sending three copies is cheaper than any kind of acknowledgment scheme, and the tables use the sequence number to ignore the repeats.
The lead unit’s BOOT button skips to the next color. It bumps the color offset and broadcasts right away, so every table follows within a moment. That one was Claude’s idea, not mine. I’d never thought about it, but it’s a handy override if a color looks wrong in the room.
The tables
Each table keeps one number: the difference between its own clock and the lead unit’s.
showClock = millis() + clockOffset;
When a broadcast arrives, the table works out what that offset should be. If it’s the first one, or the clock is off by more than 40 ms, it snaps straight to it. If it’s only a little off, it moves a quarter of the way there. That small detail matters: snapping on every broadcast would make the fade between colors stutter, and nudging keeps it smooth.
The numbers in that chart are made up to show the idea. In practice, once a table has synced, its error stays at a few milliseconds.
Before a table hears anything, it doesn’t sit dark. It glows on its own clock and lines up as soon as the first broadcast lands.
Napping between broadcasts
The radio is the biggest power draw on these boards, and these run on small batteries for a whole evening. Listening all night would waste most of the battery on silence.
But the tables know when the next broadcast is coming: on every 5-second boundary of the show clock. So after a table hears a broadcast, it switches its radio off, sleeps until 400 ms before the next one is due, wakes up, listens and goes back to sleep. In a simulation of the final sketches, the radio was on about 10% of the time, and a table synced within 3 seconds of power-on. Those are simulated numbers, though. I haven’t watched two real units sync yet. That’s the first test once I have a second working unit.
If a table misses a broadcast, it tries again at the next one. After three misses in a row (the lead unit was switched off, or restarted and shifted its timing), it stops napping and listens continuously until it hears the lead again. Meanwhile it keeps glowing on its own clock. The board’s crystal drifts by a fraction of a second per hour, so even a table that lost the lead entirely would stay close enough that nobody at the table would notice.
Settings that have to match
Both sketches share one settings block: channel, magic number, timing, the five colors. Claude put a comment on it saying to keep it identical in both files. A mismatch there doesn’t fail loudly. A table on the wrong channel just glows on its own clock forever, looking almost right.
Two other settings came from people’s experience with these specific boards, not the datasheet:
- Transmit power is turned down to 8.5 dBm. Super Mini boards are widely reported to be more reliable at reduced power, because of how the tiny antenna is laid out.
- The CPU runs at 80 MHz instead of 160. It’s plenty for blending colors, and it saves battery.
Where the AI helped, and where I steered
Claude wrote both sketches, plus a small simulation that ran them together before I flashed any boards. The “send a clock, not commands” design was its suggestion, and it was the right one.
The steering was mostly about what the product needed. I asked for the board’s bright onboard LED to be dimmed to a faint glow, so I can tell a board is alive without it lighting up the table. And an earlier prototype sketch failed to compile on my setup, because the ESP32 board package changed between versions 2 and 3; the final sketches handle both.
What I’d take from this
- Share the state, not the instructions. If every device can compute the result from a shared clock, lost messages stop mattering.
- Make the receivers predict. Knowing when the next message arrives is what makes the radio naps possible.
- Fail toward “almost right.” A table that loses the lead keeps glowing, and only drifts slowly.
- Test the system before the hardware. A simulation checked the timing before I had 22 boards to reflash.
Next
Part 6 is the build itself: flashing, soldering and gluing a unit together, and starting the battery test to see how long one actually runs at wedding brightness.