Design a site like this with WordPress.com
Get started

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.

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.

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.

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.

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

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.

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.

  • Arduino UNO – read Best Arduino Starter Kits
  • RGB LED common anode or RGB LED common cathode
  • 3× 1k Ω trimpot
  • Breadboard 
  • 3 × 220 Ω Resistor
  • Jumper wires

Schematic Diagram

Follow the next schematic diagram to wire the circuit:

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:
 
 
*/

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);
}

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

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:

Thanks for reading.

Advertisement

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

11 thoughts on “How do RGB LEDs work?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: