Interfacing RGB LED With Arduino Nano
Interfacing RGB LED With Arduino Nano
Interfacing RGB LED With Arduino Nano
Nano
In this project, we'll explore how to use an RGB LED with an Arduino Nano
and make the LED glow in different colors.
● When all three LEDs are at full brightness, the result is white light.
● When only the red and green LEDs are on, the result is yellow light.
● When only the green and blue LEDs are on, the result is cyan light.
1. Arduino Nano 1
2. RGB LED ( common cathode ): 3 nos
3. Resistors(220 ohm) : 3 nos
4. Breadboard(full size) : 1
5. Jumper Wires : multiple
6. BC547 (NPN transistor) : 3 nos
Circuit Diagram
The RGB LED has four pins: one common anode (CA) and three cathodes for
the red, green, and blue LEDs. The common anode is connected to a 5V
power supply, and each cathode is connected to the collector of an NPN
transistor. The emitters of the transistors are connected to the ground. The
bases of the transistors are connected to Arduino pins D6, D5, and D3.
The Arduino Nano is powered by the 5V supply, with its 5V and GND pins
connected to the positive and negative rails of the breadboard, respectively.
The use of transistors allows the Arduino to control the higher current
needed by the RGB LED, while the resistors ensure that the base current of
the transistors is within safe limits.
This setup is a common method for RGB LED control in various electronics
projects, enabling a wide range of color mixing and lighting applications.
In the above schematics if you want you add more LED’s then you can add
more LEDs.
Two changes must be done in above circuit:
void setup() {
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
pinMode(BlueLed, OUTPUT);
void loop() {
Code Explanation
Pin Definitions:
Setup Function:
Loop Function:
● The loop function runs continuously, creating the color-mixing effect.
1. First Color Mixing Sequence (Green and Blue with varying Red):
○ Sets the Green LED to full brightness (255).
○ Two nested for loops iterate through index1 and index2 from 0
to 255.
○ Within the inner loop, the brightness of the Red LED (index1)
and the Blue LED (index2) are adjusted using analogWrite.
○ This creates a gradient effect where the Red and Blue LEDs
blend with the Green LED.
2. Second Color Mixing Sequence (Blue and Red with varying
Green):
○ Sets the Blue LED to full brightness (255).
○ The nested loops iterate again.
○ This time, the brightness of the Green LED (index1) and the
Red LED (index2) are adjusted.
○ This creates a gradient effect where the Green and Red LEDs
blend with the Blue LED.
3. Third Color Mixing Sequence (Red and Blue with varying Green):
○ Sets the Red LED to full brightness (255).
○ The nested loops iterate again.
○ This time, the brightness of the Green LED (index1) and the
Blue LED (index2) are adjusted.
○ This creates a gradient effect where the Green and Blue LEDs
blend with the Red LED.
Related Project
RGB LED Interfacing With Arduino UNO