Archive for May, 2012

Arduino 101 – Blinking LED

HI….If you are reading this page, i assume that you either have an Arduino microcontroller board or are looking to get one. Well ..for those of you who are new to microcontrollers and are looking to get ones board for themselves, let me tell you that Arduino is the perfect way to begin. It uses the Atmega microcontroller and is very easy to use. Also there are tons of Arduino projects posted on the internet which you can and should check out on your time.

Ok…so lets begin..Some knowledge of any of the programming languages out there would make things easier for you to understand but its not the end of the world if you don’t.

In this post i ll tell you how to get an LED blinking with the help of your Arduino board.

In  order to do this you ll require the following items:

  • An Arduino board.
  • USB cable
  • Resistor
  • LED
  • Some single strand wires
  • Bread board

The circuit for this implementation is quite simple.

Connect the pin3 to the anode of the led. The cathode ( the leg towards the blunt side when you look from the top ) is connected to one end of the resistor. The other end of the resistor is connected to the GND pin on the board.

Now that you have all the components, the next step is to download the arduino software from this link:

http://arduino.cc/hu/Main/Software

Once you are done with it, start the application. The interface is really simple to understand. There is a text editor, where you type in your code, and a few button above that let you verify and upload your program to the board.

The following figure shows you the code that we have used here.

Lets go through the code line by line.

int led =3;

This line declares an integer variable led and we assign to it the digital pin number that we are going to use. (In this case the 3rd digital pin)

void setup(){

pinMode(led, OUTPUT);

}

This “setup” is a function/method which runs, only once, whenever you load a new program onto your board or whenever you “power on” the board before moving to the loop() method. Also it returns nothing (“void”). In this method we define all the stuff necessary for a program to run properly eg. configuring the pins as inputs/outputs, setting the baud rate etc. (Here in the setup() method we define our int variable “led” as an “Output” using the pinMode() method,  since  well…it is gonna be our output pin)

Next we move on to the loop() function.

This is where you write the code that you want your microcontroller to follow. The loop function repeats itself as long as the board has power (that s why the “loop”). As mentioned earlier the loop() function executes after the setup() function.

In this example we want our LED to blink continuously…so here s the code that makes it happen:

void loop (){

digitalWrite(led,HIGH);

delay(500);

digitalWrite(led,LOW);

delay(500);

}

Remember that the variable “led” is used to denote the 3rd digital pin here. So the first line tells the microcontroller to set the voltage on that pin to “High” value or 5V. This action forward biases the LED and it starts emitting light.

Next is the delay() statement. The argument to this function is the number of milliseconds it must wait before executing the next statement. Here the microcontroller waits for 500 ms before setting the voltage of the pin to “Low” or 0V which turns off the diode. This is followed by another delay of 500ms.

And then it again goes to digitalWrite(led, HIGH); And so the cycle continues.

Once we are done typing in the code, we ll need to verify it. This is done by clicking on the button which has a tick mark on it (refer to the figure). If everything s all right we need to upload the code to the board, but before doing that make sure that you have selected the correct board and the port. In order to check which COM port the board is using go to Start -> Right click on Computer ->  manage -> Device manager. You will see “COM” in the list. See the one that your board is using and then in the Arduino window go to Tools and select your board and the COM port that you just verified.

Now for the last step….click on the Upload button (the one with the arrow, next to the verify button).

The result is … we have an LED that stays on for half a second and turns off for the next half repeatedly…hence a blinking LED.

, , , ,

Leave a comment