Arduino Mega RGB LED Simulator: Wiring, Code and a Live Circuit
This is a common cathode RGB LED wired to an Arduino Mega 2560 through three 220 ohm resistors and stepping through red, green, blue and white live in your browser.
- 5 V logic
- No library: analogWrite()
- Colors on 9, 10, 11
- Common cathode, 3 x 220 ohm
Wiring the RGB LED to the Arduino Mega
The three color pins go to pin 9, pin 10 and pin 11, all of which can do PWM, and the long leg goes to ground.
Every connection in the circuit above, with the board’s own pin names.
| RGB LED pin | Arduino Mega pin | Why |
|---|---|---|
| R (red) | pin 9 | The red anode, through its own 220 ohm resistor. The pin must be able to do PWM. |
| G (green) | pin 10 | The green anode, through its own resistor. |
| B (blue) | pin 11 | The blue anode, through its own resistor. |
| C (common cathode) | GND | The longest leg joins all three cathodes and goes to ground. |
| Three 220 ohm resistors | One in each color lead | Each color is its own LED and needs its own resistor. |
Power
Each color is a separate LED behind its own 220 ohm resistor, so each pin drives only a few milliamps at 5 V. This is a common cathode part: HIGH lights a color. A common anode part has its long leg on 5V and lights on LOW, so analogWrite values must be inverted.
Pins to leave alone on the Arduino Mega
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The RGB LED uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int RED_PIN = 9; // pin 9
const int GREEN_PIN = 10; // pin 10
const int BLUE_PIN = 11; // pin 11
void setColor(int r, int g, int b, const char* name) {
analogWrite(RED_PIN, r);
analogWrite(GREEN_PIN, g);
analogWrite(BLUE_PIN, b);
Serial.println(name);
delay(400);
}
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
setColor(255, 0, 0, "Red");
setColor(0, 255, 0, "Green");
setColor(0, 0, 255, "Blue");
setColor(255, 255, 255, "White");
}
analogWrite() sends a PWM signal whose duty is the value from 0 to 255, so each color can be set to any brightness and the three mix into any color. 255 on all three makes white, and 0 on all three turns the LED off.
setColor() writes the three duties, prints the color’s name and waits 400 ms, so the loop steps red, green, blue and white about every two seconds.
Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the RGB LED on the Arduino Mega
Using one resistor for all three colors
The three LEDs have different voltage drops, so on one shared resistor the color that drops least takes most of the current and the mix looks wrong. Give each color its own resistor.
Mixing up common cathode and common anode
A common cathode part has its long leg on ground and lights when a pin goes HIGH. A common anode part has its long leg on 5V and lights when a pin goes LOW. With the wrong type everything looks inverted: white when you wanted off.
Choosing pins without PWM
analogWrite() only makes a smooth brightness on pins with PWM hardware. On a pin without it the color is either fully on or fully off. On the Arduino Mega these three pins are PWM pins.
Questions and answers
How do I connect an RGB LED to an Arduino Mega 2560?
Put a 220 ohm resistor in each color lead, wire them to pin 9, pin 10 and pin 11, and join the longest leg to GND for a common cathode part. The sketch then sets each color with analogWrite().
How do I tell a common cathode from a common anode LED?
Connect the long leg to ground and a color leg to 5 V through a resistor. If the color lights, it is common cathode. If nothing lights, try the long leg on power and the color leg on ground.
How do I make other colors?
Mix the three duties: setColor(255, 128, 0, ...) is orange and setColor(128, 0, 255, ...) is violet. The eye does not see brightness in a straight line, so a gamma table gives smoother fades.
Why is the white tinted?
The three LEDs are not equally bright, so equal duties are not white. Lower the strongest color, usually green or blue, until the mix looks right.
Build your own RGB LED project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.