How do RGB LEDs work?

With an RGB LED you can produce almost any color. How is this possible with just one single LED? In this article you’ll learn:

  • How RGB LEDs work
  • Control an RGB LED with an Arduino

How do RGB LEDs work?

An RGB LED is a combination of 3 LEDs in just one package:

  • 1x Red LED
  • 1x Green LED
  • 1x Blue LED

You can produce almost any color by combining those three colors. An RGB LED is shown in the following figure:

How to create different colors?

With an RGB LED you can, of course, produce red, green, and blue light, and by configuring the intensity of each LED, you can produce other colors as well.

This is our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks!  SIGN UP NOW »

This is our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! SIGN UP NOW »

For example, to produce purely blue light, you’d set the blue LED to the highest intensity and the green and red LEDs to the lowest intensity. For a white light, you’d set all three LEDs to the highest intensity.

Mixing colors

To produce other colors, you can combine the three colors in different intensities. To adjust the intensity of each LED you can use a PWM signal.

Because the LEDs are very close to each other, our eyes see the result of the combination of colors, rather than the three colors individually.

To have an idea on how to combine the colors, take a look at the following chart. This is the simplest color mixing chart, but gives you an idea how it works and how to produce different colors.

color mixing

Common Anode and Common Cathode RGB LEDs

There are two kinds of RGB LEDs: common anode LED and common cathode LED. The figure below illustrates a common anode and a common cathode LED.

rgb led

In a common cathode RGB LED, all three LEDs share a negative connection (cathode). In a common anode RGB LED, the three LEDs share a positive connection (anode).

This results in an LED that has 4 pins, one for each LED, and one common cathode or one common anode.

RGB LED Pins

RGB LEDs have four leads—one for each LED and another for the common anode or cathode. You can identify each lead by its length, as shown in the following figure.

rgb led pin

Download our Free eBooks and Resources

With the LED facing you so the anode or cathode (the longest lead) is second from the left, the leads should be in the following order: red, anode or cathode, green, and blue.

Distinguish between RGB LED common anode and common cathode

The best way to distinguish between a common cathode and common anode RGB LEDs is using a multimeter.

Put you multimeter is in continuity mode.

Place the red multimeter tip on the longest LED lead. Then, place the black tip on one of the other leads.

If the LED lights up, this means you have a common anode LED.

identify common anode rgb led

On the other hand,  if you have a common cathode RGB LED, you need to placed the black tip on the longest lead, and the red tip on one of the other leads (see figure below).

identify common cathode rgb led

So, to distinguish between common cathode and common anode RGB LEDs:

  • Use a multimeter in continuity mode
  • If the LED lights up with the red tip on the longest lead and the black on one of the other leads – you have a common anode RGB LED
  • If the LED lights up with the black tip on the longest lead and the red tip on one of the other leads – you have a common cathode RGB LED.

Control an RGB LED with the Arduino

In this example, we show you how to control the color of an RGB LED using an Arduino.

The project we’ll build uses three potentiometers to control the light intensity of each pin (LED) of the RGB LED to produce any color you want.

DSC08404

Parts required

For this example you need the following parts:

Note: we’ll be using a common anode LED for this project, but if you already have a common cathode LED it’s fine to use that; just watch out for the differences noted in the circuit wiring and code.

DSC08413

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic Diagram

Follow the next schematic diagram to wire the circuit:

rgb led_potentiometer

Important: if you’re using an common cathode RGB LED , you need to connect the longer lead to GND instead of 5V.

Code

Upload the following sketch to your Arduino board:

/*
 
 All the resources for this project:
 https://randomnerdtutorials.com/
 
*/

int redPin = 3;     // Red RGB pin -> D3
int greenPin = 5;   // Green RGB pin -> D5
int bluePin = 6;    // Blue RGB pin -> D6

int potRed = A0;     // Potentiometer controls Red pin -> A0
int potGreen = A1;   // Potentiometer controls Green pin -> A1
int potBlue = A2;    // Potentiometer controls Blue pin -> A2

