RGB colors on pins 11, 10 and 9, on an Arduino Uno
An RGB LED with red on pin 11, green on 10 and blue on 9, each through 220 ohm: analogWrite shows seven colors in turn and names each one. 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
- Additive color mixing with red, green and blue
analogWritevalues from 0 to 255- Common cathode RGB LEDs
- Storing data in an array of structs
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
An RGB LED holds three tiny LEDs, one red, one green and one blue, in one case. Red is on pin 11, green on pin 10 and blue on pin 9, each through a 220 ohm resistor. The long common leg goes to ground.
The sketch holds a list of seven colors. For each one it sends a value from 0 to 255 to each pin with analogWrite, prints the color's name, and waits a second.
Mixing creates the other colors: red and green make yellow, green and blue make cyan, red and blue make magenta, and all three make white.
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 11 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 9 |
| 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.
// An RGB LED on pins 11, 10 and 9: seven colors, one after another.
//
// pin 11 -> 220 ohm -> red leg pin 10 -> 220 ohm -> green leg
// pin 9 -> 220 ohm -> blue leg the long leg (common cathode) -> GND
//
// analogWrite() sets each color from 0 to 255. Mixing them makes the rest:
// red and green make yellow, green and blue cyan, red and blue magenta.
const int RED_PIN = 11;
const int GREEN_PIN = 10;
const int BLUE_PIN = 9;
struct Color {
const char *name;
uint8_t r, g, b;
};
const Color COLORS[] = {
{"red", 255, 0, 0}, {"green", 0, 255, 0}, {"blue", 0, 0, 255}, {"yellow", 255, 255, 0},
{"cyan", 0, 255, 255}, {"magenta", 255, 0, 255}, {"white", 255, 255, 255},
};
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: RGB LED on pins 11, 10 and 9");
}
void loop() {
for (const Color &c : COLORS) {
analogWrite(RED_PIN, c.r);
analogWrite(GREEN_PIN, c.g);
analogWrite(BLUE_PIN, c.b);
Serial.println(c.name);
delay(1000);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes the three color pins outputs and starts the serial port with a short message.
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: RGB LED on pins 11, 10 and 9");
}loop sketch.ino
Goes through the COLORS list. For each color it writes the red, green and blue values to their pins, prints the name, and waits 1000 ms. When the list ends it starts over.
void loop() {
for (const Color &c : COLORS) {
analogWrite(RED_PIN, c.r);
analogWrite(GREEN_PIN, c.g);
analogWrite(BLUE_PIN, c.b);
Serial.println(c.name);
delay(1000);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors are wrong This project
Check that red is on pin 11, green on pin 10 and blue on pin 9. - The LED stays dark This project
The long leg must go to ground. This sketch is written for a common cathode LED. - 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.