RGB fade on pins 6, 5 and 3, on an Arduino Uno
A common-cathode RGB LED with red on pin 6, green on 5 and blue on 3, each through 220 ohm: analogWrite hands the light from one color to the next. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- PWM brightness with analogWrite
- Mixing red, green and blue light
- Common cathode LEDs
- Cross-fading one value into another
- PWM pins on the Uno
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
An RGB LED is three small LEDs under one lens. Red is on pin 6, green on pin 5 and blue on pin 3, each through a 220 ohm resistor. The common cathode goes to ground. Those three pins are PWM pins, so analogWrite can set each from 0 to 255.
The sketch hands the light from one color to the next. As one value falls from 255 to 0, the next rises from 0 to 255. Your eye blends the three into one color that walks around the wheel: red to green to blue and back to red.
Each step waits STEP_MS, 8 milliseconds.
Parts list
6 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × RGB LEDIn Starter Projects Kit, Sound and Lights Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 3 |
| RGB LED pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (3) pin 2 | RGB LED pin R |
| Resistor, 220 Ω (2) pin 2 | RGB LED pin G |
| Resistor, 220 Ω (1) pin 2 | RGB LED pin B |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// RGB fade on pins 6, 5 and 3: red into green, green into blue, blue into red.
//
// pin 6 the red leg, through 220 ohm
// pin 5 the green leg, through 220 ohm
// pin 3 the blue leg, through 220 ohm
// the long leg (the common cathode) to GND
//
// An RGB LED is three small LEDs under one lens. analogWrite() sets each one
// from 0 (off) to 255 (full on), and your eye blends the three into a single
// color. Pins 3, 5 and 6 are all PWM pins, marked with a ~ on the board.
//
// The sketch hands the light from one color to the next: as one value falls
// from 255 to 0, the next rises from 0 to 255, so the total stays the same
// and the color walks all the way around the wheel.
const int RED_PIN = 6;
const int GREEN_PIN = 5;
const int BLUE_PIN = 3;
const int STEP_MS = 8;
void show(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
// Cross-fade from `from` to `to`, leaving the third pin dark.
void handOver(int from, int to, const char *name) {
for (int value = 0; value <= 255; value++) {
analogWrite(from, 255 - value);
analogWrite(to, value);
delay(STEP_MS);
}
Serial.println(name);
}
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: RGB fade on pins 6, 5 and 3");
show(255, 0, 0);
Serial.println("red");
}
void loop() {
handOver(RED_PIN, GREEN_PIN, "green");
handOver(GREEN_PIN, BLUE_PIN, "blue");
handOver(BLUE_PIN, RED_PIN, "red");
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
Sets all three pins at once with analogWrite. setup uses it to start the LED at full red, with green and blue off.
void show(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}handOver sketch.ino
Fades one color out and another in. A loop counts value from 0 to 255. The first pin gets 255 minus value and the second gets value, with a pause of STEP_MS per step. Then it prints the name of the new color.
// Cross-fade from `from` to `to`, leaving the third pin dark.
void handOver(int from, int to, const char *name) {
for (int value = 0; value <= 255; value++) {
analogWrite(from, 255 - value);
analogWrite(to, value);
delay(STEP_MS);
}
Serial.println(name);
}loop sketch.ino
Calls handOver three times, red to green, green to blue and blue to red, which makes one full trip around the wheel. It repeats forever.
void loop() {
handOver(RED_PIN, GREEN_PIN, "green");
handOver(GREEN_PIN, BLUE_PIN, "blue");
handOver(BLUE_PIN, RED_PIN, "red");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A color is missing This project
Check each resistor goes to the right pin: red on 6, green on 5, blue on 3. A mixed-up wire changes the order. - The LED stays off This project
The common cathode must go to GND. If you have a common anode part, the logic is reversed. - The colors are wrong or one color never lights. RGB LED
Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on. - One color is much brighter than the others, or the LED runs too hot. RGB LED
Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.