How to Use an RGB LED with Arduino

Learn to use a multi colour LED

Written By: Marcus Schappi

Dash icon
Difficulty
Easy
Steps icon
Steps
11
While RGB LEDs look like conventional LEDs, they are three-in-one, comprising of a red, green and blue LED.

In this guide, we will learn how to control the brightness of each LED and so, change the colour of an RGB LED. We will be using a Little Bird Uno R3 development board, and a Multicolor RGB LED with White Diffused Lens, a mini breadboard and some jumper wires.

After completing this guide, you will be one step ahead with creating your own devices that require illumination or indication, such as an LED cube or an animated pixel art box!

Step 1 Insert the RGB LED

Insert your RGB LED with the Red Pin on the left hand side.

Step 2 Insert a 220 Ohm Resistor on the Red Channel

Insert a 220 Ohm Resistor in line with the Red Pin (pin on the flat side) of the RGB LED.

Step 3 Insert a 220 Ohm Resistor on the Green Channel

Insert a 220 Ohm Resistor in line with the Green Pin of the RGB LED.

Step 4 Insert a 220 Ohm Resistor on the Blue Channel

Insert a 220 Ohm Resistor in line with the Blue Pin of the RGB LED

Step 5 Connect Pin 11 to the Red Channel

Run a jumper from Digital Pin 11 to the 220 Ohm Resistor on the Red Pin.

Step 6 Connect Ground to the Cathode

Run a jumper from Ground to the Ground / Cathode Pin of the RGB LED.

Step 7 Connect Pin 10 to the Green Channel

Run a jumper from Digital Pin 10 to the 220 Ohm Resistor on the Green Pin.

Step 8 Connect Pin 10 to the Green Channel

Run a jumper from Digital Pin 10 to the 220 Ohm Resistor on the Green Pin.

Step 9 Connect Pin 9 to the Blue Channel

Run a jumper from Digital Pin 9 to the 220 Ohm Resistor on the Blue Pin.

Step 10 Upload the code

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

void setup() {

  //  Set the RGB LED pins as OUTPUTs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  setColor(255, 0, 0); //  Red
  delay(1000);
  setColor(0, 255, 0); // Green
  delay(1000);
  setColor(0, 0, 255); // Blue
  delay(1000);
  setColor(255, 255, 0); // Yellow
  delay(1000);
  setColor(80, 0, 80); // Magenta
  delay(1000);
  setColor(0, 255, 255); // Cyan
  delay(1000);
}

// This is a function. You can think of a function
// as a predefined snipped of code that you can call upon
// within your program.

// A function in Arduino starts with the "return type".
// The "return type" is the type of data the function
// will return.

// In our example the function doesn't return anything
// When we have a function that doesn't return anything,
// we put "void" as teh return type

void setColor(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}
Grab this code and upload it in the Arduino IDE.
Your RGB LED should start to cycle through the colours. There is a 1 second delay on each colour change.

Step 11 Change up the code!

Here is a video of the RGB LED going through the colours.
Next up try and make it a bit Christmassy! Maybe flash Green with a Fade to Red!