RGB LED Color Mixing
with Arduino
Create millions of unique colors by combining Red, Green, and Blue PWM channels — controlled live with 3 potentiometers. Simulated free in Wokwi, no hardware needed.
What You'll Build
A real-time RGB color mixing station — turn 3 potentiometers to blend any color, see the result on the LED, and read the HEX value live in the Serial Monitor.
Color Mixing
Blend Red, Green, Blue channels using PWM to produce any of 16 million colors.
PWM Control
Use analogWrite() with 0–255 values to precisely control LED brightness per channel.
Potentiometer Input
Three pots map analog readings (0–1023) to PWM values (0–255) in real time.
HEX Output
Serial Monitor prints live RGB values and the matching HTML hex color code.
map() Function
Learn Arduino's map() to scale one number range to another — a key utility function.
Color Theory
Discover how additive RGB mixing produces secondary colors: yellow, cyan, magenta, and white.
RGB Color Theory & PWM
An RGB LED has three tiny LEDs inside — one for each primary color. By varying each LED's brightness with PWM, you mix them like paint — except it's additive mixing with light.
Red
Arduino Pin 9 (PWM)
Pot on A0
0–255 range
Green
Arduino Pin 10 (PWM)
Pot on A1
0–255 range
Blue
Arduino Pin 11 (PWM)
Pot on A2
0–255 range
PWM — How 0 to 255 Controls Brightness
PWM (Pulse Width Modulation) rapidly switches a pin ON and OFF. The ratio of ON time to OFF time (duty cycle) controls perceived brightness without wasting power through a resistor.
Components Used
All available as virtual parts in Wokwi — no shopping needed.
Wiring the Circuit
Paste the diagram.json for an instant setup, or wire manually using the tables below.
-
Quick Setup: Paste the diagram.json
- Click the diagram.json tab in the Wokwi editor
- Select all (Ctrl+A) and delete existing content
- Paste the JSON below, then press Ctrl+S
{ "version": 1, "author": "RGB LED Color Mixing", "editor": "wokwi", "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} }, { "type": "wokwi-rgb-led", "id": "rgb1", "top": -153.6, "left": 153.4, "attrs": { "common": "cathode" } }, { "type": "wokwi-potentiometer", "id": "pot1", "top": 46.55, "left": -182.4, "rotate": 90, "attrs": { "label": "RED" } }, { "type": "wokwi-potentiometer", "id": "pot2", "top": 46.55, "left": -86.4, "rotate": 90, "attrs": { "label": "GREEN" } }, { "type": "wokwi-potentiometer", "id": "pot3", "top": 46.55, "left": 9.6, "rotate": 90, "attrs": { "label": "BLUE" } }, { "type": "wokwi-resistor", "id": "r1", "top": -86.4, "left": 96.65, "rotate": 90, "attrs": { "value": "220" } }, { "type": "wokwi-resistor", "id": "r2", "top": -86.4, "left": 153.65, "rotate": 90, "attrs": { "value": "220" } }, { "type": "wokwi-resistor", "id": "r3", "top": -86.4, "left": 210.65, "rotate": 90, "attrs": { "value": "220" } } ], "connections": [ [ "rgb1:COM", "uno:GND.2", "black", ["v0"] ], [ "uno:9", "r1:2", "red", ["v0"] ], [ "r1:1", "rgb1:R", "red", ["v0"] ], [ "uno:10", "r2:2", "green", ["v0"] ], [ "r2:1", "rgb1:G", "green", ["v0"] ], [ "uno:11", "r3:2", "blue", ["v0"] ], [ "r3:1", "rgb1:B", "blue", ["v0"] ], [ "pot1:SIG", "uno:A0", "red", ["h0"] ], [ "pot1:VCC", "uno:5V", "red", ["h0"] ], [ "pot1:GND", "uno:GND.1", "black", ["h0"] ], [ "pot2:SIG", "uno:A1", "green", ["h0"] ], [ "pot2:VCC", "uno:5V", "red", ["h0"] ], [ "pot2:GND", "uno:GND.1", "black", ["h0"] ], [ "pot3:SIG", "uno:A2", "blue", ["h0"] ], [ "pot3:VCC", "uno:5V", "red", ["h0"] ], [ "pot3:GND", "uno:GND.1", "black", ["h0"] ] ], "dependencies": {} }⚠️ Copy the entire JSON including the outer { } braces, or the circuit won't load correctly.
-
RGB LED Pin Connections
LED Pin Via Resistor Arduino Color R (Red anode) 220Ω (r1) Pin 9 ~PWM Red wire G (Green anode) 220Ω (r2) Pin 10 ~PWM Green wire B (Blue anode) 220Ω (r3) Pin 11 ~PWM Blue wire COM (cathode) — GND Black wire 💡 This is a common cathode RGB LED — one shared GND, three separate anodes. Common anode LEDs work inversely (HIGH = OFF), so check your component type.
-
Potentiometer Connections
Pot VCC GND SIG → Arduino pot1 (RED) 5V GND A0 pot2 (GREEN) 5V GND A1 pot3 (BLUE) 5V GND A2 💡 All three pots share the same 5V and GND rails. Only the SIG wire is different for each — that's what makes each pot control one independent color channel.
Arduino Code — Explained
Paste the full code into sketch.ino in Wokwi. Here's each section explained:
① Pin Setup & Variable Declarations
Defines which Arduino pins connect to which color channels and pots.
/* * RGB LED Color Mixing Station * Control RGB LED colors using 3 potentiometers * Real-time HEX color output on Serial Monitor */ // RGB LED PWM pins (must support ~PWM) const int RED_PIN = 9; const int GREEN_PIN = 10; const int BLUE_PIN = 11; // Potentiometer analog input pins const int RED_POT = A0; const int GREEN_POT = A1; const int BLUE_POT = A2; // Color channel values (0–255) int redValue = 0, greenValue = 0, blueValue = 0; void setup() { Serial.begin(9600); Serial.println("========================================"); Serial.println(" RGB LED Color Mixing Station"); Serial.println("========================================"); Serial.println("Format: R:xxx G:xxx B:xxx | HEX:#XXXXXX"); Serial.println("----------------------------------------"); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); // Boot test — flash white for 1 second setColor(255, 255, 255); delay(1000); setColor(0, 0, 0); Serial.println("Ready! Turn the potentiometers to mix."); } void loop() { // 1. Read pots (0–1023) int rp = analogRead(RED_POT); int gp = analogRead(GREEN_POT); int bp = analogRead(BLUE_POT); // 2. Map to PWM range (0–255) redValue = map(rp, 0, 1023, 0, 255); greenValue = map(gp, 0, 1023, 0, 255); blueValue = map(bp, 0, 1023, 0, 255); // 3. Drive the LED setColor(redValue, greenValue, blueValue); // 4. Print to Serial Monitor displayColorInfo(); delay(100); } void setColor(int r, int g, int b) { analogWrite(RED_PIN, r); analogWrite(GREEN_PIN, g); analogWrite(BLUE_PIN, b); } void displayColorInfo() { // RGB decimal values Serial.print("R:"); printPad(redValue); Serial.print(" G:"); printPad(greenValue); Serial.print(" B:"); printPad(blueValue); // HEX color code Serial.print(" | HEX:#"); printHex(redValue); printHex(greenValue); printHex(blueValue); // Color name Serial.print(" | "); Serial.println(getColorName()); } void printPad(int v) { if (v < 100) Serial.print(" "); if (v < 10) Serial.print(" "); Serial.print(v); } void printHex(int v) { if (v < 16) Serial.print("0"); Serial.print(v, HEX); } String getColorName() { if (redValue < 30 && greenValue < 30 && blueValue < 30) return "Black / Off"; if (redValue > 200 && greenValue > 200 && blueValue > 200) return "White"; if (redValue > 200 && greenValue < 100 && blueValue < 100) return "Red"; if (greenValue > 200 && redValue < 100 && blueValue < 100) return "Green"; if (blueValue > 200 && redValue < 100 && greenValue < 100) return "Blue"; if (redValue > 150 && greenValue > 150 && blueValue < 100) return "Yellow"; if (redValue < 100 && greenValue > 150 && blueValue > 150) return "Cyan"; if (redValue > 150 && greenValue < 100 && blueValue > 150) return "Magenta"; if (redValue > 150 && greenValue > 100 && blueValue < 80) return "Orange"; if (redValue > 150 && greenValue < 100 && blueValue > 100) return "Purple"; return "Mixed Color"; }
② Serial Monitor Output
Live output showing RGB values, HEX code, and color name as you turn the pots:
Additive Color Mixing Results
These are the key colors you can produce — try each combination on the simulation!
Testing in Wokwi
Interact with the 3 potentiometer knobs and watch the RGB LED update in real time.
-
Paste Code and Press Play
Paste the code into sketch.ino, click ▶ Play. The RGB LED briefly flashes white (boot test), then goes dark and waits for pot input.
-
Turn the Three Potentiometers
Click each potentiometer in Wokwi — a circular slider appears. Try these combos:
- RED pot max, others at 0 → Pure Red
- RED + GREEN max, BLUE at 0 → Yellow
- All three max → White
- GREEN + BLUE max, RED at 0 → Cyan
- RED + BLUE max, GREEN at 0 → Magenta
💡 Watch the LED color change smoothly as you turn each knob — no lag because the loop runs every 100ms.
-
Read the HEX Value in Serial Monitor
Open the Serial Monitor tab. Every 100ms it prints the exact RGB values and the HTML hex color code — useful for copying color values into web design projects!
-
Try Hard-Coding a Specific Color
In the code, replace the
loop()content withsetColor(255, 140, 0);for a fixed orange — remove the pot reads and just callsetColor()directly. This is how you'd use it in a real lighting controller.⚠️ Don't forget to put the pots back if you want interactive control again — just undo your changes.
Key Learning Outcomes
Skills gained from completing this project that apply across all future Arduino work.
analogWrite()map() function — scaling one range to anotheranalogRead() on A0–A2Extension Challenges
Finished the core project? Push further with these.
- Add a rainbow cycling effect using
millis()— no potentiometers needed - Display the current HEX color code on an I2C LCD
- Add a button that saves the current color and flashes it 3 times
- Smoothly fade between saved colors using linear interpolation
- Use the Serial Monitor as input — type an RGB value to set the LED
- Add a 4th "White channel" LED controlled by all 3 pots averaged
Quick Quiz
Test your understanding before moving to the next module.
Q1. What does PWM stand for, and what does it control?
Q2. What color does mixing Red (255) + Green (255) + Blue (0) produce?
Q3. Why does the code use map(potValue, 0, 1023, 0, 255)?
Q4. Why are 220Ω resistors needed on each RGB LED pin?
Q5. Which Arduino UNO pins in this project support PWM?
Explore More Arduino Projects
Continue learning with more free Wokwi simulations, organized by difficulty.
Comments
Post a Comment