LESSON 13: Controlling RGB LED with Arduino

OK, we are ready to move forward and learn new circuit skills and new programming skills. In today’s lesson we will learn how to control an RGB (Red, Green, Blue) LED with an arduino microcontroller! This will introduce us to a new circuit component, and will require us to learn some new programming skills.

Circuit to control RGB LED with an Arduino

An RGB LED is basically three LED’s in one. It has 4 leads. One lead, the long lead, is the common ground. Then one lead controls the red LED, one lead controls the green LED, and one lead controls the blue LED. All three of the LED’s are connected to ground through the same pin. You can control the color you get out of the LED based on the voltages you write to the different control pins. A schematic will probably help you understand how the component works and how you should hook up to it.

This schematic shows how to work with a common cathode RGB LED

This picture shows the four pins for a common cathode RGB LED. “Common Cathode” just means that the LED’s share the ground pin. There are also “Common Anode” LED’s which share a common high voltage pin, and then each color has its own ground. I think these are much more confusing, but just mention them so that you know that this tutorial is for the common cathode type.  The Sparkfun Inventor Kit has the common cathode configuration, which is the type I prefer. Also note in the drawing you can see that the length of the pins is your clue as to which pin controls red, which green and which blue.

Now lets think about using this in a circuit. As you can see in the schematic, all three of the LED’s share a common ground pin. You can easily see that pin should be connected to your circuit ground. Now, think about how you would connect the control pins. To control a normal LED, you needed to connect to one arduino output pin. To control this LED, how many control pins will you need to use on the arduino? That’s right . . . you will need three control pins. Also, remember than you never connect an LED directly to a voltage source, you always use a series current limiting resistor (typically 330 ohms). For these new RGB LED’s how many current limiting resistors will we need? We will need three . . . each color control pin will need its own current limiting resistor. So each color control pin will connect to an arduino output pin through its own current limiting resistor. Once that is hooked up, we can control what color the LED is by writing voltages from the arduino to specific legs of the RGB LED. If we write a voltage to the red pin, the LED will be red. If we write a voltage to the blue pin, the LED will be blue. Also the exciting thing is that if you write voltages to multiple pins, you can get the in between blended colors. Basically by analogWrite-ing different values to the 3 different control pins, you can get any imaginable color. But first, lets go ahead and get our circuit set up. The following schematic controls red from arduino pin 6, green from arduino pin 10 and blue from arduino pin 11. Go ahead and hook this circuit up.

Arduino RGB LED circuit
Arduino circuit to control the color of an RGB LED.

Now lets play around with a program that will independently turn on the different colors. We will start simple so we can get an intuitive feel for how the LED works.

 With the code above, what color do you anticipate the LED will be? Hook up the circuit and type in the code, and see what happens. It is important that you type in the code. Do not cut and paste my code. You need to type it in. When you type it in, you will probably make mistakes and when you do you will have to troubleshoot or debug your code. That means you have to find your mistakes. All programmers make mistakes, and it is important very early on to learn how to find your mistakes.

Modify the code above so that the LED turns green.

Now modify it again so that the LED turns red.

Now try for the in between colors. How would you get the LED to turn orange? Play around with achieving different colors. Try to get the following colors:






