Control an RGB LED with an Arduino

Arduino Uno tutorials

🕑 This lesson will take about 15 minutes

In this lesson, you will learn how to control an RGB LED (Light Emitting Diode) light with an Arduino. An RGB LED uses combinations of Red, Green, and Blue light to produce different colours. You can write code to specify the amounts of red, green and blue light you would like to mix to produce a desire colour.

The RGB LED has four pins: one for controlling red light, one for green light, one for blue light and one for ground (GND). The longest pin is GND.

There are four pins at the end of the RGB LED. The first on the left is for red, the second pin (which is the longest) is for ground, then the next one is for green, and the last on the right is for blue.

Required parts

  • 1 x Arduino Uno

  • 1 x RGB LED

  • 1 x breadboard

  • 3 x 220 Ohm resistors

  • 4 x male-to-male jumper wires

Wiring schematic

  1. Connect the red pin to digital pin 11 on the Arduino via a 220 Ohm resistor on the breadboard.

  2. Connect the long pin (between the red and green pins) to GND on the Arduino

  3. Connect the green pin to digital pin 10 on the Arduino via a 220 Ohm resistor on the breadboard.

  4. Connect the blue pin to digital pin 9 on the Arduino via a 220 Ohm resistor on the breadboard.

Image showsRGB LED on breadboard. The left pin is connected through a resistor to pin 11 on Arduino. The second (long) pin is connected straight to GND, the third pin to pin 10 via resistor, and fourth pin (on the right) to pin 9 via resistor

The code

Upload the following code to the Arduino Uno.

You can also use the following code which has a dedicated function for setting the colour of the LED. This function makes it easier to change the red, green, and blue values for the LED using one line of code just by calling the RGB_color() function.