Arduino UNO R4 - RGB LED

This tutorial instructs you how to use Arduino to control the RGB LED. In detail, we will learn:

Arduino UNO R4 RGB LED

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×RGB LED Module
1×(Alternative) RGB LED
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor 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

The RGB LED can create any color by mixing the three primary colors: red, green, and blue. It contains three separate LEDs: one red, one green, and one blue. All are housed together in one unit.

Pinout

An RGB LED has four pins.

  • Connect the Common (Cathode-) pin to GND (0V).
  • The R (red) pin controls the red color.
  • The G (green) pin controls the green color.
  • The B (blue) pin controls the blue color.
RGB LED Pinout

To connect an RGB LED to an Arduino UNO R4, we should use resistors that limit the current, which makes the setup a bit complicated. However, we can use the RGB LED module which has these resistors built-in already.

The RGB LED module has four pins too.

  • Common (Cathode-) pin should be connected to GND (0V).
  • R (red) pin controls the red color.
  • G (green) pin controls the green color.
  • B (blue) pin controls the blue color.
RGB LED Module Pinout

※ NOTE THAT:

This tutorial uses an RGB LED with a common cathode. This means the common pin is the cathode. Different RGB LEDs might have the common pin as the anode.

How it works

In physics, a color is made up of three values: Red (R), Green (G), and Blue (B). Each value can be from 0 to 255.

⇒ There are a total of 256 x 256 x 256 colors created by combining three different values.

⇒ If we send PWM signals (with a duty cycle between 0 and 255) to the R, G, and B pins, we can make the RGB LED display any color we choose. The duty cycle of PWM signals to the R, G, and B pins matches the color values of Red (R), Green (G), and Blue (B).

Wiring Diagram

  • Wiring diagram between Arduino UNO R4 to an RGB LED.
The wiring diagram between Arduino UNO R4 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, use three separate resistors on the other pins as above diagram. Different LEDs in the RGB package do not share the exact same features, which means they do not equally share current. This can cause uneven brightness and potentially damage the LEDs if using a single resistor on the common pin.

  • Wiring diagram between Arduino UNO R4 to an RGB LED module
The wiring diagram between Arduino UNO R4 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 change the RGB LED to any color, for example, the color #00979D.

  • Choose the color you want to use and find its color code.
  • Convert the color code to RGB values using this tool. Remember these values: R = 0, G = 151, B = 157.
RGB LED color picker
  • Define the Arduino UNO R4 pins that connect to the R, G, and B pins. For instance:
#define PIN_RED 9 // Arduino UNO R4 pin connected to the LED's red pin #define PIN_GREEN 6 // Arduino UNO R4 pin connected to the LED's green pin #define PIN_BLUE 3 // Arduino UNO R4 pin connected to the LED's blue pin
  • Configure these Arduino UNO R4 pins as outputs.
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Program Arduino pin to generate PWM signal to show the color (#00979D; R = 0, G = 151, B = 157).
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

Arduino UNO R4 - RGB LED Example Code

The code below changes the LED color in this order:

  • #00C9CC (R = 0, G = 201, B = 204)
  • #F7788A (R = 247, G = 120, B = 138)
  • #34A853 (R = 52, G = 168, B = 83)
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-rgb-led */ #define PIN_RED 9 // Arduino UNO R4 pin connected to the LED's red pin #define PIN_GREEN 6 // Arduino UNO R4 pin connected to the LED's green pin #define PIN_BLUE 3 // Arduino UNO R4 pin connected to the LED's blue pin void setup() { pinMode(PIN_RED, OUTPUT); // Set red LED pin as an output pinMode(PIN_GREEN, OUTPUT); // Set green LED pin as an output pinMode(PIN_BLUE, OUTPUT); // Set blue LED pin as an output } void loop() { // Set RGB LED to teal color #00C9CC (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 // Set RGB LED to salmon pink #F7788A (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 // Set RGB LED to Google green #34A853 (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 UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-rgb-led */ #define PIN_RED 9 // Arduino UNO R4 pin connected to the LED's red pin #define PIN_GREEN 6 // Arduino UNO R4 pin connected to the LED's green pin #define PIN_BLUE 3 // Arduino UNO R4 pin connected to the LED's blue pin void setup() { pinMode(PIN_RED, OUTPUT); // Set RED LED pin as an output pinMode(PIN_GREEN, OUTPUT); // Set GREEN LED pin as an output pinMode(PIN_BLUE, OUTPUT); // Set BLUE LED pin as an output } void loop() { // Set RGB color to teal setColor(0, 201, 204); delay(1000); // Wait for 1 second // Set RGB color to soft red setColor(247, 120, 138); delay(1000); // Wait for 1 second // Set RGB color to green setColor(52, 168, 83); delay(1000); // Wait for 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); // Write RED value to RED LED analogWrite(PIN_GREEN, G); // Write GREEN value to GREEN LED analogWrite(PIN_BLUE, B); // Write BLUE value to BLUE to LED }

Addtional Knowledge

  • To set up an RGB LED with a common Anode:
    • Connect the common pin to the 3.3V pin on the Arduino UNO R4.
    • In the analogWrite() function, adjust the R, G, and B values to 255 - R, 255 - G, and 255 - B, respectively.
  • A series of RGB LEDs connected together forms 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!