Monday 16 May 2016

RGB LED Tutorials

In this blog I Will show you how to get start with RGB LED using Arduino.

Small Introduction:

RGB LED contain red, blue and green LED's. Blue and green combine to produce turquoise, green and red combine to produce yellow, red and blue combine to make violet, red, green and blue combine to make white. There are literally more than 16,000,000 possibilities with the more sophisticated controllers. With an RGB LED you'll be able to produce any colour glow your heart desires.Intresting isnt it! At first using an RGB LED seems quite complex, but it quite quickly becomes clear when you starts hand on over these types. In this blog i will show to how to begin with RGB leds with some basic codes and finally i will show you 5 different colour scheme using RGB Led with arduino with different codes.
Lets get Started!
Attention : I have used COMMON ANODE(+) type LED.

Look out the video First!!


Step -1: Part Lists

1. RGB LED-Common Anode type
2. Arduino UNO or Nano will also work 
3. Resistor- 100 ohm~Or near by Value
4. NPN Transistor-BC 547
5. Potentiometer 10K
6. ON/OFF Push Button
7. Connecting wires and leads

Step 2: Understand RGB and Circuit


When you will buy a RGB led it will be available in two configuration that is Common Anode And Common Cathode.So but it properly.

Code 1: Simple Blink code


Here we go with the first code. I have added a copy of code with this step. You have to review the code in order to understand the workind of led and in the starting of code you will see the PINS connection which connects to RGB led .In some case you may have to change the connection due to PWN Pins of the arduino Uno/Nano 

So at the beginning of the code please review the pin and connect accroding to them and then upload the code and finally you with the initial success of your RGB led. Simple isn't it!

Code:
void setup() {



// initialize digital pin 5,6,7 & 8 as an output.

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH); }
// the loop function runs over and over again forever  
// Here the as pin 8 is common anode
// As pins 5 and 6 are set to HIGH,the diodes connected to
//blue and green LED are in reverse biased condition
//therefore only the Red colour will be seen on the RGB LED
void loop()
{
digitalWrite(5, HIGH);

digitalWrite(6, HIGH);

digitalWrite(7, LOW);

digitalWrite(8, HIGH);

delay(500);

// Here the as pin 8 is common anode
// As pins 6 and 7 are set to HIGH,the diodes connected to
//blue and red LED are in reverse biased condition therefore
//only the Green colour will be seen on the RGB LED

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

delay(500);

// Here the as pin 8 is common anode
// As pins 5 and 7 are set to HIGH,the diodes connected to
//red and green LED are in reverse biased condition
//therefore only the Blue colour will be seen on the RGB LED

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

delay(500);

//Further mixture of colours can be produced by switching on any 2 colours at the same time

}

Code 2: Simple colour mixing

Now we are going to upload our next basic code that is colour mixing. In the beginning i have mentioned that how mixing of colour takes place. connect the pins of led according to code and then upload. You will find that how easy it is and how attractive the LED looks like when Mixing takes place.
Code:
void setup() 

{

// initialize digital pin 5,6,7 & 8 as an output.



pinMode(5, OUTPUT);



pinMode(6, OUTPUT);


pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

digitalWrite(5, HIGH);

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

digitalWrite(8, HIGH); 
}

void loop()
{
digitalWrite(5, HIGH);

digitalWrite(6, HIGH);

digitalWrite(7, LOW);     //RED ON

digitalWrite(8, HIGH);

delay(1000);
//


digitalWrite(5, LOW);     //GREEN ON

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

delay(1000);
//

digitalWrite(5, HIGH);

digitalWrite(6, LOW);     //BLUE ON

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

delay(1000);                                  // CYCLE ALL ON FOE 1 SEC
//


digitalWrite(5, LOW);

digitalWrite(6, LOW);     // GREEN BLUE ON

digitalWrite(7, HIGH);

digitalWrite(8, HIGH);

delay(1000);
//

digitalWrite(5, LOW);

digitalWrite(6, HIGH);

digitalWrite(7, LOW);     // GREEN RED ON

digitalWrite(8, HIGH);

delay(1000);
//

digitalWrite(5, HIGH);

digitalWrite(6, LOW);

digitalWrite(7, LOW);     //BLUE RED ON

digitalWrite(8, HIGH);

delay(1000);                                   // CYCLE ALL AND MIX
//
}

