Menu
CoddyTech

Light an RGB LED

Part of the Outputs section of Coddy's Arduino journey. Lesson 6 of 24.

An RGB LED is three small LEDs in one case: red, green and blue. It has four legs, one for each colour and one shared by all three.

In a common cathode RGB LED the shared leg is the minus side of all three LEDs, so it goes to GND. Each colour leg goes through its own resistor to its own pin. On this board:

const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

Each colour is an ordinary LED: set its pin HIGH to light it and LOW to turn it off.

digitalWrite(redPin, HIGH);   // red on
digitalWrite(bluePin, LOW);   // blue off

To change from one colour to another, turn the old one off as well as turning the new one on. If both stay on, you see them mixed.

challenge icon

Challenge

Easy

The RGB LED has red on pin 11, green on pin 10 and blue on pin 9. Make it switch between two colours:

  • red for one second
  • then blue for one second
  • again and again, with only one colour lit at a time

Press Run, then Check.

Try it yourself

const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // red for a second, then blue for a second
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Outputs

Practice on your own: Arduino playground