Connect and use an RGB LED with an Arduino
RGB LEDs are a fun and easy way to add some colour to your projects. Since they are like 3 regular LED in one, the way to use and connect them is not much different.
They come mostly in 2 versions: Common Anode or Common Cathode.
Common Anode uses 5V on the common pin, while Common Cathode connects to Ground.
As with any LED, we need to connect some resistors inline (3 total) so we limit the current being drawn.
In our sketch we will start with the LED in the Red colour state, then fade to Green, then fade to Blue and finally back to the Red colour. By doing this we will cycle through most of the colour that can be achieved.
The RGB LED we are using is a Common Anode, so it requires 5V to the common pin.
* If we were using a Common Cathode, we would connect it to Ground(GND) instead.
We are using 3 resistor to limit the current drawn for the Red, Green, Blue pins. Each of them are 330 Ohms.
The connections are: Pin3-Pin1(red), 5V-Pin2(Anode), Pin5-Pin3(blue), Pin6-Pin4(green).
Our code will use FOR loops to cycle through the colors.
The First FOR loop will go from RED to GREEN
The Second FOR will go from GREEN to BLUE
and finally the last FOR will go from BLUE to RED.
As always you can watch our Tutorial video on this page to get more information about the code used.
Copy and paste the above code in the Arduino IDE to program your Arduino.