Contribution, Developer, Student

HACKWARE102: BLINKING AN LED WITH AN ARDUINO

Hey guys, its kayode. Hope you have all been good? Last time, we talked about our arduino and all we needed to get coding and controlling the outside world.

In programming generally(C, C++, JAVA,HTML and so on), the first task every beginner must first accomplish is to display “HELLO WORLD” on the screen. Well it is similar in arduino programming just that you don’t get to display “HELLO WORLD” but here you get to turn an LED (Light Emitting Diode) on and off. So guys let’s get started. We are going to need the following;

  • Arduino Uno
  • A computer with the arduino IDE
  • A USB cable
  • 220R resistor
  • An led (Any color but preferably red)

BLINKING LED

const int LED=13;

void setup() {

// put your setup code here, to run once:

pinMode (LED,OUTPUT);

}

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(LED,HIGH);  // turn the LED on (HIGH is the voltage level)

delay(1000);                   //  wait for a second

digitalWrite(LED,LOW); // turn the LED off (OFF is the voltage level)

delay(1000);                  // wait for a second

}

Now we have to load our program from the computer to the arduino board. For you to load successfully you have to take the following steps;

Select the development board: I am using an Arduino Uno board. To choose the board, click Tools on menu bar. Choose the option Board and select your correct arduino board. I have chosen the arduino uno. See the screenshot below

 

Select the port: The port number is assigned while installing the hardware driver of board. You can find the port number by accessing device manager on Windows. See the section Port (COM & LPT) and look for an open port named “Arduino Uno (COMxx)“.  In my case, it’s COM37. So my port number is 37. To select the right port, go to Tools–> Serial Port and select the port number. View the screenshot below.

 

So after all this we are good to go. Next thing we are going to the is to verify our code. To verify involves compiling our code that is converting into 1’s and 0’s that our microcontroller understands. After compiling, then we burn the code to microcontroller using the upload button in the arduno IDE. Depending on the size of your program, this will take a little time. If you look on your arduino board, you can see the 2 LED’s named Tx and Rx blinking. This is an indication of successful communication between your computer and arduino board. If the program has been uploaded successfully, you will see a message “Done Uploading“.

Now lets break down the code.

First we have const int LED = 13;

This line of code assigns 13 to the variable LED. Here we declare a variable with name LED as a constant integer and at the same time we assign the variable to pin number 13. From now on, you can replace pin 13 with the variable name LED anywhere in the program.

The second line begins with void setup() and it has a block of statements written inside parentheses. Setup() is a function in arduino programming language used to configure microcontroller pins on our arduino uno either as  INPUT or  OUTPUT. When we configure as an INPUT, we are giving data to our microcontroller and an OUTPUT means the micro controller is giving out data. So we are going to send commands to turn LED ON and OFF to pin 13 of arduino board. We have to configure pin 13 as OUTPUT in our program. This is done in the void setup() block. We use a function named pinMode() to configure pins of arduino. To configure pin 13 as OUTPUT, we need to write the instruction pinMode(LED,OUTPUT); where LED is 13..

The setup() function is called when a program starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.

The next block begins with void loop() – here loop() is another predefined function in arduino programming language. This function executes all statements written inside its parentheses line by line consecutively; from first line inside parentheses to last line. Once the execution finishes last line, it will repeat the process of execution again beginning from first line in parentheses. In our case, the loop() function turns on our led on and off continuously as far as power is provided.

Our led is connected to pin 13. To turn it ON, we have to supply a voltage at pin 13. We are going to do that via software commands. We need to pass this voltage to pin 13 of arduino board. So we just need to write an instruction digitalWrite(LED,HIGH); and this instruction when executed by microcontroller will supply a +5 volts at pin 13. This voltage will power the led and it will turn ON. To turn off the led, we use the instructions digitalWrite(LED,LOW). This instruction will turn the led OFF. Now we have to create a delay between our ON and OFF time. We will use a function called delay() to do this task of setting time delay. All you need to do is write your desired delay in milli seconds as an argument in the delay function. To get a 1 second delay, we should write delay(1000);  and this will create a delay of 1000 milli seconds in between LED ON and OFF time.

 

Hurray, we have successfully done the hardware version of “HELLO WORD”. We have turn a led on and then off. You should be proud of your progress so far. You can change the delay time to see how the led reacts in every situation.

In our next tutorial we are going to learn how we can read the state of a push button and then use it to control a led. Till then, keep creating!!!

 

You Might Also Like

Let's hear your view