Arduino Tutorial: How to use an RGB LED with Arduino.

Arduino Tutorial: How to use an RGB LED with Arduino.

Dear friends welcome to another video tutorial! Today we are going to learn how to use an
RGB led with Arduino. There is a lot to cover so let’s get started! Hey guys, I am Nick and welcome to
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! 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. The parts that we are going to need for this
project are the following: • An Arduino Uno
• An RGB LED • 3 330 Ohms resistors
• A small breadboard • Some wires
You can find links for all the parts in the description of the video below. Let’s now connect the parts together. 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. As always you can find the code of the examples
in the description of the video. As you can see using an RGB led with Arduino
is very easy. Now we can easily add RGB LEDs to our projects
in order to make them much more interesting. I am going to use RGB LEDs a lot in my future
projects. I hope that you liked this video, if so please
press the like button below. It really helps! Thanks! If this is your first time here, I would love
to have you subscribed. In this channel I post videos about DIY projects
every Saturday. I love making things and I believe that anyone
can make things, anyone can become a maker. That’s why I created this channel, in order
to share my knowledge with the community and learn from the community. I hope you will join us. Until next Saturday, Watch, Learn, Build! [ Translating these subtitles? Add your name here! ]

49 thoughts on “Arduino Tutorial: How to use an RGB LED with Arduino.

  1. your code dosnt work!! i keep getting this error

    Arduino: 1.8.2 (Mac OS X), Board: "Arduino Nano, ATmega328"

    /Users/stefanomuneroni/Desktop/RGBexample1/RGBexample2.ino: In function 'void setup()':
    RGBexample2:5: error: redefinition of 'void setup()'
    void setup()
    /Users/stefanomuneroni/Desktop/RGBexample1/RGBexample1.ino:6:6: note: 'void setup()' previously defined here
    void setup()
    /Users/stefanomuneroni/Desktop/RGBexample1/RGBexample2.ino: In function 'void loop()':
    RGBexample2:13: error: redefinition of 'void loop()'
    void loop()
    /Users/stefanomuneroni/Desktop/RGBexample1/RGBexample1.ino:13:6: note: 'void loop()' previously defined here
    void loop()
    exit status 1
    redefinition of 'void setup()'

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

  2. sorry im new!! it keeps highlighting the void setup!! not sure what to do!! because it looks correct to me

  3. Hi,
    What sketch would you use for controlling two different 2 rgb leds? I'm trying to write it so that the first led changes between red and blue and the second led changes between yellow and green. The problem I'm getting is that both leds are changing between red and blue.

  4. Hey! so if i got a common anode can I still control the brightness of the coloured leds?, because as far as I know I have to phisically connect the coloured leds to ground for them to light up, can I do some ground control inside the code?

  5. Hey! So i fixed he thing before, now what's going on is that I don't know which the highest value of the AnalogFunction is, I have to use up to a 500 in some of the values to get a true purple as setting it for example even to 350 it'll still look white.

  6. Hi, I enjoyed your video. I know am aware you video is 2 years old but if you are still online I would like to ask a question of you.

  7. C' est mon premier montage que je réussi à faire tout seul et qui fonctionne  …un gros Merci.

  8. Sir,
    I am your little follower from india…
    I watch your video's from last two years..

    And from inspiration i started my own YouTube channel…and need your support to grow…plzz help me sir..?

  9. the problem i have is that i have to connect the ground pin to source and connect the gnd to the other electrodes of the led to get the wanted colors

  10. Hi! How do I use the keyboard to control the LED? Example Press R to light red. Press G to light green. Press Y to light yellow.

  11. Thank you so much for this. I recently "discovered" Arduino and would like to play around with it some more, and your clear explanation about everything in this project helps a lot. I almost instantly liked and subscribed

Leave a Reply

Your email address will not be published. Required fields are marked *