KY-016 RGB LED 3-Channel PWM Color Mixer
Blend millions of additive RGB hues using three independent hardware PWM channels on Arduino Uno.
Master additive color mixing with the KY-016 common-cathode RGB LED module (or discrete 4-leg RGB LED). Drive Red, Green, and Blue dies independently using 8-bit PWM on pins D9, D10, and D11 to synthesize custom colors, breathing fades, and interactive presets.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerEach RGB channel draws ~12–15mA at 255 PWM duty cycle (~45mA combined at full white), safely powered directly by Arduino Uno GPIO pins through per-channel current-limiting resistors. KY-016 includes onboard 150Ω SMD resistors; discrete 4-pin RGB LEDs require three separate 220Ω resistors (one per color leg).
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P with 6 hardware PWM channels | 1 | $8.00 |
| KY-016 RGB LED Module (or 4-Pin RGB LED) | Common-cathode 5mm tri-color LED | 1 | $1.00 |
| 220Ω Resistors (×3 for bare LED) | Only needed if using bare 4-pin RGB LED | 3 | $0.15 |
| Breadboard & Jumper Wires | 4× color-coded jumpers | 1 | $2.00 |
Electrical & System Architecture
Connect KY-016 GND (-) to Arduino GND, R to D9 (~), G to D10 (~), and B to D11 (~). If using a discrete 4-pin RGB LED, place a 220Ω resistor in series with each of R, G, and B.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling KY-016 Common-Cathode RGB LED Module acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Check Common-Cathode vs. Common-Anode Type
KY-016 modules are common-cathode (pin marked "-" connects to GND).
- Inspect the 4 pins on KY-016: R, G, B, and - (GND).
- For a bare 4-leg RGB LED, the longest pin is the common cathode (or common anode).
- Never share a single resistor on the common pin—each color needs its own resistor due to differing forward voltages.
Wire R, G, B to PWM Pins D9, D10, D11
Connect each color pin to a tilde (~) PWM pin on the Arduino Uno.
- Connect KY-016 "-" to Arduino GND.
- Connect R → D9, G → D10, and B → D11.
- Add 220Ω resistors in series with R, G, and B if using a bare 4-leg LED.
Verify Individual Primary Channels
Upload the channel test diagnostic to ensure Red, Green, and Blue map accurately.
- Flash the diagnostic check below.
- Confirm the LED illuminates pure Red, then pure Green, then pure Blue.
- Swap any mismatched pin labels if your KY-016 silkscreen differs.
Upload Smooth PWM Color Cross-Fader
Flash the additive mixer sketch to step through named colors and continuous spectrum transitions.
- Upload the production sketch below.
- Open Serial Monitor at 115200 baud to observe active RGB tuples.
- Watch seamless transitions across Red → Yellow → Green → Cyan → Blue → Magenta.
Calibrate Pure White Balance
Red often overpowers Green/Blue visually; test reduced Red weighting (e.g., 180, 255, 220) for neutral white.
- Observe raw (255, 255, 255) color tint through a diffuser.
- Reduce the dominant channel by 20–30% to achieve crisp neutral white.
- Record your calibrated RGB values for future status lamps.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: KY-016 Common-Cathode RGB LED Module Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for KY-016 Common-Cathode RGB LED Module before running the full project firmware.
// Diagnostic: Verify R(D9), G(D10), B(D11) Pin Mapping
void setup() { pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); }
void loop() {
analogWrite(9, 180); analogWrite(10, 0); analogWrite(11, 0); delay(700);
analogWrite(9, 0); analogWrite(10, 180); analogWrite(11, 0); delay(700);
analogWrite(9, 0); analogWrite(10, 0); analogWrite(11, 180); delay(700);
}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 11: KY-016 3-Channel PWM RGB Color Mixer
* Board: Arduino Uno R3 | Pins: R=D9, G=D10, B=D11
*/
const uint8_t PIN_R = 9;
const uint8_t PIN_G = 10;
const uint8_t PIN_B = 11;
const bool COMMON_ANODE = false; // Set true for common-anode RGB LEDs
void setColor(uint8_t r, uint8_t g, uint8_t b) {
analogWrite(PIN_R, COMMON_ANODE ? 255 - r : r);
analogWrite(PIN_G, COMMON_ANODE ? 255 - g : g);
analogWrite(PIN_B, COMMON_ANODE ? 255 - b : b);
}
void crossFade(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2, uint16_t steps, uint8_t waitMs) {
for (uint16_t i = 0; i <= steps; i++) {
uint8_t r = r1 + ((int)(r2 - r1) * i) / steps;
uint8_t g = g1 + ((int)(g2 - g1) * i) / steps;
uint8_t b = b1 + ((int)(b2 - b1) * i) / steps;
setColor(r, g, b);
delay(waitMs);
}
}
void setup() {
pinMode(PIN_R, OUTPUT);
pinMode(PIN_G, OUTPUT);
pinMode(PIN_B, OUTPUT);
Serial.begin(115200);
Serial.println(F("KY-016 PWM RGB Color Mixer Ready"));
}
void loop() {
Serial.println(F("Fade: Red -> Amber -> Green"));
crossFade(255, 0, 0, 0, 255, 0, 100, 15);
Serial.println(F("Fade: Green -> Cyan -> Blue"));
crossFade(0, 255, 0, 0, 0, 255, 100, 15);
Serial.println(F("Fade: Blue -> Magenta -> Red"));
crossFade(0, 0, 255, 255, 0, 0, 100, 15);
}Firmware Architecture Explanation:
- Provides a single COMMON_ANODE boolean flag to support both KY-016 common-cathode and common-anode parts.
- Linearly interpolates all three 8-bit PWM channels smoothly over 100 steps per spectrum edge.
- Uses pure hardware PWM on D9/D10/D11 for zero visual flicker.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| R=255, G=255, B=0 | D9+D10 HIGH | Red + Green Mix | Emit Bright Yellow |
| R=0, G=255, B=255 | D10+D11 HIGH | Green + Blue Mix | Emit Electric Cyan |
| R=255, G=0, B=255 | D9+D11 HIGH | Red + Blue Mix | Emit Vivid Magenta |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Colors look inverted (0 is full bright, 255 is off) | Common-Anode RGB LED wired to 5V instead of Common-Cathode | Verify whether common pin goes to 5V or GND | Set COMMON_ANODE = true in the sketch. |
| Only Red lights when mixing Red + Blue | Single shared resistor on common cathode pin | Check resistor placement on breadboard | Give R, G, and B each their own separate 220Ω resistor. |
| One color channel stays dark | Loose jumper or non-PWM pin assignment | Run primary channel diagnostic | Confirm wires land on D9, D10, and D11. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Wire 3 push buttons on D2/D3/D4 to step Red, Green, and Blue brightness independently.
Parse hex codes like #00FF88 from Serial Monitor and display them live.
Scale overall RGB brightness automatically using an LDR light sensor (Project 13).