RGB LED and Arduino

This article covers the basics of using an RGB (Red Green Blue) LED with an Arduino.

We use the analogWrite function to control the RGB color of the LED.

At first glance, RGB LEDs look just like regular LEDs, but they actually have three LEDs inside: one red, one green, and yes, one blue. By controlling the brightness of each one, you can control the color of the LED.

That is, we will adjust the brightness of each LED and get the desired output color, as if it were an artist’s palette or as if you were adjusting the frequencies on your player. For this you can use variable resistors. But the resulting scheme will be quite complex. Luckily, Arduino offers us the analogWrite function. If we use the pins marked “~” on the board, we can regulate the voltage that is supplied to the corresponding LED.

Required nodes

In order to implement our small project, we will need:

1 RGB LED 10 mm

3 resistors 270 Ω (red, purple, brown stripes). You can use a resistor with a resistance of up to 1 kOhm, but do not forget that as the resistance increases, the LED begins to shine less brightly.


The six digits of the number correspond to three pairs of numbers; the first pair is the red component of the color, the next two digits are the green component, and the last pair is the blue component. That is, the expression #FF0000 corresponds to red, since this will be the maximum brightness of the red LED (FF is 255 in hexadecimal), and the red and blue components are equal to 0.

Try lighting an LED using, for example, an indigo shade: #4B0082.

The red, green and blue components of indigo are 4B, 00 and 82 respectively. We can use them within the "setColor" function with the following line of code:

setColor(0x4B, 0x0, 0x82); // indigo

For the three components, we use a notation that prefixes each with a leading "0x" character.

As you play with different shades of the RGB LED, don’t forget to set the ‘delay’ after using each one.

PWM and Arduino

Pulse width modulation (PWM in English) is one of the power management methods. In our case, PWM is used to control the brightness of each individual LED.

The figure below schematically shows the signal from one of the Arduino PWM pins.


Every 1/500 second the PWM output generates a pulse. The length of this pulse is controlled by the "analogWrite" function. That is, "analogWrite(0)" will not generate any pulse, but "analogWrite(255)" will generate a signal that will last until the very beginning of the next one. That is, it will appear that one continuous pulse is being sent.

When we specify a value between 0 and 255 within the analogWrite function, we generate a pulse of a certain duration. If the pulse length is 5%, we will supply 5% of the maximum available power to the specified Arduino output and it will appear that the LED is not at maximum brightness.

Leave your comments, questions and share your personal experience below. In the discussion, new ideas and projects are often born!