Arduino Mega - RGB LED

This guide shows you how to use Arduino to control an RGB LED. We will learn in detail:

Arduino Mega RGB LED

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×RGB LED Module
1×Alternatively, RGB LED
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of RGB LED

An RGB LED can make many colors by mixing red, green, and blue. It has three tiny LEDs: one red, one green, and one blue. They are all in one small unit.

Pinout

A red-green-blue LED has four pins.

  • Connect the common pin to ground (0 volts).
  • The R pin sets the red color.
  • The G pin sets the green color.
  • The B pin sets the blue color.
RGB LED Pinout

To connect an RGB LED to an Arduino Mega, you need resistors to limit the current, which can make the setup a bit complicated. But you can use the RGB LED module (LINK_MAIN_LED_RGB_MODULE), which already has these resistors built in.

The RGB LED module also has four pins.

  • The common pin (cathode) goes to ground (0V).
  • The R pin makes the red color.
  • The G pin makes the green color.
  • The B pin makes the blue color.
RGB LED Module Pinout

※ NOTE THAT:

In this tutorial we use an RGB LED with a common cathode. That means the shared pin is the cathode (the negative side). Some RGB LEDs have the common pin as the anode (the positive side).

How it works

In science, color has three numbers: Red (R), Green (G), and Blue (B). Each number is between 0 and 255.

There are 256 times 256 times 256 colors in total, created by combining three numbers.

If we send PWM signals (fast on/off pulses) to the red, green, and blue pins, the RGB LED can show any color we want. The length of time each signal stays on (the 0–255 value) sets how much red, green, and blue light is used.

Wiring Diagram

  • Wiring diagram for connecting an Arduino Mega to an RGB LED.
The wiring diagram between Arduino Mega RGB LED

This image is created using Fritzing. Click to enlarge image

Do not use one resistor on the common pin of an RGB LED. Instead, place three resistors—one on each color pin—as shown in the diagram above. The red, green, and blue parts of an RGB LED are not exactly the same, so they do not share current equally. This can cause uneven brightness and could damage the LEDs if you use a single resistor on the common pin.

  • Wiring diagram for Arduino Mega to an RGB LED module
The wiring diagram between Arduino Mega RGB LED module

This image is created using Fritzing. Click to enlarge image

How To Control RGB LED

Let's learn, step by step, how to set the RGB LED to any color, for example, the color #00979D.

  • Pick the color you want and find its color code.
    • You can get a color code from this color picker: color picker.
    • If you need a color from a photo, use this Colors From Image tool: Colors From Image.
  • Convert the color code to RGB values using this tool: this tool. Remember these values: R = 0, G = 151, B = 157.
RGB LED color picker
  • Choose the Arduino Mega pins that connect to the red, green, and blue pins. For example:
#define PIN_RED 9 // Digital pin on the Arduino Mega for the red LED #define PIN_GREEN 6 // Digital pin on the Arduino Mega for the green LED #define PIN_BLUE 3 // Digital pin on the Arduino Mega for the blue LED
  • Set these Arduino Mega pins to be outputs.
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Set an Arduino pin to output a PWM signal that shows the color #00979D (R = 0, G = 151, B = 157).
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

Arduino Mega - RGB LED Example Code

The code shown below changes the LED color in this order:

  • Color code #00C9CC, blue-green color. Red 0, Green 201, Blue 204.
  • Color code #F7788A, pinkish-red color. Red 247, Green 120, Blue 138.
  • Color code #34A853, green color. Red 52, Green 168, Blue 83.
/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-rgb-led */ #define PIN_RED 9 // Pin used to drive the red channel of the RGB LED #define PIN_GREEN 6 // Pin used to drive the green channel of the RGB LED #define PIN_BLUE 3 // Pin used to drive the blue channel of the RGB LED void setup() { pinMode(PIN_RED, OUTPUT); // Configure red channel as an output pinMode(PIN_GREEN, OUTPUT); // Configure green channel as an output pinMode(PIN_BLUE, OUTPUT); // Configure blue channel as an output } void loop() { // Display teal color (#00C9CC) on the RGB LED (R = 0, G = 201, B = 204) analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 201); analogWrite(PIN_BLUE, 204); delay(1000); // Maintain color for 1 second // Show salmon pink color (#F7788A) on the RGB LED (R = 247, G = 120, B = 138) analogWrite(PIN_RED, 247); analogWrite(PIN_GREEN, 120); analogWrite(PIN_BLUE, 138); delay(1000); // Maintain color for 1 second // Show Google green color (#34A853) on the RGB LED (R = 52, G = 168, B = 83) analogWrite(PIN_RED, 52); analogWrite(PIN_GREEN, 168); analogWrite(PIN_BLUE, 83); delay(1000); // Maintain color for 1 second }

When using many colors, we could shorten the code by creating a function:

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-rgb-led */ #define PIN_RED 9 // Pin number for the red channel on the LED, mapped to Arduino Mega #define PIN_GREEN 6 // Pin number for the green channel on the LED, mapped to Arduino Mega #define PIN_BLUE 3 // Pin number for the blue channel on the LED, mapped to Arduino Mega void setup() { pinMode(PIN_RED, OUTPUT); // Configure red channel pin as an output pinMode(PIN_GREEN, OUTPUT); // Configure green channel pin as an output pinMode(PIN_BLUE, OUTPUT); // Configure blue channel pin as an output } void loop() { // Choose teal as the active color setColor(0, 201, 204); delay(1000); // Pause for 1 second // Choose soft red as the active color setColor(247, 120, 138); delay(1000); // Pause for 1 second // Choose green as the active color setColor(52, 168, 83); delay(1000); // Pause for 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); // Output red intensity value to the red LED pin analogWrite(PIN_GREEN, G); // Output green intensity value to the green LED pin analogWrite(PIN_BLUE, B); // Output blue intensity value to the blue LED pin }

Addtional Knowledge

  • To set up an RGB LED with a common anode:
    • Connect the common pin to the 3.3V pin on the Arduino Mega.
    • In the analogWrite() function, set the red, green, and blue values to 255 - R, 255 - G, and 255 - B.
  • A group of RGB LEDs connected together makes an RGB LED strip. There are two types of LED strips: addressable and non-addressable. We will provide tutorials for each type.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!