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 Runs in your browser. Free, and no account needed.
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");
}
Parts list
6 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × RGB LED
- 3 × Resistor, 220 Ω
How it is wired
7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 6; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 5; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 3; Resistor, 220 Ω (1) pin 1
- Ground: Arduino Uno R3 pin GND; RGB LED pin C
- RGB LED pin R; Resistor, 220 Ω (3) pin 2
- RGB LED pin G; Resistor, 220 Ω (2) pin 2
- RGB LED pin B; Resistor, 220 Ω (1) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.