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
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:
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);
}
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:
- Guide for WS2812B Addressable RGB LED Strip with Arduino
- What’s the Best LED Strip For Your Project?
- Node-RED with WS2812B Addressable RGB LED Strip
- Arduino Step-by-step Projects – Build 25 Projects (course)
If you like this post probably you might like my next ones (click here to subscribe my blog).
Thanks for reading.
February 2, 2019
Bonjour,
Merci beaucoup pour ce travail remarquable et particulièrement intéressant
Thank you for reading,
Rui
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
Thanks for the suggestion Henk!
I might do that project in the future, after completing what I have on my list
Dear, Rui Santos
can you do a interview with me i really like your article and i want to ask you some
questions for a science project
Sincerely, Bear
Hi.
You can send your questions using our contact page: https://randomnerdtutorials.com/contact/
Regards,
Sara 🙂
Nice article. May I ask what’s the reason for the dots in (255./1023.)?
Very nice article for students.
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
You need a higher resistance on RED so it doesn’t overlap the other colors.
I’m using 150R for the Red leg and 100R for the Green and Blue legs and manage to see all the colors (I found the values here):
https://diotlabs.daraghbyrne.me/docs/leds-continued/images/rgbled_1.png
Using the same on all the legs doesn’t seams to work in practice…
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.
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
Hi.
Yes, you can use them as long as you reference our website in the References section.
Regards
Sara
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?
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