void setup() {
  pinMode(redPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(greenPin, OUTPUT);
  
  pinMode(potRed, INPUT); 
  pinMode(potGreen, INPUT); 
  pinMode(potBlue, INPUT); 
}

void loop() {
  // Reads the current position of the potentiometer and converts 
  // to a value between 0 and 255 to control the according RGB pin with PWM
  // RGB LED COMMON ANODE
  analogWrite(redPin, 255-(255./1023.)*analogRead(potRed));
  analogWrite(greenPin, 255-(255./1023.)*analogRead(potGreen));
  analogWrite(bluePin, 255-(255./1023.)*analogRead(potBlue));
 
  // Uncomment for RGB LED COMMON CATHODE
  /*
  analogWrite(redPin, (255./1023.)*analogRead(potRed));
  analogWrite(greenPin, (255./1023.)*analogRead(potGreen));
  analogWrite(bluePin, (255./1023.)*analogRead(potBlue));
  */
  
  delay(10);
}

View raw code

Important: if you’re using an RGB LED common cathode, you need to comment and uncomment some code in the loop()

How the Code Works

The code to control an RGB LED is very straightforward. In our sketch, we start by defining three integer variables called redPin, greenPin and bluePin that refer to the pins that the LED leads are connected to:

int redPin = 3; // Red RGB pin -> D3
int greenPin = 5; // Green RGB pin -> D5
int bluePin = 6; // Blue RGB pin -> D6

Then, we also declare variables to refer to the potentiometers. The potRed will control the red lead, and so on.

int potRed = A0; // Potentiometer controls Red pin -> A0
int potGreen = A1; // Potentiometer controls Green pin -> A1
int potBlue = A2; // Potentiometer controls Blue pin -> A2

The potentiometers should be connected to the Arduino analog pins, because we want to read an analog value from the pots.

In the setup() function, you set the LED pins as outputs:

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

And the potentiometers as inputs:

pinMode(potRed, INPUT);
pinMode(potGreen, INPUT);
pinMode(potBlue, INPUT);

In the loop(), we read the value from the potentiometers and control each LED of the RGB LED accordingly.

To read the value from a potentiometer, we need to use the analogRead() function. For example, to read the value from potRed:

analogRead(potRed)

The analogRead() function returns a value between 0 and 1023.

To send a PWM signal to the LEDs, we use the analogWrite() function. The analogWrite() function accepts as arguments the pin we want to control and a value between 0 and 255, in which 0 corresponds to zero brightness, and 255 maximum brightness. However, a common anode RGB LED works the other way around. Sending 0 sets the LED to maximum brightness, and 255 sets to the lowest brightness (off). So, we need to subtract the result to 255.

Because we read a value between 0 and 1023, and need to output a value between 0 and 255, we need to multiply the value read by (255/1023).

analogWrite(redPin,255-(255./1023.)*analogRead(potRed));
analogWrite(greenPin,255-(255./1023.)*analogRead(potGreen));
analogWrite(bluePin,255-(255./1023.)*analogRead(potBlue));

To control the common cathode RGB LED, comment the previous lines and uncomment the next ones.

analogWrite(redPin,(255./1023.)*analogRead(potRed)); 
analogWrite(greenPin,(255./1023.)*analogRead(potGreen)); 
analogWrite(bluePin,(255./1023.)*analogRead(potBlue));

Sending 255 sets the LEDs to the maximum brightness, and 0 sets to the lowest brightness.

Demonstration

Watch this quick video demonstration to see this project in action:

Wrapping Up

In summary:

  • an RGB LED is a combination of three LEDs in just one package: red, green and blue;
  • there are two kinds of RGB LEDs: common cathode and common anode RGB LEDs;
  • you generate different colors by adjusting the brightness of each of the three LEDs of the RGB LED;
  • to adjust the brightness of each LED, you use a PWM signal.

I hope you found this guide useful. If you like this subject, you may also be interested in the following resources:

If you like this post probably you might like my next ones (click here to subscribe my blog).

Thanks for reading.

February 2, 2019


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

10 thoughts on “How do RGB LEDs work?”

  1. Thanks for your interesting articles and book (which I bought).
    Can’t you make an article describing a LCD display (40×2) connected to an ESP8266-01with I2C interface and using a web page to program text on the dispaly.

    Greetings Henk

  2. I bought a pack of RBG leds (Cathode) that can mix blue and green but it can’t do RED with any other colors. Any time I activate RED it offs other colors (I was trying to get white, mixing all colors).
    What could I be doing wrong or what Leds should I be looking in order to get white trough mixing?
    Thanks,
    Paulo Medronho

  3. Nevermind,
    I was applying low voltage (resistance 220 included on each “leg” color) when applied pure 3V i got the all colors and got white.

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.