Arduino LESSON 13: Controlling RGB LED with Arduino
Arduino LESSON 13: Controlling RGB LED with Arduino
OK, we're ready to move on and master new circuit and programming abilities. In today's session, we'll see how to use an arduino microcontroller to operate an RGB (Red, Green, Blue) LED! This will introduce us to a new circuit component and will need the acquisition of new programming abilities.OK, we're ready to move on and master new circuit and programming abilities. In today's session, we'll see how to use an arduino microcontroller to operate an RGB (Red, Green, Blue) LED! This will introduce us to a new circuit component and will need the acquisition of new programming abilities.

Circuit to control RGB LED with an Arduino
An RGB LED is essentially three LEDs rolled into one. It has four leads. The common basis is one lead, the long lead. Then one lead controls the red LED, another the green LED, and yet another the blue LED. All three LEDs are linked to ground through the same pin. The voltages you send to the different control pins may be used to alter the colour of the LED. A schematic will most likely assist you in understanding how the component works and how you should connect to it.

This schematic shows how to work with a common cathode RGB LED
This image depicts the four pins of a common cathode RGB LED. The term "Common Cathode" simply refers to the fact that the LEDs share the same ground pin. There are also "Common Anode" LEDs that share a high voltage pin and each colour has its own ground. These are considerably more complicated in my opinion, but I'll include them only to let you know that this guide is for the common cathode kind. The Sparkfun Inventor Kit has the common cathode arrangement, which I like. Also, as you can see in the image, the length of the pins gives you a hint as to which pin controls red, which green, and which blue.
Now consider utilising this in a circuit. As seen in the circuit, all three LEDs share a single ground connection. As you can see, the pin should be linked to the circuit ground. Consider how you would connect the control pins. To control a standard LED, connect to one of the Arduino output pins. How many control pins on the Arduino will you need to utilise to operate this LED? That's right... three control pins are required. Also, keep in mind that you should never connect an LED directly to a voltage source; instead, use a series current limiting resistor (typically 330 ohms). How many current limiting resistors will we need for these new RGB LEDs? Three will be required... each colour control pin will require its own current limiting resistor. As a result, each colour control pin will be connected to an Arduino output pin through its own current limiting resistor. Once it is connected, we can change the LED's colour by sending voltages from the arduino to particular legs of the RGB LED. When we provide a voltage to the red pin, the LED becomes red. When we provide a voltage to the blue pin, the LED becomes blue. The fascinating part is that you can achieve the in-between mixed hues by writing voltages to several pins. Basically, you can get any colour you want by analogWriting various values to the three separate control pins. But first, let's get our circuit up and running. The circuit below controls red from Arduino pin 6, green from Arduino pin 10, and blue from Arduino pin 11. Go ahead and connect this circuit.

Arduino circuit to control the color of an RGB LED.
Let's now experiment with a program that will independently switch on the various colours. We'll start simply so we can get a sense of how the LED operates.
int redPin=11; //set red LED pin to 11
int greenPin=10; //set green LED pin to 10
int bluePin=6; //set blue LED pin to 6
int brightness=100; //Set brightness to 100
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Turn on Serial port
pinMode(redPin, OUTPUT); //Set redPin to be an output
pinMode(greenPin, OUTPUT); //Set greenPin to be an output
pinMode(bluePin, OUTPUT); //set bluePin to be an output
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(redPin, 0); //turn off red pin
analogWrite(greenPin, 0); //turn off green pin
analogWrite(bluePin, brightness); //write 100 (brightness) to blue pin
}
What colour do you think the LED will be if you use the code above? Connect the circuit and enter the code to see what happens. It is critical that you enter the code. Please do not copy and paste my code. You must type it in. When you write it in, you will almost certainly make mistakes, which will need you to troubleshoot or debug your code. That implies you must identify your flaws. All programmers make mistakes, and it is critical to understand how to spot them early on.
Change the code above to make the LED glow green.
Modify it one more so that the LED glows red.
Now attempt the in-between hues. How would you make the LED glow orange? Experiment with different colour combinations. Attempt to obtain the following colours:
Cyan
Magenta
Yellow
Orange
Purple
What's Your Reaction?