RGB LED with Arduino 101

by Oscar

In this tutorial, you will learn how to use a RGB LED with Arduino.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.


What is a RGB LED?

With an RGB (Red Green Blue) LED you’ll be able to produce any colour that is flashing everyone’s eyes. At first glance, RGB LEDs look just like regular LEDs, however, inside the usual LED package, there are actually three LEDs, one red, one green and yes, one blue. By controlling the brightness of each of the individual LEDs you can mix pretty much any color you want.

RGB LED tutorial

To Learn about the basic LED usage practice, read here.

At first using an RGB LED with Arduino seems quite complex, but it quite quickly becomes clear that its no more difficult than controlling one of their single colour counter parts.

We mix colors just like you would mix audio with a ‘mixing board’ or paint on a palette – by adjusting the brightness of each of the three LEDs. The hard way to do this would  be to use different value resistors (or variable resistors) as we played with in lesson 2. That’s a lot of work! Fortunately for us, the Arduino has an analogWrite function that you can use with pins marked with a ~ to output a variable amount of power to the appropriate LEDs.

Required Parts for this Tutorial:

RGB LED (common anode)

  • A common anode RGB LED is nothing more complicated than three one colour LEDs (one red, one green, and one blue) housed in a single package.
  • Rather than having 6 leads (a cathode and anode for each LED) it has only 4 one cathode for each colour, and one common anode. (see the schematic diagram below)
  • A common anode RGB LED is the most popular type. It is most commonly found in either a 5mm bulb size or as a 5mm pirahna form factor.

Current Limiting Resistors (270 ohm) (red-purple-brown)

  • Most LEDs are designed to work with a voltage between 1.5v and 3v. As most microcontrollers (including the Arduino) operate on 5 volts a current limiting resistor is required.
  • Consult your LEDs datasheet for maximum ratings but we like to use 270 ohm resistors. This limits the current to ~20mA, well within most LEDs and microcontroller ratings.

Arduino Microcontroller & Breadboard

  • A great open source microcontroller platform (for more details visit arduino.cc)

How to Connect RGB LED with Arduino

The common negative connection of the LED package is the second pin from the flat side of the LED package. It is also the longest of the four leads. This lead will be connected to ground.

For testing purposes, check your LED datasheet for its pin-out or below are the two most common RGB LED form factors and pin-outs. The following is an example:


Wire up the Test Schematic (below)

  • Connect a current limiting resistor (270Ω) to each of the three cathodes
  • Connect the common anode to 5V
  • Test each color by connecting its current limiting resistor to ground
  • Experiment with colour mixing a little by powering multiple elements at once

Each LED inside the package requires its own resistor to prevent too much current flowing through it. The three positive leads of the LEDs (one red, one green and one blue) are connected to Arduino output pins using these resistors.

RGB LED tutorial layout

Why Can the RGB LEDs Change Colour

The reason that you can mix any colour you like by varying the quantities of red, green and blue light is that your eye has three types of light receptor in it (red, green and blue). Your eye and brain process the amounts of red, green and blue and convert it into a color of the spectrum.


If we set the brightness of all three LEDs to be the same, then the overall color of the light will be white. If we turn off the blue LED, so that just the red and green LEDs are the same brightness, then the light will appear yellow.

We can control the brightness of each of the red, green and blue parts of the LED separately, making it possible to mix any color we like.

Basic Arduino Code for RGB LED

The following test sketch will cycle through the colors red, green, blue, yellow, purple, and aqua. These colors being some of the standard Internet colors.

[sourcecode language=”cpp”]
Adafruit Arduino – Lesson 3. RGB LED

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

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

void loop()
setColor(255, 0, 0); // red
setColor(0, 255, 0); // green
setColor(0, 0, 255); // blue
setColor(255, 255, 0); // yellow
setColor(80, 0, 80); // purple
setColor(0, 255, 255); // aqua

void setColor(int red, int green, int blue)
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

Notice the essence here is the setColor function

This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case the number will be in the range 0 to 255. Where 0 means off and 255 means maximum brightness. The function then calls ‘analogWrite’ to set the brightness of each LED.

Display HTML Color (HEX) on RGB LEDs

If you have ever build a website, you might have come across HTML color which are defined by hexadecimal notation (HEX). For example

black -- 000000 darkblue -- 00008B

Let’s say if we want to display purple (#4B0082). indigo

The red, green and blue parts of indigo are (in hex) 4B, 00 and 82 respectively. We can plug those into the ‘setColor’ function like this:

[sourcecode language=”cpp”]
setColor(0x4B, 0x0, 0x82); // indigo

We have used hex numbers for the three parts of the color by putting ‘0x’ in front of them.

Theory Behind (not necessary, only if you feel interested)

Pulse Width Modulation (or PWM) is a technique for controlling power. We also use it here to control the brightness of each of the LEDs. PWM is so widely used in electronics you probably have seen it else where, for example, it is used in controlling servos, DC Voltage Converter etc.

See below picture for the signal from one of the PWM.

RGB LED theory PMW

Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the ‘analogWrite’ function. So ‘analogWrite(0)’ will not produce any pulse at all and ‘analogWrite(255)’ will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time.

If we specify a value in the analogWrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power.

If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing.

You can check out Wikipedia for more info: http://en.wikipedia.org/wiki/Pulse-width_modulation

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.


Ryan 13th May 2016 - 4:37 am

Just curious but is there a reason why you need to use analogWrite instead of digitalWrite ? Does this have to do with the different brightness levels supplied by the R/G/B which a digitalWrite is unable to do…

Boyd 18th October 2015 - 8:25 am

Hi Oscar

So the code is for C++? I tried to put it into the Arduino window that says sketch at the top and then press update. So how do I actually do this?

Drew 30th November 2014 - 8:55 pm

Thank you! I found just what I needed in this thorough tutorial.

Gabrer 17th August 2014 - 3:25 pm

PS: Maybe is inappropriate call anode the pin longer only because is longer! ;)

Roger Hall 1st November 2013 - 4:51 pm

Common anode must go to +5V not earth

thetataz 13th February 2014 - 4:53 pm

This schema and this code is for common cathode, but it’s works.

For common anode, it’s analog Write must be reversed.
example :
For Red color : 0,255,255

Sorry for my bad english, i’m french

Gabrer 17th August 2014 - 3:10 pm

Anode must connect to 5v!
And reverse have no sense.. that it work’s does not mean that it works well!

Common led doesn’t work in reverse

A part of this thank’s for the tutorial! :)