Learn to code

ARDUINO & CODES

Previous Post
Adjust the brightness of an LED with PWM for Arduino
Next Post
Control the sequence LED with a potentiometer and Arduino

Adjusting the color of a RGB LED

in Arduino Visual Output

In this tutorial, you will learn how to use an RGB ( Red, Green, Blue) LED with an Arduino. The RGB LED can emit various colors depending on mixing the 3 basic colors red, green and blue. If you look at the RGB, it looks like a normal LED. However, inside the LED, there are actually three LEDs. One for red, one for green and one for blue. In other words, the RGB consists of 3 pins, one pin for each of the colors, and one common cathode. We will mix colors by adjusting the brightness of each of the three LEDs. The analogWrite() function will help us with that. For more information about analogWrite() please visit the tutorial: In the 4.7 Reading analog values

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 4
RGB LED RGB LED × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

RGB Colors

We can control the brightness of the red, green and blue parts of the RGB LED separately, making it possible to mix any color we would like. If we would set all the three parts the be the same, the overall color of the light will be white. For instance, if we turn off the blue part, and the red and green parts are the same brightness, then the light that appears from the LED will be yellow. Since black is not really a color, the closest we can come to black with our LED is to turn off all the three colors.

RGB Color Arduino

BreadBoard Layout

The cathode will be connected to the ground while the 3 pins lead for the colors will be connected through 220 Ohm resistors to 3 pins on the Arduino that provide PWM signal. The PWM signal will be used to set different voltage levels to the LEDs to get the desired colors.

The Code

// RGB LED sketch
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
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);
}
void setColor(int red, int green, int blue)
{
  //use PWM to controlt the brightness of the RGB LED
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

Code Explanation

The code above is quite easy to understand if you have read the previous tutorials. First, we need to define the pins that are connected to the RGB. We do this by creating three integer variables for the pins that are connected to the LED. Remember, the pins should be able to provide an analog signal.

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

 

In the setup() part of the sketch we declare that our pins have an output mode.

pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

 

So, in the void loop() part of the sketch we will make a function that changes the color of the LED each second. Before we take a look at the ‘loop’ function, lets look at the last function in the sketch. The function void setColor() takes three arguments, one for the brightness of the red, green and blue parts of our LED. In each case, the number will be between 0 and 255. The function than uses analogWrite() to set the brightness of the LED.

 
void setColor(int red, int green, int blue)
{
  //use PWM to controlt the brightness of the RGB LED
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

 

If you look at the ‘loop’ function you can see that we are setting the amount of red, green and blue light that we want to display and then pausing for a second before moving on to the next color.

{
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);
}

 

You can try adding different colors to the sketch to the effect. Below you can see how to use other colors and implement them into your sketch.

Using other colors

If you would like to use other colors you can use hex numbers. Colors are often presented as a ‘hex number’. For example, the color red has the number #FF0000. You can find the numbers associated with the color on:

https://htmlcolorcodes.com/color-names/

The six digits of the color number are actually three pairs of numbers. The first part is the red component, the second the green, and the third the blue part.

We can use this knowledge to transform the hex numbers into readable data so that it is displayed on the RGB LED.

Let’s try making the color indigo (#4B0082).

The red, green and blue parts are respectively: 4B, 00 and 82. We can put this into the setColor() function of our sketch by doing this:

setColor(0x4B, 0x0, 0x82);  // indigo

 

We have used hex numbers for the three parts of the color by putting ‘0x’ in front of them.

Try adding a few colors of your own to the ‘loop’ function. Remember, do not forget to put a delay after them. Otherwise, you will not see the color.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu