An RGB LED on Arduino Uno

Steps done: 0 of 5

~9~10~11GNDUNOATMEGA328P-PU

Guess the answer and press Run

Color255 80 0time, s0.0

Lesson steps

What color will 255, 80, 0 give?

In the sketch red is 255, green is 80, blue is 0. Pick a color before you run it.

red 255, green 80, blue 0. What color is the LED?

Sketch

rgb_mix.inoTo board
// Common-cathode RGB LED, cathode to GND
// R on D9, G on D10, B on D11, a 220 Ω resistor on each

const int pinR = 9;
const int pinG = 10;
const int pinB = 11;
int red = 255;   // red brightness, 0-255
int green = 80;  // green
int blue = 0;    // blue

void setup() {
  Serial.begin(9600);
}

void loop() {
  analogWrite(pinR, red);
  analogWrite(pinG, green);
  analogWrite(pinB, blue);
  Serial.print(red);
  Serial.print(" ");
  Serial.print(green);
  Serial.print(" ");
  Serial.println(blue);
  delay(500);
}

Tap a line or an orange number

Serial monitor

Serial, 9600 baudboard time 0.0 s

About the lesson

A common cathode RGB LED: three LEDs in one bulb on D9, D10 and D11, each with a 220 Ω resistor. The brightness of each colour changes on the fly, the scene mixes the colour. About 10 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.

Build it for real

Parts

  • Arduino Uno R3
  • Mini breadboard
  • 5 mm RGB LED with a common cathode
  • 3 resistors, 220 Ω
  • 4 male-to-male jumper wires

Wires

FromTo
D9resistor, then the R leg
D10resistor, then the G leg
D11resistor, then the B leg
Long leg (cathode)GND

If the LED has a common anode, its long leg goes to 5V and a color lights on LOW: in the sketch you write 255 minus the brightness.

The sketch below goes to the board with the default numbers: red 255, green 80, blue 0. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.

Flash it to your own board

The whole sketch

// Common-cathode RGB LED, cathode to GND
// R on D9, G on D10, B on D11, a 220 Ω resistor on each

const int pinR = 9;
const int pinG = 10;
const int pinB = 11;
int red = 255;   // red brightness, 0-255
int green = 80;  // green
int blue = 0;    // blue

void setup() {
  Serial.begin(9600);
}

void loop() {
  analogWrite(pinR, red);
  analogWrite(pinG, green);
  analogWrite(pinB, blue);
  Serial.print(red);
  Serial.print(" ");
  Serial.print(green);
  Serial.print(" ");
  Serial.println(blue);
  delay(500);
}

What to keep in mind on a real board

The green and blue dies take less current than red, and white at 255, 255, 255 often comes out pinkish. You tune it by lowering red.

Without resistors, only the board pin current limits the LEDs: the dies overheat, and an Uno pin handles up to 40 mA.

The sketch takes 2630 bytes of flash and 196 bytes of RAM.

Licenses of the lesson code

The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.

The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.

MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.