Learning to use RGB LEDs with Arduino opens up a world of colorful projects. These tri-color LEDs combine red, green, and blue diodes in one package, allowing you to create millions of color combinations through pulse-width modulation (PWM). This RGB LED with Arduino tutorial will show you both basic and advanced control methods.
In This Lesson
Components Needed
- Arduino Uno (or compatible board)
- Common cathode RGB LED (Key specification for correct wiring)
- 3× 220Ω (or greater) resistors (Current-limiting for each color channel)
- Breadboard and jumper wires (For prototyping)
- USB cable (For power and programming)
Connection and Circuit
For a common cathod RGB LED
- Longest LED leg (common cathode) → Arduino GND
Or
Longest LED leg (common anode) → Arduino 5V - Red anode → Digital Pin 9 (with 330Ω resistor)
- Green anode → Digital Pin 10 (with 330Ω resistor)
- Blue anode → Digital Pin 11 (with 330Ω resistor)
WARNING: Always verify your RGB LED type (common cathode vs. anode) before wiring!

Code:
The codes below work for common cathode mode, and will work upside down for common anode (subtract the below numbers from 255 for common anode).
Method 1: Basic Color Control Using analogWrite()
void setup() {
pinMode(9, OUTPUT); // Red
pinMode(10, OUTPUT); // Green
pinMode(11, OUTPUT); // Blue
}
void loop() {
// Purple (Red + Blue)
analogWrite(9, 255); // Red full
analogWrite(10, 0); // Green off
analogWrite(11, 150); // Blue mid
delay(1000);
// Teal (Green + Blue)
analogWrite(9, 0); // Red off
analogWrite(10, 100); // Green mid
analogWrite(11, 255); // Blue full
delay(1000);
// White (All colors) AND No light if common anode
analogWrite(9, 255);
analogWrite(10, 255);
analogWrite(11, 255);
delay(1000);
}
Method 2: For Loop Color Transition (All Pins)
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Rainbow cycle through all colors
for(int i=0; i<=255; i++) {
analogWrite(redPin, 255-i); // Red fades out
analogWrite(greenPin, i); // Green fades in
analogWrite(bluePin, 0); // Blue off
delay(10);
}
for(int i=0; i<=255; i++) {
analogWrite(greenPin, 255-i); // Green fades out
analogWrite(bluePin, i); // Blue fades in
analogWrite(redPin, 0); // Red off
delay(10);
}
for(int i=0; i<=255; i++) {
analogWrite(bluePin, 255-i); // Blue fades out
analogWrite(redPin, i); // Red fades in
analogWrite(greenPin, 0); // Green off
delay(10);
}
}
How this code works
For loops are used to repeat a piece of code for a number of time. They have this format
for(int i=minNo; i<maxNo; i++){
//put the code you want to repeat here
//you can have more than one line
}
This creates a counting variable called i, set it equal to a number minNo, and checks if i is less than maxNo. If i<maxNo is true, it will run the codes between the curly brackets, then perform i++, which simply adds 1 to i before it checks the i<maxNo condition again, and keeps going until i<maxNo becomes false and the loop breaks.
The analogWrite
function in Arduino is used to simulate analog output using a technique called PWM (Pulse Width Modulation). Even though Arduino doesn’t have true analog output, analogWrite(pin, value)
quickly turns the pin on and off to create an average voltage between 0 and 5 volts. The value
can be any number from 0 (always off) to 255 (always on), and values in between control how long the pin stays on versus off in each cycle. This is useful for dimming LEDs or controlling motor speeds smoothly.
Understanding Color Mixing
RGB Color Matrix:
Red | Green | Blue | Result Color |
---|---|---|---|
255 | 0 | 0 | Bright Red |
0 | 255 | 0 | Bright Green |
0 | 0 | 255 | Bright Blue |
255 | 255 | 0 | Yellow |
255 | 0 | 255 | Magenta |
0 | 255 | 255 | Cyan |
255 | 255 | 255 | White |
Advanced Techniques
- Color Palette Generator
// write this function at the end of your code
void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
// Usage, inside loop:
setColor(255, 50, 0); // Orange
- Serial Color Control (Add to setup:
Serial.begin(9600);
)
if(Serial.available() >= 3) { int r = Serial.parseInt(); int g = Serial.parseInt(); int b = Serial.parseInt(); setColor(r,g,b); }
Troubleshooting
- Only one color works: Check all three PWM pins are connected
- Colors appear dim: Verify resistor values (330Ω recommended)
- Unexpected colors: Confirm common cathode vs. anode wiring
Going Even Further
- Try to use 3 push buttons to activate the RGB colours you like instead of directly using code or Serial.parseInt() for input
- Use an ultrasonic sensor with a map() function to change the pwm value of 1 pin based on distance.
Leave a Reply