LEDSDL: A Self-Driving Lab for Color, Built Around a Tiny Machine That Can Learn

An ESP32, a spectral sensor, and a stochastic optimizer turn RGB light into a closed-loop experiment. The result is a desk-scale lab that measures its own output, scores the result, and tries again.

8 min read • View on GitHub • More from zlite

A desk-scale instrument sits inside a small enclosure with an RGB LED, a spectral sensor, and a browser window showing a live score curve. The scene explains the project as a closed-loop experiment rather than a simple LED demo, with light leaving one side of the box and measurement returning on the other.
LEDSDL turns a tiny enclosure into an experiment that can observe, score, and revise its own light output.
Key Takeaways

Most LED projects stop at control. LEDSDL goes further: it turns an RGB LED into an experiment that can look at its own output, judge it, and try a better guess. That makes it feel less like a gadget and more like a miniature lab bench.

The repo is built around a simple but sharp idea. Light is not just set, it is measured. A 10-channel spectral sensor reads the result, a scoring function compares it to a target, and a search loop keeps adjusting until the match improves.

Why color is harder than RGB numbers

In software, color often looks tidy. Three channels, a hex value, done. LEDSDL pushes back on that simplification by treating color as a spectrum that has to survive the physical world before it can be called correct.

That matters because the same LED setting can behave differently once the sensor, enclosure, and ambient conditions get involved. The project’s real subject is not color theory in the abstract. It is measurement under constraint.

The core system is a loop, not a command. It proposes a color, measures the result, scores the gap, and feeds that judgment into the next attempt.

How the machine scores itself

The heart of the repo is computeScore. It does not just ask whether the measured spectrum is close to the target spectrum. It normalizes the measurement, compares it with an L2 distance, and adds a luminosity penalty so darkness cannot cheat the result.

// Conceptual shape of the scoring logic
score = l2_distance(normalized_measured_spectrum, target_spectrum)
score += brightness_penalty_if_too_dim(measured_luminance)

// Lower is better.
// A dim near-match should lose to a bright accurate match.

That choice is small on paper and important in practice. It encodes a policy judgment: a physically good color is not just chromatically correct, it also has to be bright enough to matter. The score is a piece of engineering and a piece of taste.

A close-up cutaway shows measured spectral channels on one side and a target spectrum on the other, with a visible error gap and a brightness penalty marked beneath the comparison. The image explains that the system judges both spectral shape and luminance, not just color similarity.
LEDSDL does not optimize a vague notion of color. It compares measured spectrum against target spectrum and then penalizes outputs that are too dim.

The search loop: from guess to better guess

The optimizer is not trying to be clever in a grand AI sense. It is trying to be disciplined. It samples candidates, compares scores, accepts improvements, and keeps a history of what happened along the way.

That is the important distinction. This is not a scripted LED preset library. It is a stochastic search loop living inside a physical system, where every improvement has to survive measurement noise, sensor response, and the quirks of the enclosure.

The project’s language makes that visible. A Thought buffer records the machine’s latest move, the score, and whether the last candidate improved the result. That turns an embedded optimization run into something that can be watched instead of trusted on faith.

The UI makes the experiment legible

The web interface is not decoration. It is part of the instrument. Because the ESP32 serves state asynchronously, the browser can show the current score, the thought log, and the running history without pausing the loop that is doing the actual work.

That matters for two reasons. First, it makes the system debuggable. Second, it makes the optimization feel honest. You can see when the machine is exploring, when it is improving, and when it is stuck.

This is where the repo earns the phrase self-driving lab. The board is not hidden behind a serial console. It publishes its internal state to a browser, which gives the loop a public face.

What the CircuitPython fallback says about the project

ModeInputFeedback loopWhat it is good forWhat it cannot do
Self-driving LEDSDL loopTarget spectrum plus stochastic candidate settingsYes. Measure, score, accept, retryAutonomous color search with visible reasoningIt is more complex than a basic hardware test
CircuitPython fallback in code.pyManual serial commands for RGB valuesNo. Output is set directlyQuick bring-up and simple LED controlIt cannot discover better colors on its own

That contrast is useful because it shows the boundary of the project. code.py is scaffolding, a practical way to poke the hardware. The main C++ firmware is the actual experiment engine.

What makes this feel like a real lab, not a demo

The physical details matter. The enclosure shields the sensor from ambient light. Non-volatile storage keeps WiFi credentials across restarts. Static buffers keep memory use predictable on the ESP32. The STL files make the hardware reproducible instead of implied.

That combination is what separates a weekend proof of concept from a thoughtful instrument. LEDSDL is small, but it respects the constraints of real hardware, which is why the loop can keep running without turning into a toy.

If you zoom out, the repo is a clean example of a broader pattern: take a physical process, measure it, score it, and let software search inside that feedback loop. The result is not just automation. It is a machine that learns by doing.