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.

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

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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

15 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

    Reply
  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

    Reply
  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.

    Reply
  4. Subject: Thesis

    Hello,
    I read your article about rgb leds and found the pictures very well done. can I please use this in my thesis?

    With best regards
    Fabian Feyerer

    Reply
  5. Hello.

    Great info and just what i need for my project. I see some old comments, so i hope i can still get some help.

    I’m thinking about buying 1K sliding potentiometers, instead of trimpot. What should i pay attention to, when buying. I assume number of connectors is important?

    Reply
    • Hi.
      I guess if you get one with three connectors, it will work as a regular potentiometer/trimpot.
      So, you’ll just need to make sure you wire it correctly: VCC, GND, and data, and you’ll be fine to go with the examples provided.
      You can take a look at this tutorial from another blog that explains it quite well: mschoeffler.com/2020/11/14/arduino-tutorial-slide-potentiometer-slide-pot-controls-led-strip-ws2812b/
      Regards,
      Sara

      Reply

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.