How to use an RGB LED with Arduino

In this Arduino Tutorial, we will learn how to use an RGB led with Arduino. Also, we will learn to adjust the color of RGB led. Let’s begin to code the Arduino to make the RGB led Awesome.

An RGB LED consists of three colors in combinations, but we can make more color by using these colors i.e red, green and blue.


Here some more tutorials for you:


What is RGB LED?

RGB LEDs have red, green and blue elements in a single package. The RGB LED consists of four pins with either anode connected together (known as a common anode) or cathodes connected together (known as a common cathode).

Mainly these types of RGB LEDs are used in lamps, decorative lights and string lights.

In this tutorial, we will use the RGB LED which has a common cathode and different anodes are red, green and blue.

rgb led

Components Needed

  • RGB LED
  • 3 Resistors (300Ω – 1 KΩ)
  • Arduino Uno
  • Breadboard
  • Jumper Wires

Pin Connections

We use common cathode RGB led. Connect the cathode of the RGB LED which is a longer leg to the Ground (GND) of Arduino and the other three legs to pin 3, 5 and 6 of Arduino along with a 220 ohms resistor with each leg. The resistor will prevent the current flow and LED bursting. Refer to the below circuit diagram.


Circuit Diagram

Circuit Diagram for RGB LED with Arduino

Code for RGB LED Arduino

Copy the below code and upload it to the Arduino Board using IDE.

The above code works in the following way:

  1. Pin 3 becomes HIGH.
  2. Wait 500ms.
  3. Pin 3 becomes LOW and Pin 5 becomes HIGH.
  4. Wait 500ms.
  5. Pin 5 becomes LOW and Pin 6 becomes HIGH.
  6. Wait 500ms.
  7. Pin 6 becomes LOW.
  8. Repeat from Step 1.

Output

Output of RGB LED with Arduino

In the above example, we have code the Arduino to blink the RGB LED. The RGB led shows only three colors red, green and blue while blinking.

Now We want to adjust the color of RGB led to show more colors. We use the RGB_color function to explore more colors.


Code for Adjusting Color of RGB LED

In the below code, RGB_color(255, 0, 0) is color code function, if there is a particular color that you want to try and create, find yourself a web color chart by typing web color chart into your favorite search engine.


Another Code for Adjusting the color of RGB LED


Troubleshooting / Errors

  • Incorrect LED connection, so make sure that the LED connection is correct.
  • Use a resistor to avoid the problem of LED bursting.
  • The wire connection may be loose.
Scroll to Top