Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

With an RGB LED you can produce almost any color you would like.

How is this possible with just


one single LED?
How do RGB LEDs work?
In fact, an RGB LED is a combination of 3 LEDs in just one package:
 1x Red LED
 1x Green LED
 1x Blue LED
The light that the RGB LED produces are made combining these three LED colors. An RGB LED
looks like this:

Two types of RGB LEDs


There are common anode RGB LEDs and common cathode RGB LEDs. See figure below:

As you can see, the 3 LEDs can share the cathode or the anode. This results in an RGB LED
that has 4 pins, one for each LED, and one common cathode or one common anode.
The common anode RGB LED is the most popular type.
How to create different colors?
You can create one of those three colors – red, green or blue – by activating just one LED.
For example, if you want to produce blue, you activate the blue LED and turn off the other two.

Mixing colors
To produce other colors, you can combine the three colors in different intensities. To generate
different colors you can use PWM to adjust the brightness of each LED.
As the LEDs are very close to each other, we can only see the final colors result 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, there are more complex color charts on the web.

RGB LED Pins


RGB LEDs have 4 pins which can be distinguished by their length. The longest one is the ground
(-) or voltage (+) depending if it is a common cathode or common anode LED, respectively.
The other three legs correspond to red, green, and blue, as you can see in the figure below:

Example – Control an RGB LED with the Arduino


In this example you will use three potentiometers to control each pin of the RGB LED to produce
any color you want.
Parts required
For this example you’ll need:
 1× Arduino
 1× RGB LED common anode
 3× 1kΩ Potentiometer
 1× Breadboard
 3 × 220Ω Resistor
 Jumper wires

Schematics
Follow these schematics to complete the project:

Important: If you’re using an RGB LED common cathode, you need to connect the longer lead to
GND.
Code
Upload the following sketch to your Arduino board:
/*
All the resources for this project:
http://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./1023.)*analogRead(potRed));
analogWrite(greenPin,(255./1023.)*analogRead(potGreen));
analogWrite(bluePin,(255./1023.)*analogRead(potBlue));

// Uncomment for RGB LED COMMON CATHODE


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

delay(10);
}
view raw Projects/How_do_RGB_LEDs_work.ino

Important: If you’re using an RGB LED common cathode, you need to comment and uncomment
some code in the loop() function as described in the sketch comments.

You might also like