RGB LED Color Mixing with Arduino step by step instruction

 


The RGB LED Color Mixing with Arduino project demonstrates how to create multiple colors by combining Red, Green, and Blue (RGB) light using PWM (Pulse Width Modulation). This beginner-friendly electronics project helps students understand the basics of color theory, digital output control, and Arduino programming.

By adjusting the intensity of each color channel, you can generate thousands of unique colors, making this project perfect for STEM learning, robotics classes, and DIY electronics enthusiasts.


 Components Required

Diagram.json:
{
  "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": {}
}

  • Arduino UNO

  • RGB LED (Common Cathode or Common Anode)

  • 3 × 220Ω resistors

  • Jumper wires

  • Breadboard


 How It Works

An RGB LED contains three small LEDs inside:

  • 🔴 Red

  • 🟢 Green

  • 🔵 Blue

The Arduino controls each color pin using PWM pins (like 9, 10, 11). By varying the PWM values (0–255):

  • Low value → Dim light

  • High value → Bright light

Combining different brightness levels produces mixed colors:

  • Red + Green = Yellow

  • Red + Blue = Magenta

  • Green + Blue = Cyan

  • Red + Green + Blue = White

Code:
/*
 * RGB LED Color Mixing Station
 * Control RGB LED colors using 3 potentiometers
 * Created for Wokwi Simulator
 *
 * Features:
 * - Real-time color mixing
 * - Individual RGB channel control via potentiometers
 * - Serial monitor output showing RGB values
 * - Smooth color transitions
 */

// RGB LED Pin Definitions (PWM pins required)
const int RED_PIN = 9;      // Red LED connected to PWM pin 9
const int GREEN_PIN = 10;   // Green LED connected to PWM pin 10
const int BLUE_PIN = 11;    // Blue LED connected to PWM pin 11

// Potentiometer Pin Definitions (Analog pins)
const int RED_POT = A0;     // Potentiometer for Red channel
const int GREEN_POT = A1;   // Potentiometer for Green channel
const int BLUE_POT = A2;    // Potentiometer for Blue channel

// Variables to store color values (0-255)
int redValue = 0;
int greenValue = 0;
int blueValue = 0;

// Variables to store potentiometer readings (0-1023)
int redPotValue = 0;
int greenPotValue = 0;
int bluePotValue = 0;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  Serial.println("========================================");
  Serial.println("   RGB LED Color Mixing Station");
  Serial.println("========================================");
  Serial.println("Turn the potentiometers to mix colors!");
  Serial.println();
 
  // Set RGB LED pins as outputs
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
 
  // Set potentiometer pins as inputs (optional, analog pins are input by default)
  pinMode(RED_POT, INPUT);
  pinMode(GREEN_POT, INPUT);
  pinMode(BLUE_POT, INPUT);
 
  // Start with a test color (white) to verify LED is working
  setColor(255, 255, 255);
  delay(1000);
 
  // Then turn off
  setColor(0, 0, 0);
 
  // Display initial message
  Serial.println("System Ready!");
  Serial.println("Format: R:xxx G:xxx B:xxx | HEX:#XXXXXX");
  Serial.println("----------------------------------------");
}

void loop() {
  // Read potentiometer values (0-1023)
  redPotValue = analogRead(RED_POT);
  greenPotValue = analogRead(GREEN_POT);
  bluePotValue = analogRead(BLUE_POT);
 
  // Map potentiometer values to RGB range (0-255)
  redValue = map(redPotValue, 0, 1023, 0, 255);
  greenValue = map(greenPotValue, 0, 1023, 0, 255);
  blueValue = map(bluePotValue, 0, 1023, 0, 255);
 
  // Set the RGB LED color
  setColor(redValue, greenValue, blueValue);
 
  // Display color values in Serial Monitor
  displayColorInfo();
 
  // Small delay for stability
  delay(100);
}

// Function to set RGB LED color
void setColor(int red, int green, int blue) {
  // Write PWM values to LED pins
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

// Function to display color information
void displayColorInfo() {
  // Print RGB values
  Serial.print("R:");
  printPaddedValue(redValue);
  Serial.print(" G:");
  printPaddedValue(greenValue);
  Serial.print(" B:");
  printPaddedValue(blueValue);
 
  // Print HEX color code
  Serial.print(" | HEX:#");
  printHex(redValue);
  printHex(greenValue);
  printHex(blueValue);
 
  // Print color name (if identifiable)
  Serial.print(" | ");
  Serial.print(getColorName());
 
  // Print color bar visualization
  Serial.print(" | ");
  printColorBar();
 
  Serial.println();
}

// Function to print padded value (for alignment)
void printPaddedValue(int value) {
  if (value < 100) Serial.print(" ");
  if (value < 10) Serial.print(" ");
  Serial.print(value);
}

// Function to print hexadecimal values
void printHex(int value) {
  if (value < 16) Serial.print("0");
  Serial.print(value, HEX);
}

// Function to get approximate color name
String getColorName() {
  // All colors very low
  if (redValue < 30 && greenValue < 30 && blueValue < 30) {
    return "Black/Off    ";
  }
  // All colors high
  else if (redValue > 200 && greenValue > 200 && blueValue > 200) {
    return "White        ";
  }
  // Pure or dominant colors
  else if (redValue > 200 && greenValue < 100 && blueValue < 100) {
    return "Red          ";
  }
  else if (greenValue > 200 && redValue < 100 && blueValue < 100) {
    return "Green        ";
  }
  else if (blueValue > 200 && redValue < 100 && greenValue < 100) {
    return "Blue         ";
  }
  // Secondary colors
  else if (redValue > 150 && greenValue > 150 && blueValue < 100) {
    return "Yellow       ";
  }
  else if (redValue < 100 && greenValue > 150 && blueValue > 150) {
    return "Cyan         ";
  }
  else if (redValue > 150 && greenValue < 100 && blueValue > 150) {
    return "Magenta      ";
  }
  // Tertiary and other colors
  else if (redValue > 150 && greenValue > 100 && blueValue < 80) {
    return "Orange       ";
  }
  else if (redValue > 150 && greenValue < 100 && blueValue > 100) {
    return "Purple       ";
  }
  else if (redValue < 100 && greenValue > 150 && blueValue > 100) {
    return "Teal         ";
  }
  else if (redValue > 100 && greenValue < 80 && blueValue > 100) {
    return "Violet       ";
  }
  else if (redValue > 200 && greenValue > 150 && blueValue > 150) {
    return "Pink         ";
  }
  else {
    return "Mixed Color  ";
  }
}

// Function to print a visual color bar
void printColorBar() {
  int barLength = (redValue + greenValue + blueValue) / 20; // 0-38 characters
 
  Serial.print("[");
  for (int i = 0; i < barLength; i++) {
    Serial.print("=");
  }
  for (int i = barLength; i < 38; i++) {
    Serial.print(" ");
  }
  Serial.print("]");
}


 Key Features

  • Create millions of color combinations

  • Learn PWM signal control

  • Understand RGB color model

  • Interactive color fading effects

  • Beginner-friendly Arduino project


Learning Outcomes

  • Basics of color mixing (Additive color model)

  • Using PWM with Arduino

  • Controlling LEDs with code

  • Understanding voltage and current limiting using resistors


 Applications

  • Mood lighting systems

  • Smart home lighting

  • Decorative LED projects

  • Interactive art installations

  • IoT-based lighting control


Comments