Arduino Nano - LED RGB
This tutorial instructs you how to use Arduino Nano to control RGB LED. In detail, we will learn:
- How RGB LED works.
- How to connect an RGB LED to a Arduino Nano.
- How to program Arduino Nano to control the color of the RGB LED.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
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
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
※ 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
This image is created using Fritzing. Click to enlarge image
- 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:
- You can pick up the desired color code from the color picker
- If you want to use a color from an image, use the online Colors From Image tool
- 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
- Specify the Arduino Nano pins that are connected to the R, G, and B pins. For example:
- Set the Arduino Nano pins to output mode:
- Control the LED so that it emits the color #00979D, which is composed of Red = 0, Green = 151, and 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)
When using many colors, we could shorten the Arduino Nano code by creating a function:
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.