How to Control RGB LED with Arduino UNO

  • Description: Control the color of an RGB LED using three potentiometers for red, green, and blue components.
  • Requirements: RGB LED, 3 potentiometers, resistors.
  • Learning Points: PWM (analogWrite), RGB color blending.

Schematic:
Schematic



Code:
// File: RGB_LED_Color_Mixer.ino

// Define RGB LED pins
const int redPin = 11;    // Red LED (PWM)
const int greenPin = 10; // Green LED (PWM)
const int bluePin = 9;  // Blue LED (PWM)

// Define potentiometer pins
const int redPot = A0;   // Red potentiometer
const int greenPot = A1; // Green potentiometer
const int bluePot = A2;  // Blue potentiometer

void setup() {
  // Set RGB pins as outputs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  // Start Serial Monitor for debugging (optional)
  Serial.begin(9600);
}

void loop() {
  // Read potentiometer values (0 to 1023)
  int redValue = analogRead(redPot);
  int greenValue = analogRead(greenPot);
  int blueValue = analogRead(bluePot);

  // Map potentiometer values (0 to 255) for PWM
  redValue = map(redValue, 0, 1023, 0, 255);
  greenValue = map(greenValue, 0, 1023, 0, 255);
  blueValue = map(blueValue, 0, 1023, 0, 255);

  // Apply PWM values to RGB LED
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);

  // Debugging: Print values to Serial Monitor
  Serial.print("Red: ");
  Serial.print(redValue);
  Serial.print(" | Green: ");
  Serial.print(greenValue);
  Serial.print(" | Blue: ");
  Serial.println(blueValue);

  delay(50); // Small delay for stability
}


Colors:

white:
White


Blue:
Blue


Green:
Green


Red:
Red



Black:

Black



Blend1:

Color Blend 1




Blend2:


Color Blend 2



How It Works

  1. Potentiometer Values:

    • Each potentiometer adjusts a single color component (Red, Green, or Blue).
    • The potentiometer’s position determines an analog value between 0 and 1023.
  2. Mapping Values:

    • The analog input values (0–1023) are mapped to the PWM range (0–255) using map().
  3. PWM Output:

    • analogWrite() is used to set the brightness of each color component.
  4. Color Mixing:

    • By combining different brightness levels of Red, Green, and Blue, you can create millions of colors.
Or just Download the Project here: 
https://drive.google.com/file/d/1lMa3po8TXfzRmLPR1ty03hLwuU_fmvQc/view?usp=drive_link
And watch my youtube channel, for more tutorials!

Comments