Sunday 1 May 2016

Arduino Tutorial # 4 - Driving RBG LED

Introduction



In this tutorial we will discuss how to use RGB LED with an Arduino. RGB (REd,Green,Blue) LED looks the same as regular LED but there three LEDs, Red, Green and Blue. By controlling the brightness of the each individual LED we can  make any color we want.


The human eye has three types of light receptors in it.(red,green,blue) our brain process the amounts of red, green and blue coming from REG LED and convert into a color of the spectrum.

If we set all the LED's brightness to same than overoll color will e white. To get yellow we have to turn off the blue LED and red and green needs LED's need to set same brightness.

This way by mixing the brightness of the individual led we can create any color.




RGB - LED


RGB LED's have four pins and there are two type of RGB LED's. Common anode and common cathode.




Common Cathode 
All the cathodes (negatives) are connected together in common cathode RGB (D1) and other three pins are anode (positive) pins of Red, Green and Blue. 

Common Anode 
All the anodes (positives) are connected together in common anode RGB (D2) and other three pins are  cathode (negative) pins of Red, Green and Blue.

Common pin is the longest pin of the LED and also it is the second pin from the flat side of the LED.




Parts you will need


1x Breadboard
1x Arduino Uno
1x RGB LED (I am using common Anode)
3x 330Ω Resistor
Jumper Wires

Let's Assemble the parts on a breadboard

Connect common anode pin (long pin) to Vcc (5v) of the Arduino
Connect all the cathode pins (red,green,blue) to arduino pin 9,10 and 11 though resistors.

"if you are using common cathode RGB connect the common cathode pin to 0v (GND) instead of 5V (Vcc)"




Let's see the Sketch

This sketch will cycle through the colors red, green, blue, yellow, purple, and aqua.


We have to define our variables at the begging for each pin (color) of the RGB

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




Next we have to write setup function to set the RGB pins as output.


void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}


In loop function we are setting the amount of red, green and blue light that we want to display and then delay it by one second before moving to next color.


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);
}


You may notice that we have use a setColor function in our loop. After the loop we are going to set that function


void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}



This function takes three arguments, one for the brightness of the red, green and blue LEDs. In each case the number will be in the range 0 to 255, where 0 means off and 255 means maximum brightness. The function then calls 'analogWrite' to set the brightness of each LED.





And that's all you can now upload the sketch to your board.


You can download the full sketch from here - Download



//Tutorial 3 RGB LED

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)
{

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}



Internet Colors


You can use internet colors ('hex' number) also in with this code.

For example the color red has the number hex number #FF0000. 

The first pair being the red component of the color (#ff) and the next two digits the green part (00) and the final pair the blue part (00). Red is #FF0000.


You can find the hex number to any corresponding color in this website - http://www.color-hex.com/


Let's try to make magenta - #FF00FF

We have write our sketch as below.

setColor(0xFF, 0x0, 0xFF); // magenta

You can change the colors or change the delays time and do experiment the sketch.


Check out my video 




No comments:

Post a Comment