Varying Colors Using
RGB LED

RGB LED:
An RGB LED combines three LEDs in one package: one red, one green, and one blue. Each of these LEDs can be controlled individually.
By varying the intensity of these three colors, you can create different colors through additive color mixing.
How It Works:
The Arduino uses PWM to control the brightness of each color channel in the RGB LED.
PWM involves rapidly switching the LED on and off at a certain frequency. The ratio of on-time to off-time (duty cycle) determines the brightness.
​
By setting different PWM values for the red, green, and blue channels, you can mix these colors to create a wide variety of colors.
For example:
Red: Set only the red channel to a high value.
Green: Set only the green channel to a high value.
Blue: Set only the blue channel to a high value.
Yellow: Combine red and green.
Cyan: Combine green and blue.
Magenta: Combine red and blue.
White: Set all three channels to high values.
Materials

1) Arduino Uno (or any compatible Arduino board)
-
The microcontroller that will control the RGB LED.
​
2) RGB LED
-
A 4-pin LED with red, green, and blue channels.
-
It can be either a Common Cathode or Common Anode type.
​
3) Current-Limiting Resistors (3 x 220 ohms or 330 ohms)
-
These resistors are used to protect the RGB LED by limiting the current flowing through each color channel.
​
4) Breadboard
-
A prototyping board to easily connect and test your circuit without soldering.
​
5) Jumper Wires
-
Used to connect the components on the breadboard and to the Arduino.
​
6) USB Cable
-
To connect the Arduino to your computer for programming and power.
Basic Setup


1) Identifying the RGB LED Pins:
-
RGB LEDs have four pins:
-
Common Pin: This is the shared pin for all the LED channels (either anode or cathode).
-
Red Pin: Controls the red LED.
-
Green Pin: Controls the green LED.
-
Blue Pin: Controls the blue LED.
-
Common Cathode RGB LED: The common pin connects to GND (ground).
​​
-
Common Anode RGB LED: The common pin connects to 5V.
​
​​
2) Connect the Common Pin:
​​
-
Common Cathode RGB LED (Used in this guide)
-
Connect the common pin (longest pin) to GND on the Arduino.
-
​
-
Common Anode RGB LED
-
Connect the common pin to 5V on the Arduino.
-
3) Connect the Red Pin:
-
Place a 220 ohm resistor between the Red Pin of the RGB LED and Digital Pin 11 on the Arduino.
​
4) Connect the Blue Pin:
-
Place a 220 ohm resistor between the Blue Pin of the RGB LED and Digital Pin 10 on the Arduino.
5) Connect the Green Pin:
-
Place a 220 ohm resistor between the Green Pin of the RGB LED and Digital Pin 9 on the Arduino.​​
​


CODE BREAK-DOWN
const int redPin = 11;
-
Define the pin number for the red LED (connected to pin 11).
​​
const int greenPin = 9;
-
Define the pin number for the green LED (connected to pin 9).
​​
const int bluePin = 10;
-
Define the pin number for the blue LED (connected to pin 10).
​
void setColor(int redValue, int greenValue, int blueValue) {
-
Start the setColor function that takes three arguments (red, green, and blue intensity values).
​
analogWrite(redPin, redValue);
-
Set the brightness of the red LED based on the redValue parameter.
​
analogWrite(greenPin, greenValue);
-
Set the brightness of the green LED based on the greenValue parameter.
​
analogWrite(bluePin, blueValue);
-
Set the brightness of the blue LED based on the blueValue parameter.
​
void setup() {
-
Start the setup function, which runs once at the beginning.
​
pinMode(redPin, OUTPUT);
-
Set the red pin as an output.
​
pinMode(greenPin, OUTPUT);
-
Set the green pin as an output.
​
pinMode(bluePin, OUTPUT);
-
Set the blue pin as an output.
​
void loop() {
-
Start the loop function, which runs continuously.
​
setColor(255, 0, 0);
-
Set the RGB LED to full red (maximum red, no green, no blue).​
​
setColor(0, 255, 0);
-
Set the RGB LED to full green (no red, maximum green, no blue).​
​
setColor(0, 0, 255);
-
Set the RGB LED to full blue (no red, no green, maximum blue).​
​
setColor(255, 255, 0);
-
Set the RGB LED to yellow (maximum red, maximum green, no blue).​
​
setColor(0, 255, 255);
-
Set the RGB LED to cyan (no red, maximum green, maximum blue).​
​
setColor(255, 0, 255);
-
Set the RGB LED to magenta (maximum red, no green, maximum blue).
​
setColor(255, 255, 255);
-
Set the RGB LED to white (maximum red, green, and blue).​
​
setColor(0, 0, 0);
-
Turn off the RGB LED (no red, green, or blue).
​
delay(1000);
-
Pause for 1 second before repeating the loop.
​
