RGB color fader
Three PWM pins → three 330Ω resistors → three anodes of an RGB LED → common cathode → ground. That's it.
What you see
Press ▶ Simulate. The LED walks the hue wheel — red, yellow, green, cyan, blue, magenta, back to red. One full loop is about 7.7 seconds.
The trick: PWM
Arduino's analogWrite(pin, 0-255) doesn't actually output an analog
voltage — it outputs a square wave that's on for n/255 of the time.
The LED flickers faster than your eye can see, and you perceive the
average brightness.
analogWrite(pin, 255)→ 100% on, full brightnessanalogWrite(pin, 128)→ 50% duty, half brightnessanalogWrite(pin, 0)→ off
Mix three channels and you get 256³ ≈ 16.7 million colors, same as any monitor.
Try this
- Change each
255to64. LED gets much dimmer but the cycle still runs. - Change
delay(5)todelay(50). Cycle slows 10×. - Hardcode
analogWrite(11, 200); analogWrite(10, 40); analogWrite(9, 150);insidesetup()and remove theloopbody. You've just picked a single color and held it — purple-ish.
The resistor values
With Arduino at 5 V and the R-channel Vf ≈ 1.8 V:
I = (5 - 1.8) / 330 ≈ 9.7 mA. Comfortably under the 20 mA max.
Each color has a different Vf so the three channels brighten at slightly
different rates — that's normal for RGB LEDs and the eye adjusts.