Code 3: Colour fading and mixing

Colour Fading depends on PWM. If you dont know what is all about PWM. Google it further, or i will be doing another blog on this in future.:)
 Now,With the increase in time LED goes brighter and then goes lighter and his is what all about duty cycle.

Code:
// RGB LED - Automatic Color Cycling

//





int redPin = 11;

int bluePin = 10;

int greenPin = 9;


int redIn = 0;
int greenIn = 1;
int blueIn = 2;

int redVal;
int greenVal;
int blueVal;

void setup()
{
  redVal = 255;
  greenVal = 255;
  blueVal = 255;
  update();
}

// This function updates the LED outputs.
void update()
{
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
}

// This function updates one of the color variables
// either getting brighter or getting dimmer.
// It also updates the outputs and delays for 10 milliseconds.
void color_morph(int* value, int get_brighter)
{
  for (int i = 0; i < 255; i++)
  {
    if (get_brighter)
      (*value)--;
    else
      (*value)++;
      
    update();
    delay(10);
  }
}

void loop()
{
  // start out at black (all off)
  color_morph(&redVal,   1); // transition to red
  color_morph(&greenVal, 1); // transition to yellow
  color_morph(&redVal,   0); // transition to green
  color_morph(&blueVal,  1); // transition to aqua
  color_morph(&redVal,   1); // transition to white
  color_morph(&greenVal, 0); // transition to violet
  color_morph(&redVal,   0); // transition to blue
  color_morph(&blueVal,  0); // transition to black (all off) ,off
}

Code 4: Colour mixing with freeze button

When we need to freeze a particular colour, we can also do that. i just added simple Push button to stablize the led in order to give a particular colour. For example if we want on green led to glow then, we have to wait for green colour to apper and at the same instant we have to press the button. In this way we can freeze any colour we want or depending on need.

Code:
// Description: This program is designed to blink each color of a RGB LED

// When the reading on the input pin is low. You can switch the delay to your

// convienience :)

//





#define red 12

#define blue 11
#define green 10
//Pin outputs for RGB LED

#define input 7
//Input pin for buttom

void setup()
{
  pinMode(red,OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(input,INPUT);
  return;
}

long unsigned delayCurrent=0, delayPrev=0;
const int Delay=1000;
int count=0;

void loop()
{
  delayCurrent=millis();
  
  if( (delayCurrent-delayPrev>Delay) && digitalRead(input)==LOW )
  {
    if(count==0 || count==1 || count==2)
      digitalWrite(red,HIGH);
     else
      digitalWrite(red,LOW);
    //RED if
    if( count==1 || count==2 || count==3 || count==5)
      digitalWrite(green,HIGH);
    else
      digitalWrite(green,LOW);
    //GREEN if
    if(count==2 || count==3 || count==4)
      digitalWrite(blue,HIGH);
    else
      digitalWrite(blue,LOW);
    //BLUE if
    count++;
    if(count>5)
      count=0;  
    delayPrev=delayCurrent;
  }

}

Code 5: Brightness control

To control the brightness of the LED just add a Potentiometer across the common Anode this will enable us to control the current flow to the positive terminal of the LED. Simple and Easy isn't is! Also the higher the resistance of potentiometer lighter the LED will glowor wide control.
Now you have enough knowledge to how to get start with some basic circuits of RGB LED using Arduino.
Note-: Just add Potentiometer on common anode terminal.
Now enough Learning. Now its your turn!!..:D
Hope you liked it.
Please share if you think it was awesome.
Don't forget to follow for more cool projects!!
Fell free to have any query.
Greetings. Stay Creative Keep tinkering!

2 comments:

Learn how UPS works with its repairing, circuit diagram and waveform

Hello guys. Have you ever wondered how UPS system works, that you can find easily besides you PC powering to it. Yes you are correct that cu...