Sunday, 21 July 2013

Analog Outputs - What, Where & How? - Working with the RGB LED

Analog Outputs

Analog Outputs help us product a varying range of voltages between 0 & 5 volts. This is very much useful in achieving intensity control. We can use analog outputs to control the intensity of a LED or to control the speed of a motor.

The Induino R3 Board has 6 Analog output pins. These are pins 3, 5, 6, 9, 10 & 11 and are marked PWM.  The On-Board RGB LED is connected to Pins 3(Blue), 5(Green) & 6(RED).

Resolution


Analog signals (both Output & Input) can be a varying range of voltages between 0 & 5 Volts.. Try now to answer this question for a second: how many voltage values are there between 0 and 5 volts? The answer is simple: endless. Imagine two voltage values in the range we are working with, that are as close as possible, e.g. 3.4 and 3.41 volts. It is possible to have endless values inbetween those two: 3.401, 3.403, 3.402539 ...
This means that we would need a processor with the ability to represent endless numbers, but Arduino cannot.  Therefore, we speak about Resolution. Resolution defines the finest amount of change that is possible. In Arduino, the Analog Output resolution is 8-bits and the Analog Input resolution is 10-bits.

Lets take a closer look at Analog Ouput resolution. We have a 8-bit resolution - This means that we can use a 8 bit number to represent the voltage levels between 0 & 5. So we can use numbers between 0 and 255 to represent the voltage range of 0 to 5 volts.  In all we can step up the voltage from 0 to 5 in 255 steps or with each step we can increase (5/255) volts => 20 millivolts roughly.

Lets see how we can use this. Next Lets try to generate some random color using the RGB LED

analogWrite(pin number, 8-bit value) => the analogWrite() function writes the voltage value corresponding to the 8-bit value to the given pin number.

The Analog Pins (both INPUT & OUTPUT) do not require a pinMode() statement as they are separate ( Remember, in case of Digital Pins,  the same pin can be used as either Input or Output).


Next Lets try to generate some random color using the RGB LED

/*   Induino R3 User Guide - Program 4.1 - Simple RGB*/
 void setup()  
 {  // Leave the setup() function empty
 }  
 void loop()  
 {  
  analogWrite(3,153);// Setting the voltage for Blue to around 3 Volts  
  analogWrite(5,51);// Setting the voltage for Green to around 1 Volt  
  analogWrite(6,51);// Setting the voltage for Red to around 1 Volt  
 }  

Next, let's try and fade in / fade out the Red Color of the RGB LED.  We shall use 2 push buttons 7 & 9 for this. Here are some things to note, before we proceed.

byte - A byte is a variable type that uses 1 byte (8 bits!) of memory (lesser than a integer data type). A byte stores an 8-bit unsigned number, from 0 to 255.  Since we have a 8-bit resolution, it will be appropriate to use a byte variable type. (You can also use a integer variable without any change)

Here's our program for this
 /*   Induino R3 User Guide - Program 4.0 - Fade In / Fade Out of RED Color in RGB LED
using 2 Push Buttons on Pin 7 & 9*/

byte intensity = 0; // byte is a variable type that can hold 1 byte (8 bits) of data

void setup()  
{   
  pinMode(7,INPUT_PULLUP); // Increment Button
  pinMode(9,INPUT_PULLUP); // Decrement Button
}  
void loop()  
{  
  if(digitalRead(7)==0) // Check if the increment button is being pressed
  {
    if(intensity<255) // Check if the intensity is within permissible limits for incrementing
      intensity++; // increment intensity
    else
      intensity = 0; // if it has reached the limit, reset it to 0
    delay(20); // Note we are not waiting for the button to be released but only having a delay.
  } 
  if(digitalRead(9)==0) // Check if the decrement button is being pressed
  {
    if(intensity>0) // Check if the intensity is within permissible limits for decrementing
      intensity--; // decrement intensity
    else
      intensity = 255; // if it has reached the limit, reset it to 255
    delay(20); // Note we are not waiting for the button to be released but only having a delay.
  } 

  analogWrite(6,intensity); // write the intensity value to the Analog Pin 6 (RED)
}

Now let's try and make a bit more interesting user interface. We shall add a color selection button to the above program so that the user will be able to choose a color and then control its intensity. We shall also use the 3 LEDS on pins 11,12 & 13 to indicate to the user as to which color is being selected currently.

Here's our program for this - read through the comments and understand the program before trying to execute it.

/* Induino R3 User Guide - Program 4.2 - RGB LED COLOR CONTROL USING BUTTONS */

byte RGB[3];  // Variable to store individual Color Value of Different Colors of the RGB LED
int arr_ind=0; // Variable for Navigating through the Above Array - An Aray Index Variable

void setup()
{
  pinMode(7,INPUT_PULLUP); // The Increment Button
  pinMode(8,INPUT_PULLUP); // The Color Select Button
  pinMode(9,INPUT_PULLUP); // The Decrement Button

  pinMode(11,OUTPUT);//LED for Current Selected Color Indication for RED Color - Lights up When the User Selects RED Color
  pinMode(12,OUTPUT);//LED for Current Selected Color Indication for BLUE Color - Lights up When the User Selects BLUE Color
  pinMode(13,OUTPUT);//LED for Current Selected Color Indication for GREEN Color - Lights up When the User Selects GREEN Color

  RGB[0] = 0; // RGB[0] will store the value for the BLUE Color
  RGB[1] = 0; // RGB[1] will store the value for the RED Color
  RGB[2] = 0; // RGB[2] will store the value for the GREEN Color

  applyColor(); // Calling a Function that will handle the AnalogWrite functions for the RGB LED
}

void loop()
{
  if(digitalRead(7)==0) // Checking if the Increment button is Being Pressed, If True, the value of the currently selected color's value is incremented
  {
    if(RGB[arr_ind]<255) // Checks if the currently selected color value is lesser than 255 before incrementing. So when it reaches 255, the value is reset to 0.
      RGB[arr_ind]++;
    else
      RGB[arr_ind]=0;
    delay(100);
  }
  if(digitalRead(9)==0)// Checking if the Decrement button is Being Pressed, If True, the value of the currently selected color's value is decremented
  {
    if(RGB[arr_ind]>0)// Checks if the currently selected color value is greater than 0 before decrementing. So when it reaches 0, the value is reset to 255.
      RGB[arr_ind]--;
    else
      RGB[arr_ind]=255;
    delay(100);
  }
  if(digitalRead(8)==0)// Checking if the color button is Being Pressed, If True, the value of the array index is incremented to the next value
  {
    if(arr_ind<2)
      arr_ind++;
    else
      arr_ind=0;
    while(digitalRead(8)==0); // This while is used to debounce the button press or in other words, wait for the user to release the button
    delay(50);
  }
  if((digitalRead(7)==0)&&(digitalRead(9)==0))// Checking if both the increment & decrement buttons are being pressed at the same time. If so, all color values are reset to zero
  {
    RGB[0] = 0; // RGB[0] will store the value for the BLUE Color
    RGB[1] = 0; // RGB[1] will store the value for the RED Color
    RGB[2] = 0; // RGB[2] will store the value for the GREEN Color
    digitalWrite(11,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(13,HIGH);// This to indicate a reset in progress. All three LEDS GLOW for 200 milliseconds and go Off
    delay(200);
    digitalWrite(11,LOW);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
  }

  switch(arr_ind) // The switch is used to indicate the current color selection through the corresponding LED based on the current value of the Array Index
  {
  case 0: 
    digitalWrite(11,LOW);
    digitalWrite(12,HIGH);
    digitalWrite(13,LOW);
    break;
  case 1: 
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
    break;
  case 2: 
    digitalWrite(11,LOW);
    digitalWrite(12,LOW);
    digitalWrite(13,HIGH);
    break;
  }
  applyColor();// Calling a Function that will handle the AnalogWrite functions for the RGB LED

}

// The function applyColor() will apply the RGB array variable's current value to the Analog Pins 3,5 & 6 which control the RGB LED
void applyColor()
{
  analogWrite(3,RGB[0]);
  analogWrite(5,RGB[2]);
  analogWrite(6,RGB[1]);
}


Thats It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in

Visit www.simplelabs.co.in for more interesting products

Back to List of Contents

No comments:

Post a Comment