How to Use an RGB LED Module With Arduino

Hw 479 Rgb Led Module On Arduino Feature Image

Perhaps one of the most common modules in an Arduino starter kit, the RGB LED lets you create all sorts of colors with just four pins from the Arduino. This tutorial demonstrates two ways to use this module, so read on to learn how well it works!

What Is an RGB LED Module?

An RGB LED module uses a special kind of LED light that can emit three colors: red, green, and blue. However, you can make any color you could possibly think of by changing which one is turned on and mixing up each one’s brightness.

Rgb Led

There are two ways to go about lighting an RGB LED module on any Arduino board. The usual method is to use the Arduino’s analog pins to send PWM signals to the module. This lets you mix each color, giving you full control over the RGB LED.

Meanwhile, the other (digital) option is to use pins 11, 12, 13, and GND by attaching the module to the Arduino as you would with a shield. This saves space by not requiring jumper wires for every pin, but it does exchange fine control over the LED’s hue and brightness. Furthermore, the setup only works if the module has pins that fit properly on the Arduino board, such as the HW-479 that we’re using in this guide.

How RGB LEDs Work

At this point, you may be wondering how a single RGB LED module emits three colors. You may be surprised to learn that an RGB LED is not really just one fancy LED but three smaller LEDs encased in a larger LED. These mini LEDs share the same grounding pin, which should be the longest of the four pins in an RGB LED.

Rgb Smd Led
Image source: Wikimedia Commons

If you have a bare RGB LED instead of a module in a breakout board, you can make something akin to a module by attaching resistors on each of the color pins. The resistance value varies per manufacturer, but 220Ω per leg is a safe bet.

Tip: learn how to power light bulbs using Arduino with a relay module.

What You’ll Need

If you’re ready to get started, you’ll need these items to get this project off the ground.

Hardware

  • HW-479 RGB LED Module
  • Jumper wires (if using the analog method)
  • Arduino

Software

How to Use an RGB LED Module Using the Digital Method

This method only works on Arduino boards with a GND pin next to pin 13. Examples are the Uno, Duemilanove, Mega, and Leonardo. If you have an unofficial Arduino-based microcontroller board, make sure that it’s using female pin headers. RGB LED modules are typically in male headers.

Preparing the Code

  1. First, define the pins: 11 for blue, 12 for green, and 13 for red.
const int blueLED = 11;
const int greenLED = 12;
const int redLED = 13;
  1. On setup(), define these pins as output pins.
void setup() {
  pinMode(blueLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
}
  1. On loop(), add power to one pin, pause the code for 1000 milliseconds (1 second), then turn on the next one until all pins are powered. Finally, turn them off one by one.
void loop() {
  digitalWrite(blueLED, HIGH);
  delay(1000);
  digitalWrite(greenLED, HIGH);
  delay(1000);
  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(blueLED, LOW);
  delay(1000);
  digitalWrite(greenLED, LOW);
  delay(1000);
  digitalWrite(redLED, LOW);
  delay(1000);
}

This is the full version of the code:

const int blueLED = 11;
const int greenLED = 12;
const int redLED = 13;
 
void setup() {
  pinMode(blueLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
}
 
void loop() {
  digitalWrite(blueLED, HIGH);
  delay(1000);
  digitalWrite(greenLED, HIGH);
  delay(1000);
  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(blueLED, LOW);
  delay(1000);
  digitalWrite(greenLED, LOW);
  delay(1000);
  digitalWrite(redLED, LOW);
  delay(1000);
}

Building the Circuit

The circuit is simple. Point the “-” pin on the RGB LED module to the Arduino’s GND pin. If you align it right, the other three pins should enter pins 13, 12, and 11.

Hw 479 Rgb Led Module Directly Mounted On Arduino Uno

Making It Work

Once you’ve uploaded the code to the Arduino, you should see the colors on the RGB LED change every second.

You can edit the code and make it show two or all colors at the same time by turning them to HIGH together before the delay() function, like this:

digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(1000);

How to Use an RGB LED Module Using the PWM Method

This method gives finer control compared to the previous method. The color of the LED depends on the ratio of duty cycles between pins 9, 10, and 11. The greater the ratio toward one color, the stronger it becomes in the mix.

Also, increasing the duty cycle boosts the LED’s brightness. If you increase all of the duty cycles while keeping the same ratio, you’ll end up with a very bright RGB LED.

Preparing the Code

  1. Define the pins: 9 for blue, 10 for green, and 11 for red.
const int blueLED = 9;
const int greenLED = 10;
const int redLED = 11;
  1. Turn these into output pins.
void setup() {
  pinMode(blueLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
}
  1. On loop(), use the analogWrite() function instead of digitalWrite() to control the pins’ duty cycles. The process is similar to using PWM to control servo motors with a Raspberry Pi, except that we’re using C++ instead of Python.
void loop() {
  analogWrite(greenLED, 25);
  analogWrite(blueLED, 50);
  delay(250);
  analogWrite(greenLED, 50);
  delay(250);
  analogWrite(greenLED, 100);
  delay(250);
  analogWrite(greenLED, 150);
  delay(250);
  analogWrite(greenLED, 125);
  delay(250);
  analogWrite(greenLED, 80);
  delay(250);
  analogWrite(greenLED, 40);
  delay(250);
  analogWrite(greenLED, 20);
  delay(250);
  analogWrite(greenLED, 0);
  delay(250);
}

Building the Circuit

Using jumper wires, connect pins B, G, and R to pins 9, 10, and 11. Connect the GND or pin to any of the Arduino’s GND pins.

Hw 479 Rgb Led Module Wired To Arduino 1

Making It Work

You may have noticed that we only used two colors this time: green and blue. That’s to make it easier to see the effects of changing the ratios on the RGB LED’s overall color. The LED looks deep blue and turns turquoise before climaxing into a greenish cyan, then fading back to a deep blue.

Also, you can combine this with the previous method by using pin 11 as an analog pin to control the brightness of the blue pin.

Want another fun project to try? Learn how to use a Raspberry Pi to make an Arduino Uno blink.

Frequently Asked Questions

What happens if I add an extra resistor on the RGB LED module?

RGB LED modules already have built-in resistors. If you add another, you’ll just make yours dimmer than it should have been while running on the same amount of voltage and current.

What makes an LED light up?

LEDs create light through a process called electroluminescence, a property of certain materials that lights up when affected by an electromagnetic field. They are made from gallium-based semiconductors that emit light instead of heat.

Why don't LEDs light up when wired the opposite way?

LEDs are diodes – hence, the name light-emitting diodes. All diodes let electricity pass through from one side but not from the other. Their cathode and anode parts are typically made from two different materials that, when pressed together, force electrons to spend more energy to go the other way, like a car travelin a steep uphill path.

Why do TVs use red LEDs?

Compared to other LEDs, red LEDs can brightly light up using less energy compared to other colored LEDs having the same size, which makes them great options as indicator LEDs when TV manufacturers plan for energy efficiency in their products.

All images by Terenz Jomar Dela Cruz.

Terenz Jomar Dela Cruz
Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox