The RGB LED Sidequest š”
Source: Dev.to
Welcome back to my journey From Dust to Droid Dreams
While the primary path to building a droid is a longāterm mission, sometimes you have to take a sideāquest to master the fundamentals of the ānervous system.ā
Todayās mission: MultiāLED color blending and the hidden traps of Serial debugging.
I discovered that I had an RGB LED and decided to rewire the existing Red, Blue, and Green LEDs while keeping the Yellow LED asāis. The goal was to create a smooth transition between Red, Green, and Blue using PulseāWidth Modulation (PWM), with a dedicated Yellow LED that glows based on the overlap of the Red and Green channels.
The āFiddlyā Hardware
First lesson of the sideāquest: The physical world is messy.
The RGB LED setup is super fiddlyāwires cross over easily, and it requires very careful application of resistors to keep the signals clean and the components safe.
Encountering the āLagā Monster
Once I got the LEDs in sync, I wanted to monitor the values changing in real time. This did not work the way I expected. Suddenly, the synchronization disappearedāthe Yellow LED started stuttering and wouldnāt come on at the right time.
Problematic Code
void updateLEDs() {
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
// Serial output inside the main update loop
Serial.print("R: "); Serial.print(redValue);
Serial.print(" | G: "); Serial.print(greenValue);
Serial.print(" | B: "); Serial.print(blueValue);
Serial.print(" | Y-LED: "); Serial.print(yellowBright);
// ... this was slowing everything down!
}
At the default 9600āÆbaud, Serial communication is relatively slow. Every time the code reaches these print statements, it pauses the loop to send data. This creates a āhiccupā in the PWM signal, making the LEDs flicker and the timing feel āoff.ā
The Optimization Fix
To resolve the lag and reclaim that smooth fade, I applied two key fixes:
- BaudāRate Boost ā increased the speed from 9600 to 115200 (updated in both the code and the IDE configuration).
- Timed Printing (Nonāblocking) ā instead of printing every single loop, I used a timer (
millis()) to update the Serial output every 100āÆms. This keeps the processor focused on the LED pulses.
void updateLEDs() {
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
int yellowBright = (redValue 100) {
Serial.print("R:"); Serial.print(redValue);
Serial.print(" G:"); Serial.print(greenValue);
Serial.print(" B:"); Serial.print(blueValue);
// Added a small offset (+5) so the yellow line is visible on the plotter
Serial.print(" Y-LED:"); Serial.println(yellowBright + 5);
lastPrintTime = millis();
}
}
New Tool Use Unlocked: The Serial Plotter
I also spent some time with the Arduino IDEās Serial Plotter (ToolsāÆ>āÆSerial Plotter). Itās a fantastic tool for visualizing how the color variables oscillate in waves.
I had to iterate a few times to get the Yellow line to show up properly. Because the values were identical to the Red or Green lines, they were hidden behind them. Adding a small offset (+5) to the print statement finally made the Yellow line visible!
A minor mystery: I havenāt figured out how to get the line colors in the Plotter to actually match the RGB colors Iām outputting. The IDE seems to assign them automatically based on the order of the variables. If anyone has a hack for this, please let me know in the comments! Even without matching colors, seeing the waves overlap in realātime is a huge help for debugging the logic.
Key Takeaways for the Droid Build
- Communication Overhead: Consider how much ātalkingā your microcontroller is doingāit might be slowing down its āthinking.ā
- Visualization over Text: Data is much easier to digest as a wave than as a wall of scrolling numbers.
- Timing: (continue your notes hereā¦)
Everything
Donāt just default to delay(). millis() allows for multiātasking, whereas delay() essentially puts the Arduino Uno to sleep.
Happy building, and may your LEDs always blend smoothly!


