Wednesday, May 16, 2012

Simple Labs' Quick Start Kit for Arduino - LED Interfacing - How To?

The world of LEDS

LEDs (Light Emitting Diodes) are the most commonly used of electronic components. They are everywhere – torches, displays, indicators, etc. Every project will eventually end up having atleast one led. The Official Arduino Boards and most of the Clones do come with an on-board LED which you can try with the default blink program found in the File->Examples->Basic menu of Arduino IDE.

So How to Connect a LED?

The Long of the LED is the Anode (Positive Terminal!) and the short led is the Cathode(Negative Terminal). 
The Terminals of the LED
Place the LED as shown
Place a Current Limiting Resistor (too much current passing through an LED can burn it!) between the negative terminal of the LED and the '-'ve terminal on the power rail (we will soon connect this to the '-'ve (aka ground) of our Arduino Board!)
Take a wire from the pin marked 'GND' of your arduino and connect it to the '-'ve power rail  of the breadboard. Now all points of the '-' ve power rail will be connected to the ground of the arduino!
Next, Connect a wire between the 11th pin of the arduino(yes the 11th pin and not the 13th pin!)  and the  positive terminal of the LED. This is going to be our control line for the LED
Programming to control the LED
So how can you control a LED? Well there are only 2 ways to control an LED. you can either switch it ON / OFF or you can control the intensity with which it glows (Very much like a fan!). So lets see how to do the first control - ON/OFF (we can call this digital control! Very much 0s & 1s)

Try the following code. [Blink.ino]
 /*  
  Blink  
  Turns on an LED on for one second, then off for one second, repeatedly.  
  This example code is in the public domain.  
  */  
 void setup() {          
  // initialize the digital pin as an output.  
  // We have our LED connected to Pin 11  
  pinMode(11, OUTPUT);     // Sets the 11th pin as an Output pin
 }  
 void loop() {  
  digitalWrite(11, HIGH);  // set the LED on  
  delay(1000);       // wait for a second  
  digitalWrite(11, LOW);  // set the LED off  
  delay(1000);       // wait for a second  
 } 

Next, Lets see how to control the intensity of the LED.

The Intensity can be varied by controlling the voltage applied to the individual pins. If you take a look at the 11th PIN on the arduino, you would see a marking 'PWM' next to it (Remember we connected our LED to the 11th pin). The PWM pins in addition to generating digital HIGH / LOW signals can generate analog voltages between 0 & 5.

The PWM Pins [Pins 3,5,6,9,10,11] that can generate a PWM signal of 8-bit resolution.[8-bits can represent a maximum value of 255, and a 8-bit resolution here means that 5 volts is represented by 255 divisions. So if you want to generate 1 volt, you would use the value 51]

The analogWrite function will take a 8-bit numerical value as a parameter [called duty cycle] and produce an output voltage corresponding to this value. It will set the pin to generate a steady square wave of the specified duty cycle at roughly 490Hz frequency.

Finally, when using a pin in the PWM mode, we don't have to use the pinMode() function.

Try the following code with the same setup and see how the intensity increases and decreases...[Intensity.ino]
 /*  
  Intensity 
  Increases the Intensity of a LED from 0 to maximum and on reaching maximum
  starts decreasing back to 0
  */  
  
 int intensity = 0;
 
 void setup() {          

 }  
 void loop() {  
   
   while(intensity < 255) // Check if intensity has reached maximum value, if yes then exit the loop
   {
     analogWrite(11,intensity);
     delay(25);
     intensity++;
   }
   
   while(intensity > 0) // Check if intensity has reached minimum value, if yes then exit the loop
   {
     analogWrite(11,intensity);
     delay(25);
     intensity--;
   }
     
 } 

8 comments:

  1. if i connect more than two led in series mode ,the led is not blinking.in paralled 3 led is working. why so ?

    ReplyDelete
  2. Hi Mohammed,

    In parallel the current across all the LED's will remain same so it glowing when in series current reduces from LED to another so LED will not glow.

    ReplyDelete
  3. I have just order the quick starter kit. I dot't have any power supply. What is the power source used here.

    ReplyDelete
  4. I am confused about placing resistor to cathode or anode of an LED. Web has discussions saying it doesn't matter. I am not satisfied as a beginner. Can you clarify or point to me to clear my confusion please? I appreciate your support.

    ReplyDelete
    Replies
    1. resistor should be placed before anode.. This is because we have to limit the current through the led.. That is the actual use of resistor.

      Delete
  5. once the program has started working how to stop it????

    ReplyDelete
  6. Why is it that the longer end should go into the anode and vice-versa for the shorter end?

    ReplyDelete