RGB LED Color Mixing with Arduino step by step instruction

RGB LED Color Mixing with Arduino — PWM Control + Wokwi Simulator | MakeMindz
Beginner Project · Module 4 · Arduino + Wokwi

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.

Arduino UNO RGB LED 3 Potentiometers PWM + analogWrite() ~30 min project Beginner
// 00 — Module Overview

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.


// 01 — The Science

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.

0
0% — LED off
64
25% duty cycle
128
50% — medium
192
75% duty cycle
255
100% — full bright
💡 Only certain Arduino UNO pins support PWM — look for the ~ symbol on the board. Pins 9, 10, and 11 all have PWM, making them perfect for RGB control.

// 02 — Parts List

Components Used

All available as virtual parts in Wokwi — no shopping needed.

01
Arduino UNOMicrocontroller — runs code, reads pots, drives RGB LED via PWM.
02
RGB LED (Common Cathode)Single component with 4 pins — shared GND, separate R/G/B anodes.
03
3× PotentiometersOne per color channel — labeled RED, GREEN, BLUE. Analog knob inputs.
04
3× 220Ω ResistorsCurrent limiters — one on each RGB LED pin to prevent burnout.
05
Jumper WiresColor-coded: red for R channel, green for G, blue for B.
06
BreadboardPrototype board for connecting components without soldering.

// 03 — Circuit Setup

Wiring the Circuit

Paste the diagram.json for an instant setup, or wire manually using the tables below.

  1. 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
    diagram.json — paste into Wokwi
    {
      "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.

  2. RGB LED Pin Connections

    LED PinVia ResistorArduinoColor
    R (Red anode)220Ω (r1)Pin 9 ~PWMRed wire
    G (Green anode)220Ω (r2)Pin 10 ~PWMGreen wire
    B (Blue anode)220Ω (r3)Pin 11 ~PWMBlue wire
    COM (cathode)GNDBlack 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.

  3. Potentiometer Connections

    PotVCCGNDSIG → Arduino
    pot1 (RED)5VGNDA0
    pot2 (GREEN)5VGNDA1
    pot3 (BLUE)5VGNDA2

    💡 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.


// 04 — The Code

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.

sketch.ino — full code
/*
 * 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:

RGB LED Color Mixing Station
Format: R:xxx G:xxx B:xxx | HEX:#XXXXXX
----------------------------------------
R:255 G: 0 B: 0 | HEX:#FF0000 | Red
R: 0 G:255 B: 0 | HEX:#00FF00 | Green
R: 0 G: 0 B:255 | HEX:#0000FF | Blue
R:255 G:220 B: 0 | HEX:#FFDC00 | Yellow
R: 0 G:200 B:200 | HEX:#00C8C8 | Cyan
R:200 G: 0 B:200 | HEX:#C800C8 | Magenta
R:255 G:255 B:255 | HEX:#FFFFFF | White

// 05 — Color Reference

Additive Color Mixing Results

These are the key colors you can produce — try each combination on the simulation!

Red
R:255 G:0 B:0
Green
R:0 G:255 B:0
Blue
R:0 G:0 B:255
Yellow
R:255 G:255 B:0
Cyan
R:0 G:255 B:255
Magenta
R:255 G:0 B:255
White
R:255 G:255 B:255
Orange
R:255 G:100 B:0
Purple
R:128 G:0 B:255
💡 This is additive color mixing — opposite to paint! With light: Red + Green = Yellow (not brown). All three at full = White. This is how TV screens and phone displays work.

// 06 — Running the Simulation

Testing in Wokwi

Interact with the 3 potentiometer knobs and watch the RGB LED update in real time.

  1. 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.

  2. 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.

  3. 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!

  4. Try Hard-Coding a Specific Color

    In the code, replace the loop() content with setColor(255, 140, 0); for a fixed orange — remove the pot reads and just call setColor() 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.


// 07 — What You Learn

Key Learning Outcomes

Skills gained from completing this project that apply across all future Arduino work.

🔁Additive color model (RGB) — how screens produce all colors
📡PWM — pulse width modulation with analogWrite()
💡LED control — current limiting with resistors
🎚️map() function — scaling one range to another
📟Analog input with analogRead() on A0–A2
🖥️Hex color output — print formatted data to Serial Monitor

// 08 — Extend It

Extension 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
// 09 — Knowledge Check

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?


// 10 — More Projects

Explore More Arduino Projects

Continue learning with more free Wokwi simulations, organized by difficulty.

Module 4 · RGB LED Color Mixing · Arduino + Wokwi

Simulate free at wokwi.com · No hardware needed · MakeMindz

Comments

try for free