Arduino Nano - LED RGB

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

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×RGB LED
3×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of RGB LED

The RGB LED is capable of producing any color by combining the three primary colors: red, green, and blue. It is constructed from three distinct LEDs (red, green, or blue) that are housed in a single case.

The RGB LED Pinout

An RGB LED has four pins:

  • Common (Cathode-) pin: must be connected to GND (0V)
  • R (red): pin is used for adjusting red
  • G (green): pin is used for adjusting green
  • B (blue): pin is used for adjusting blue
RGB LED pinout

To make an RGB LED work with Arduino Nano, we gotta have some current-limiting resistors. It can get pretty tricky to wire everything up. But, luckily, we can just use this cool RGB LED module that already has those resistors built-in!

RGB LED Module also includes four pins:

  • Common (Cathode-) pin: needs to be connected to GND (0V)
  • R (red): pin is used to control red
  • G (green): pin is used to control green
  • B (blue): pin is used to control blue
RGB LED Module Pinout

※ NOTE THAT:

The common pin of the RGB LED can either be a cathode or anode, depending on the type of RGB LED. This tutorial utilizes a common cathode one.

How it works

In physics, a color consists of three components: Red (R), Grean (G) and Blue (B). The range of each color value is from 0 to 255. The combination of three values creates a total of 256 x 256 x 256 colors.

We can use Arduino Nano to create any color we desire by supplying PWM signals (with a duty cycle ranging from 0 to 255) to the R, G, and B pins of an RGB LED.

The duty cycle of PWM signals sent to the R, G, and B pins are in proportion to the Red (R), Green (G), and Blue (B) color values respectively.

Wiring Diagram

  • Wiring diagram between Arduino Nano and RGB LED
The wiring diagram between Arduino Nano and RGB LED

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram between Arduino Nano and RGB LED module
The wiring diagram between Arduino Nano and RGB LED module

This image is created using Fritzing. Click to enlarge image

How To Control RGB LED

Let's learn how to control the GRB LED to any color, for example #00979D, step-by-step:

  • First, determine which color you want to display and get its color code. Tips:
  • Then, convert the color code to R, G, B values using the tool from w3school. Note down these values. In this case: R = 0, G = 151, B = 157
RGB LED color picker
  • Specify the Arduino Nano pins that are connected to the R, G, and B pins. For example:
const int PIN_RED = 11; const int PIN_GREEN = 10; const int PIN_BLUE = 9;
  • Set the Arduino Nano pins to output mode:
pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT);
  • Control the LED so that it emits the color #00979D, which is composed of Red = 0, Green = 151, and Blue = 157.
analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 151); analogWrite(PIN_BLUE, 157);

Arduino Nano - LED RGB Example Code

The Arduino Nano code below changes the color of the LED in a specific order:

  • #00C9CC (Red = 0, Green = 201, Blue = 204)
  • #F7788A (Red = 247, Green = 120, Blue = 138)
  • #34A853 (Red = 52, Green = 168, Blue = 83)
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-rgb */ const int PIN_RED = 11; const int PIN_GREEN = 10; const int PIN_BLUE = 9; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // color code #00C9CC (R = 0, G = 201, B = 204) analogWrite(PIN_RED, 0); analogWrite(PIN_GREEN, 201); analogWrite(PIN_BLUE, 204); delay(1000); // keep the color 1 second // color code #F7788A (R = 247, G = 120, B = 138) analogWrite(PIN_RED, 247); analogWrite(PIN_GREEN, 120); analogWrite(PIN_BLUE, 138); delay(1000); // keep the color 1 second // color code #34A853 (R = 52, G = 168, B = 83) analogWrite(PIN_RED, 52); analogWrite(PIN_GREEN, 168); analogWrite(PIN_BLUE, 83); delay(1000); // keep the color 1 second }

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

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-led-rgb */ const int PIN_RED = 11; const int PIN_GREEN = 10; const int PIN_BLUE = 9; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // color code #00C9CC (R = 0, G = 201, B = 204) setColor(0, 201, 204); delay(1000); // keep the color 1 second // color code #F7788A (R = 247, G = 120, B = 138) setColor(247, 120, 138); delay(1000); // keep the color 1 second // color code #34A853 (R = 52, G = 168, B = 83) setColor(52, 168, 83); delay(1000); // keep the color 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); analogWrite(PIN_GREEN, G); analogWrite(PIN_BLUE, B); }

Addtional Knowledge

For RGB LED with common Anode, you need to:

  • Connect the common pin to 3.3V of Arduino Nano.
  • Utilize analogWrite() function with R, G and B values as 255 minus the desired value for each color, respectively.

A sequence of RGB LEDs connected together creates the RGB LED Strip. LED strips can be divided into addressable LED strips and non-addressable LED strips. We will be creating tutorials for both types of LED strips.

※ NOTE THAT:

Do not use a single resistor in the common pin of an RGB LED instead of three resistors in the other pins.

This is because, although in theory it is acceptable to use a single resistor in the common pin, in reality this is not the case. The LEDs in an RGB package are not identical, meaning that the resistors for each LED will be different. This will cause an unequal distribution of current, resulting in different brightness levels and potentially damaging one or more of the LEDs, and eventually the other LEDs.