Saturday, May 07, 2011

Blink with Delay: Let There Be Light!

It is only customary to start one's journey with their Arduino by opening the Blink example code, uploading it to your board, and staring mouth agape at your first beautiful creation.


Our first project consists of powering an LED with the Arduino and "blinking" it. The LED is connected to PIN 53 but can easily be moved to any of the other digital output pins. Since we don't want to burn out the LED, it needs a current limiting resistor to be placed in series with the Ground pin located also on the Arduino. The resistor I used for the circuit was 470 Ohms, but that's only because I couldn't find any 220 Ohm resistors. 

 Made with LTspice, use your imagination

You can calculate the minimum resistance value needed for any LED by taking the supply voltage (In this case 5V from the Arduino) and subtracting the voltage drop across the LED. You can find this value by checking the data sheet from the LED manufacturer. If you've got a mystery LED, it is safe to assume it is going to draw about 1.7-2.0 volts. Then, you divide by the typical current drawn by the LED. Once again, if you have a mystery LED, they normally draw about 20mA.

(5V - 1.7V) / 20mA = 165 Ohms

This is the minimum resistance you would want to use for your current limiting resistor. You can, of course, use a greater resistor value. The LED will receive less current though and will be a bit dimmer, but you probably won't notice and this way you don't have to worry about destroying the LED. Also, if you are powering the Arduino off of a battery pack, you may want to use a higher resistance anyways to increase the battery life of your project.

Here is the blink program I wrote based off of the sample code provided in the Arduino IDE. As the blog progresses I'm going to start assuming that you know how the basic functions work and start skimming through explinations. There will always be comments in the code that you can follow along with.

Noah Stahl
Arduino Mega 2560

This program will blink an LED on Pin 53 using
a hard coded delay of 1000ms.
unsigned int toggle = 1;      //Keep track of LED state

void setup(){
  pinMode(53, OUTPUT);        //Sets pin 53 as an output

void loop(){
  digitalWrite(53, toggle);   //Change LED state
  delay(1000);                //Wait 1000ms
  toggle = !toggle;           //Change toggle to opposite of its current value

You can download my updated Blink code from here:

First, Pin 53 is set as an output using the pinMode function in setup. The first number in the parenthesis corresponds to the pin that you want to set. If you don't have an Arduino Mega and don't have a pin 53, just change this number to one of your DIGITAL pins. Once the program moves into the loop, Pin 53's voltage output is set either ON or OFF depending on the value stored in the toggle variable. Next, the delay function is used to wait 1000ms (1 second). If we wanted to speed up the interval between blinks, we would just decrease the value passed into the delay function. And finally, the toggle variable's value is changed to the opposite of its current value.

Once you download, verify, and upload the sketch onto your Arduino you should see your LED blink at a one second interval.

This code, while very simple, is in fact a gigantic resource hog. The problem with using the delay() function is that the Arduino will do just that, wait. While the Arduino is stuck waiting, no other processes can take place. This would be very inconvenient for any project requiring something else to be happening while your Arduino sits there and counts. My next post will teach you how to get around this by using timers and interrupts.

Up Next: Timer2 Overflow Interrupt...

No comments:

Post a Comment