How to use an RGB LED to get whatever color you want?New
What is an RGB LED and how does it work?
An RGB LED is basically an LED package that can produce almost any color.
RGB LED Types and Structure
As mentioned earlier, RGB LEDs have three LEDs inside them and usually, these three internal LEDs share either a common anode or a common cathode especially in a through-hole package. So basically, we can categorize RGB LEDs as common anode or common cathode type just like in seven segment displays.
When you look at an RGB LED, you will see that it has four leads. If you face it so that its longest lead is second from the left, the leads should be in the following order: red, anode or cathode, green, and blue.
Common Anode vs. Common Cathode
In a common anode RGB LED, the anode of the internal LEDs are all connected to the external anode lead. To control each color, you need to apply a LOW signal or ground to the red, green, and blue leads and connect the anode lead to the positive terminal of the power supply.
In a common cathode RGB LED, the cathode of the internal LEDs are all connected to the external cathode lead. To control each color, you need to apply a HIGH signal or VCC to the red, green, and blue leads and connect the anode lead to the negative terminal of the power supply.
Controlling an RGB LED using an Arduino Uno
In order to get the color that we want from an RGB LED, we need to set the intensity of each internal LED. To do this, we can either use constant current reduction (CCR) or pulse width modulation (PWM). But in this demo, we're going to use PWM since we are using an Arduino Uno to control the RGB LED.
1. Arduino Uno
2. 3 Resistors
3. 3 Potentiometers/Trimmers
4. Jumper Wires
[Explanation - setup, hardware connection, flow]
We have two schematic diagrams here, one is for the common anode wiring and the other one is for the common cathode. In our example, we are using the wiring for the common cathode RGB LED. But they're almost the same. The only difference is that the second lead of the RGB LED from the left is connected to the 5V pin of the Arduino Uno for the common anode while for the common cathode, the second lead is connected to GND.
Note: You need to change some parts of the code/sketch below if you're using a common anode RGB LED.
[Explanation - Code]
[Explanation - Code]
int RGBLED_RedPin = 9; int RGBLED_GreenPin = 10; int RGBLED_BluePin = 11 pinMode(RGBLED_RedPin, OUTPUT); pinMode(RGBLED_GreenPin, OUTPUT); pinMode(RGBLED_BluePin, OUTPUT);
These lines configure the digital pins D9, D10, and D11 to be the PWM output pins.
int Pot_RedPin = analogRead(A0); int PWMValue_RedPin = Pot_RedPin/4; int Pot_GreenPin = analogRead(A1); int PWMValue_GreenPin = Pot_GreenPin/4; int Pot_BluePin = analogRead(A2); int PWMValue_BluePin = Pot_BluePin/4;
This part of the code tells the Arduino Uno to read the voltage across the wiper terminal of the potentiometers/trimmers through channels A0, A1, and A2. Then it converts the values into a (0-255) range since the ADC uses 10-bit resolution which ranges from (0-1023) but the PWM function of the Arduino Uno uses a (0-255) range.
analogWrite(RGBLED_RedPin, PWMValue_RedPin); analogWrite(RGBLED_GreenPin, PWMValue_GreenPin); analogWrite(RGBLED_BluePin, PWMValue_BluePin);
These lines programs the Arduino Uno