In this video Arduino RGB LED Tutorial we are going to see how an RGB LED with an Arduino Uno. Let’s get started!
Arduino RGB LED Tutorial – The Parts
Hey guys, I am Nick and welcome to educ8s.tv a channel that is all about DIY electronics projects with Arduino, Raspberry Pi, ESP8266 and other popular boards. In this video we are going to learn how to use an RGB led with Arduino, a very interesting type of LED. As you can see I have connected this LED to an Arduino Uno and every second it changes its color. That’s very handy because we can use only one LED in our projects and produce many colors!
Arduino RGB LED Tutorial – What is an RGB LED?
But what is an RGB LED? An RGB LED is a light emitting diode which can display any color we want. It consists of 3 discrete LEDs, a RED, a GREEN, and a BLUE LED housed in a single package. So by combining these 3 colors we can create any color. As you can see, the RGB LED has 4 pins. So far the LEDs we were using only had 2 pins. The pins are used in order to control the color of the LED. The longest pin is either the anode or the cathode depending on the type of the RGB LED. The LED I have bought is a common cathode LED so the longest leg will be connected to GND. If it was a common anode LED, the longest pin would connect to 5V. The other three pins are Red, Green, and Blue. Let’s connect this RGB LED to Arduino and see how to make it work.
WHERE TO BUY
Arduino Uno: http://bit.ly/Cheap_Uno
RGB LEDs: http://bit.ly/10_RGB_LED
Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!
Arduino RGB LED Tutorial – The Code
I have connected an RGB LED to a small breadboard. This is the longest pin, this the RED pin, the Green and the Blue. In order to limit the current that will run through it we need to use 3 resistors, one for each color pin. If we do not use a resistor or if we use a low value resistor the LED will be destroyed. We need to use a 330 Ohms resistor or higher. The higher the resistor value though the lower the brightness of the LED, so the ideal resistor value is 330 Ohms.
Now all we have to do is to connect the first pin of the LED to digital pin 9, the second pin to GND, the third pin to digital pin 10 and the last pin to digital pin 11. In this first example you can use any digital pins you like. Now if we power up Arduino we can see that the LED lights up RED, then GREEN, and BLUE. All we do in the code is to write HIGH to one digital pin of the Arduino and the LED will light up. So, if we write HIGH to digital pin 9 which is connected to the first pin of the LED, the LED will turn RED at the highest brightness. We follow the same procedure for the other two pins as well.
But if we want to control the brightness of the LED in order to produce more colors we have to use the PWM functionality of the Arduino. Pulse Width Modulation, is a technique for getting analog results with digital means. Instead of writing HIGH to the digital pin, with PWM we send a pulse. The more time the signal is HIGH the brighter the LED appears to the human eye even though it turns on and off quickly. PWM is attached to certain pins of the Arduino Uno. Those digital pins the support PWM have this symbol next to them. I will prepare a detailed tutorial on PWM soon because it is a very useful feature.
We have connected the RGB LED to digital pin 9,10 and 11 which support PWM so we can run another example code that produces more colors using the PWM feature. Let’s see. As you can see that LED now produces many colors, and with different brightness. Less see how to achieve that in code.
I am using a simple function which is named setColor to set the color of the LED. This function takes three arguments. The first one is the brightness of the red LED, the second one is the brightness of the green and lastly the brightness of the blue LED. In each case the number will be in the range 0 to 255, where 0 means off and 255 means maximum brightness. The function then calls ‘analogWrite’ command to set the brightness of each LED.
So, in this line we set the brightness of the red LED to 1 which is the lowest value that we can set, and the other two LEDs are disabled. So, the result we get is RED color with very low brightness. In this line of code we set the brightness of the RED LED to maximum so we get a very bright red color. If we light up more than one LEDs and set their brightness we can create any color we like. For example in this line of code we create the yellow color! You can experiment with the code and create your own colors easily.
CODE OF THE PROJECT
SUBSCRIBE ON YOUTUBE
Never miss a video: Subscribe to educ8s.tv