How to Control RGB LEDs With Arduino

by Lisleapex Blog in Circuits > Arduino

80 Views, 1 Favorites, 0 Comments

How to Control RGB LEDs With Arduino

How to Control RGB LEDs with Arduino.png

In this guide, you will learn how to control an RGB LED using an Arduino. An RGB (Red-Green-Blue) LED can produce a variety of colors by mixing different intensities of red, green, and blue light. You will learn to create a basic Arduino RGB LED circuit and cycle through some basic colors as examples.


Using the provided schematic and breadboard images, along with the example code below, you should have everything you need to easily setup and control the color output of an RGB LED.

Supplies

Arduino Uno


Breadboard (and some breadboard wires)


3x resistors (220Ω)


RGB LEDs

Connect an RGB LED to an Arduino

circuit.png
using common anode LEDs.png

Here is the schematic for the circuit. This diagram uses three resistors and a common anode RGB LED (you'll find the schematic for common cathode below).


If you are using common anode LEDs, you will need to connect the common anode pin to 5V like this:


Steps to Connect the Circuit on a Breadboard

Steps to connect the circuit on a breadboard1.png
Steps to connect the circuit on a breadboard2.png

If you are using a common cathode RGB LED, connect the cathode to the GND pin on the Arduino. If your RGB LED is common anode, connect the anode to the 5V pin on the Arduino.


Connect the red, green, and blue legs of the LED to pins 11, 10, and 9 of the Arduino respectively, each through a 220 ohm resistor.


Make sure your Arduino is connected to your computer via a USB cable.


Use one of the breadboard images below as a visual guide for setting up the connections.


Common cathode RGB LED connected to Arduino.

Upload the Arduino RGBLED Code

Upload the code below to your Arduino using the ArduinoIDE and you should see the LED cycle through different colors, stopping for one second for each color.


Full Arduino code for RGBLED (common cathode):


int redPin= 11;

int greenPin = 10;

int bluePin = 9;


void setup() {

 pinMode(redPin, OUTPUT);

 pinMode(greenPin, OUTPUT);

 pinMode(bluePin, OUTPUT);

}


void loop() {

 setColor(255, 0, 0); // Red Color

 delay(1000);

  

 setColor(0, 255, 0); // Green Color

 delay(1000);

  

 setColor(0, 0, 255); // Blue Color

 delay(1000);

  

 setColor(255, 255, 0); // Yellow Color

 delay(1000);


 setColor(0, 255, 255); // Cyan Color

 delay(1000);

  

 setColor(255, 0, 255); // Magenta Color

 delay(1000);

  

 setColor(255, 165, 0); // Orange Color

 delay(1000);

  

 setColor(128, 0, 128); // Purple Color

 delay(1000);

  

 setColor(255, 255, 255); // White Color

 delay(1000);

}


void setColor(int redValue, int greenValue, int blueValue) {

 analogWrite(redPin, redValue);

 analogWrite(greenPin, greenValue);

 analogWrite(bluePin, blueValue);

}


If you are using a common anode RGBLED, the logic is reversed compared to a common cathode RGBLED. In a common cathode RGBLED, you provide power (HIGH) to a specific pin to turn on a color. For a common anode RGBLED, you connect a specific pin to ground (LOW) to turn on a color.


So, with a common anode RGBLED, to set the color, you need to subtract each color value from the maximum value (i.e. 255) before applying it. This inversion ensures that a value of 255 (full intensity) for a specific color will cause that color to be turned off, while a value of 0 (no intensity) will cause that color to be fully turned on.

Complete Arduino Code for RGBLED (Common Anode)

int redPin= 11;

int greenPin = 10;

int bluePin = 9;


void setup() {

 pinMode(redPin, OUTPUT);

 pinMode(greenPin, OUTPUT);

 pinMode(bluePin, OUTPUT);

}


void loop() {

 setColor(255, 0, 0); // Red Color

 delay(1000);

  

 setColor(0, 255, 0); // Green Color

 delay(1000);

  

 setColor(0, 0, 255); // Blue Color

 delay(1000);

  

 setColor(255, 255, 0); // Yellow Color

 delay(1000);


 setColor(0, 255, 255); // Cyan Color

 delay(1000);

  

 setColor(255, 0, 255); // Magenta Color

 delay(1000);

  

 setColor(255, 165, 0); // Orange Color

 delay(1000);

  

 setColor(128, 0, 128); // Purple Color

 delay(1000);

  

 setColor(255, 255, 255); // White Color

 delay(1000);

}


void setColor(int redValue, int greenValue, int blueValue) {

 analogWrite(redPin, 255 - redValue);

 analogWrite(greenPin, 255 - greenValue);

 analogWrite(bluePin, 255 - blueValue);

}


How the Code Works

This code first sets up the RGBLED pins via the variables redPin, greenPin, and bluePin. Change these if you are using different pins than in the example circuit.


Then the code lights up the RGBLED in red, green, blue, yellow, cyan, magenta, orange, purple, and white, pausing for one second on each color.

Conclusion

Try different color combinations by changing the values ​​in the function! Remember that an RGB LED combines red, green, and blue light to produce a variety of colors. You can now create a colorful display using an Arduino and an RGB LED!