26 thoughts on “LESSON 13: Controlling RGB LED with Arduino”

  1. I have greatly enjoyed your videos. I find that your style with careful instruction backed with written comments is commendable. I like the fact that a usable code is included so that I have a solid start that I can experiment with and develop further.
    I do have a problem with “Lesson 13” in that the only RGB diodes I can get here in Copenhagen are common Anodes! and not common cathodes as used by you. Could you be so kind as to send me some suggestions as to making a substitution?
    best wishes -jeff

    1. Jeff, it should be almost the same only connect the common annode to +5. Then connect the three ground pins to three different arduino pins. If you pull a pin low, that one will turn on. If a pin is high, it will be off. I believe that will work.

      1. But if they are using RGB that are common Anode and trying to follow your video’s code. How could they do it with a RGB that is common Anode.

        The Arduino kit I bought came with a RGB common Anode led. But I was lucky I also had a RGB led module that was common cathode based.

  2. Great Teacher, thank you for the exceptional lessons on Arduino.

    I have a question. When do we use analogWrite and when do we use digitalWrite?

    In lesson 6, the redLedPin and the yellowLedPin were connected to analog pins 9 &10, and you used digitalWrite.

    In lesson 13, the RGB LED were connected to 11, 10 & 6 pins (all analog pins) and now you used analogWrite.

    Can you enlighten me on this, please?

    Thank you.

    1. digitalWrite is when you want on or off. You apply 5V or 0V. The analog write uses pulse width modulation (PWM) to give you simulation of the in between values. Like low, medium, high.

    2. Not really, the ones in lesson 13 are all digital pins. the analog pins are on the other side of the board marked A0 to A5.

      There are two types of dgital pins those that are pwm and those that are not. PWM pins are indicated by a wavy line next to the number (3,5,6,9,10,11).

      All digital pins can be give a high or low value which is basically on and off. However, the PWM pins can also give an in between output 0-255. so if you have something like an led which varies in brightness and it is attached to a PWM pin (e.g. Pin 9) you can either give it a high or low command which would make it on at full brightness or completely off or else you can give it a number between 0-255 e.g. 128 which would put it on at half the brightness.

  3. Thanks for the instructions. I got it working. But why do green and blue flash but red doesn’t? I tried 220 and 1000 ohm resistors and 2 leds. Also, I think you got blue and green back to front because they were swapped and I checked that it matched your diags and LED diagram.

  4. Hi,

    Your videos are of great help for any projects. I wanted to know how can I control 20 RGB LEDs separately from a Arduino UNO. Each RGB LED requires 3 pins, but UNO has 11 digital I/O pins. I don’t need to control brightness though, so ON/OFF stages will also work. Is there a way to do that?
    Say, I want to give colors to these 20 LEDs according to some number. If I enter 5, 5 LEDs glow with RED color, then if I enter 2, 2 LEDs glow with GREEN color and accordingly.. Thanks a lot for your help.

    1. Arljlt – I find the easiest way to control a large number of multicolored LEDs is to use Neopixels (I bought mine from Adafruit). They are individually addressable LEDs (WS2812 LEDs). It sets the LED color via a data signal so it uses only 1 pin on the arduino (plus you need to power the lights of course). They are very cool.
      It would be very easy to do what you are requesting with neopixels.

  5. How do I modify this code so that the LED will blink different colors? I tried adding a delay and then new analogwrite commands, but that didn’t work. Thanks

  6. Your videos are well managed and very much informative. I got just one quick question regarding user inputs.
    There is two different results the compiler throws out.
    Situation 1. Colour choice before while loops.
    Code is colourChoice=Serial.readString();
    When this is done, when running program, at first no matter what right colour choose red, green or yellow, compiler throws out choice is invalid as if we chose wrong colour. Another thing, you have to two types for eg, choice red and again red to make changes.

    Situation 2 is opposite of situation one. while loop before user input.
    I think it is compiler related problems but I find it very interesting and can not understand how it is doing it. Please shed some light on it.

  7. hi, these lesson are by far best to learn about Arduino and you are really a very nice teacher. I want to know about how can i use common anode RGB LED ?
    i just need the circuit diagram and source code. I googled for it and find some, but they are too complicated.
    waiting for response…

  8. Hi, I work on a project in which i have to read string from an external server to arduino. The request from server contain a group of characters (L=On) with what i would like to turn a led On. The problem is, the request come with informations about the server (Glassfish…. content lenght….).
    I don’t know how to capture the only information i need.

  9. float v;
    int l;
    int r=12;
    int g=10;
    int b=8;
    void setup(){

    void loop(){


  10. //Get the RGB led to go from red to green to blue and increase in brightness as it goes through each color

    int redLed=11;//red led on pin 11 PWM
    int greenLed=10;//green led on pin 10 PWM
    int blueLed=6;//blue led on pin 6 PWM

    int potPin=A0;//Read analog read 0 to 1023 from potentiometer
    int readValue;//=0 to 1023
    float voltage;
    float writeValue;//=0 to 255

    void setup() {



    void loop() {


    voltage=(5./1023.)*readValue; //Voltage = 5 divided by 1023 * the read value don’t forget the point “5.” after the number or no float will print

    writeValue=(255./1023.)*readValue;//to analog write to pin needs to be 0 to 255 ” 255. divided by 1023. * by the analog read pin 0 to 1023 ”

    if(writeValue >=0. && writeValue85. && writeValue170. && writeValue<=255.){


  11. In lesson 13 you wrote this direction for wiring up the rgb LED. ” The following schematic controls red from arduino pin 6, green from arduino pin 10 and blue from arduino pin 11. Go ahead and hook this circuit up.”. The program and the drawn out wiring diagram show the red and the blue LED reversed. I don’t know if anyone has pointed this out, but I noticed it immediately when wiring up this lesson.

  12. I am enjoying and learning a lot of good stuff from this site,and i’ve been making beautiful projects based on this knowledge ,i would like to provide an easement in the circuit ,instead of using three resistor you can use one resistor on cathode pin ,it is just a suggestion ,am i write?

  13. I’m sure we will learn an easier way to do this. Seems a lot of code just for the one LED. Here is what I came up with for red, green, blue, cyan, magenta, yellow, purple, pink, & orange. Took a lot of trial and error to get there. Not perfect but as close as I could get it before getting a headache. 🙂

    int redPin=11; //set redPin to pin 11
    int greenPin=10; //set greenPin to pin 10
    int bluePin=6; //set bluePin to pin 6
    int brightness=255; //set brightness to 75 of 255

    void setup() {
    Serial.begin(9600); //turn on serial monitor at 9600 baud
    pinMode(redPin, OUTPUT); //set redPin as an output
    pinMode(greenPin, OUTPUT); //set greenPin as an output
    pinMode(bluePin, OUTPUT); //set bluePin as an output


    void loop() {
    //set LED to RED
    analogWrite(redPin,brightness); //set redPin output to 255
    delay(2000); //delay of 2 second
    //set LED to GREEN
    analogWrite(redPin, 0); //turn off red LED
    analogWrite(greenPin, brightness); //set greenPin output to 255
    delay(2000); //delay of 2 second
    //set LED to BLUE
    analogWrite(greenPin, 0); //turn off green LED
    analogWrite(bluePin, brightness); //set bluePin output to 255
    delay(2000); //delay of 2 second
    //set LED to CYAN
    analogWrite(redPin, 0); //set red LED to 0
    analogWrite(greenPin, 255); //set green LED to 255
    analogWrite(bluePin, 255); //set blue LED to 255
    delay(2000); //delay of 2 second
    //set LED to MAGENTA
    analogWrite(redPin,255); //set red LED to 255
    analogWrite(greenPin, 0); //set green LED to 0
    analogWrite(bluePin, 245); //set blue LED to 245
    delay(2000); //delay of 2 second
    //set LED to YELLOW
    analogWrite(redPin, 255); //set red LED to 255
    analogWrite(greenPin, 107); //set green LED to 107
    analogWrite(bluePin, 0); //set blue LED to 0
    delay(2000); //delay of 2 second
    //set LED to PURPLE
    analogWrite(redPin, 190); //set red LED to 190
    analogWrite(greenPin, 0); //set green LED to 0
    analogWrite(bluePin, 255); //set blue LED to 255
    delay(2000); //delay of 2 second
    //set LED to PINK
    analogWrite(redPin, 255); //set red LED to 255
    analogWrite(greenPin, 0); //set green LED to 0
    analogWrite(bluePin, 100); //set blue LED to 100
    delay(2000); //delay of 2 second
    //set LED to ORANGE
    analogWrite(redPin, 245); //set red LED to 245
    analogWrite(greenPin, 40); //set green LED to 40
    analogWrite(bluePin, 0); //set blue LED to 0
    delay(2000); //delay of 2 second
    //turn off all LED’s
    analogWrite(redPin, 0); //set red LED to 0
    analogWrite(greenPin, 0); //set green LED to 0
    analogWrite(bluePin, 0); //set blue LED to 0


Comments are closed.