Arduino RGB LED configuration! That’s what we’re covering in this tutorial! Most of the time you will be using LEDs that are a single color. Red, green, or blue for example. an RGB LED is basically three LEDs in one that can produce many thousands of colors!

Please note this tutorial is not about WS2812b or similar. We have a separate tutorial about those.

RGB LEDs Explained

Before we get into the Arduino RGB LED configuration, wiring, etc. Let’s first go over the basics of the RGB LED itself. The RGB LED consists of 3 LEDs in one. A red, a green, and a blue LED all in the same housing with separate leads for each. This allows the LED to mix colors at different intensities, allowing the presentation of many thousands of colors. There are 4-1 RGB LEDs that include a white LED. Though those are rarer, they work the same way.

RGB LED Pinout

There are two type of RGB LEDs you will encounter and they are wired very differently

  • Common Anode (+) – This type of LED has four pins. A cathode (-) for each color and a shared anode (+). The LED shares a common 3.3V to 5V input voltage. Grounding each anode will light the different colors.
  • Common Cathode (-) – This type of LED has four pins. An anode (+) for each color and shared cathode (-). The LED shares a common ground, apply 3.3V to 5V to each Anode will light the different colors. This is the most common RGB LED.
Arduino RGB LED Common Cathode vs Common Anode

Arduino RGB LED Colors

The human eye has only three cones (most people). These cones are detectors for red, green, and blue color. However, LEDs and LCDs are only capable of displaying those colors individually. These LEDs can be mixed to show show different intensities of these three colors. Because of the small size of each LED your eyes are tricked into seeing different colors that aren’t actually there!

With the Arduino and a technique called PWM, we can pulse each color of the LED separately to create many different colors. The PWM of the Arduino is programmable from 0-255. 0 would be off and 255 would be 100% brightness. That’s 256 possible variations. This gives us a total number of 16,777,216 possible colors to play with.

For example, On a common cathode LED, if we set PWM to these values we will get the color Magenta on our LED. Or at least that’s what your eye will be tricked into seeing!

  • Red PWM = 255 (100%)
  • Green PWM = 0 (0%)
  • Blue PWM = 255 (100%)

Generally speaking, the red is always to the flat side of the LED and the longest pin is your common. However, any configuration is possible. If you don’t have the specs for your LED, you may just need to test it one pin at a time.

Parts List for this Project

If you’d like to do this tutorial at home, here’s a handy parts list for the project. Some of these may be affiliate links. They cost you nothing, but pay us a small commission so that we can continue to create great content like this.

QTYPART/LINK
1XArduino Uno
1XUSB Type B Cable
1XSolderless Breadboard
1XJumper Wire Kit
1XResistor Kit (220 Ohm)
1XLED Kit (incl RGB)
Get a Geek Pub Uno!

Arduino RGB LED Wiring Diagram

Since common cathode is by far the most common LED you will encounter, we’re going to use that in our Arduino RGB LED wiring diagram. Wire your RGB LED up as shown.

Note: If your colors seem off or do not appear correctly, it is most likely that your LED has a custom pinout. You may need to try applying power one pin at a time to determine which pin is which color. The common pin should always be the longest pin.

Arduino RGB LED Wiring Diagram

Arduino RGB LED Code

This first set of code will allow you to simply pick the color you’d like the LED to be:

/*
    ARDUINO RGB LED TUTORIAL: SINGLE COLOR
    By: TheGeekPub.com
    More Arduino Tutorials: https://www.thegeekpub.com/arduino-tutorials/
*/

const int PIN_RED   = 9; //Red LED on pin 9
const int PIN_GREEN = 10; //Green LED on pin 10
const int PIN_BLUE  = 11; //Blue LED on Pin 11

//variables to hold our color intensities
int red;
int green;
int blue;

/* This function "Set Color" will set the color of the LED
   rather than doing it over and over in the loop above. */
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED,   R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE,  B);
}

void setup() {
  //set all three pins to output mode
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);

  //set the color of the LED
  setColor(248, 226, 55); //geek pub yellow!
}

void loop() {
  //nothing needed here
}

This code will cycle through all of the 16 millions colors rapidly. If its too fast, you can uncomment the delay.

/*
    ARDUINO RGB LED TUTORIAL: AUTO CYCLE COLORS
    By: TheGeekPub.com
    More Arduino Tutorials: https://www.thegeekpub.com/arduino-tutorials/
*/

const int PIN_RED   = 9; //Red LED on pin 9
const int PIN_GREEN = 10; //Green LED on pin 10
const int PIN_BLUE  = 11; //Blue LED on Pin 11

//variables to hold our color intensities
int red;
int green;
int blue;

/* This function "Set Color" will set the color of the LED
   rather than doing it over and over in the loop above. */
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED,   R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE,  B);
}

void setup() {
  //set all three pins to output mode
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
}

void loop() {
  for (red = 0; red <= 255; red++) {
    for (green = 0; green <= 255; green++) {
      for (blue = 0; blue <= 255; blue++) {
        setColor(red, green, blue);
        //delay(10);
      }
    }
  }
}

Next Steps

Now that you’re an Arduino RGB LED expert, you can move on to the next tutorial or go back to the index!