ARDUINO BLINK

Author: A.S.Vikram
      In this blog we are gonna have a very basic code that can blink an led on a arduino. This blog will go on from the basics to make even a noobie to understand what an arduino is.

Led blink using arduino

What is a Arduino?

             Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online and many more which i will be posting in the coming days.

How to get started?

           Getting started with an arduino is very simple. There are many types of arduino's available in the market, lets see some of the most commonly used arduino boards:
Arduino Nano

Arduino Mega 2560

Arduino UNO

Arduino Pro Mini


            Selecting the appropriate board as for your need is an important thing when getting started with arduino. A detailed article on how to select an arduino board is given here. Now all in hand whats left is to program the arduino board according to our need. To program the arduino boards a open source ide is available here. Download the software from the given link and install it in your pc. The arduino ide software is supported in windows, mac and linux systems.
          For  beginners ill recommend to use a arduino uno board.  Connect the arduino board to your pc using a proper usb cable and launch  the arduino ide software.

    Note for Windows users!
           To access the arduino board from an windows pc com port drivers should be installed. Each arduino board as a different usb to serial drivers. It is good to install both drivers in your pc for hassle free usage. The links for the drivers are given below,
    CP2102 usb to serial driver     -      driver1        
    CH340   usb to serial driver     -      driver2 

         Install both the drivers in your windows pc, reconnect the arduino board now you can see a new com port attached to the pc in the device manager. In linux and mac the driver is already pre installed.

    Components needed for arduino blink:


  1.  Arduino board
  2.  Bread board
  3.  A Led
  4.  220 Ohm resistor
  5.  Connecting wires


    Circuit diagram for arduino blink:



    Lets get started with the programming.....
      Start the arduino ide software in your pc with the arduino board connected. The first page you will get is,

    From this page goto File - Examples - Basics - Blink  now you will get a page like,

         
       You can even get the blink code from here which will be more easy for you to understand.

       Now lets go through the code step by step and analyze how it works....
                 
       The code can be divided into three sections which is known as program structure. They are 
  1. Variable declaration sction
  2. Setup section
  3. Loop section




1. Variable declaration section:
        
                 This section is used to declare variables which can be used in the setup and the loop section. This is not a mandatory but is useful when the code gets complex. The variables can be declared in many formats such as 
         
         In the above given code the int led = 13;  is used which declares a integer to a variable. The variable is the led and the variable holds the integer value as 13. Now each time when the variable led is used in the setup() or loop() section it refers to the value  13. 

     The other types of variables will be covered in the upcoming blogs with a brief description. Now lets move on to the setup setion.

2. Setup section:

   The setup section is nothing but a code which runs only once. The setup function follows the variable declaration section. The setup section is declared as void setup() followed by a open curly bracket '{' which declares the starting of the setup() function. Now the code which must be run in the setup is placed and at the end the setup is closed by using closed curly bracket '}'.
       
    Function which are declared in the setup function includes pinMode, Serial.begin(), declarations for library variables and many more.
       
        In the above code mentioned it has the code 
                              pinMode(led, OUTPUT);
        
         pinMode is used to define the mode of the pin used in the arduino board whether it is used as a OUTPUT or an INPUT, here it is declared as a output since it is gonna switch a load on and off. If you are connecting an external input to the arduino it should be declared as a INPUT. Thus now it is clear that the above line defines the 13th pin of the arduino as output.

3. Loop section:
     
            The code placed in the loop section runs continuously after the setup section is executed. Once the program reaches the loop section it runs continuously till the arduino board is powered on. The loop section is declared as similar to the setup function void loop() followed by a curly open bracket '{' then the code which should be run continuously then the curly close bracket '}'.
    
     Moving on to the blink code which contains 

void loop() {

  digitalWrite(led, HIGH);   
  delay(1000);                      
  digitalWrite(led, LOW);   
  delay(1000);                       
}

digitalWrite(ledHIGH); - which means the 13th pin is digitally written with  a value high which digitally makes pin login high. In other words it can be said that the 13th pin is tied to logic high which turns on the led connected to that pin.

delay(1000); - delay is a function used to pause the code for a certain period of time. The delay is usually mentioned in ms (milli Seconds). Therefore here the delay is 1000ms which equals to 1 second. Now it is clear that the led glows for 1second.

digitalWrite(ledLOW);  -  Since the led has to be turned off after 1000ms now the 13th pin is digitally written low which makes the pin tie to 0V potential. Now the led is turned OFF.

delay(1000);  - The led stays in off condition for another 1000ms.
  
      after this line the code ends but since this code is placed in the loop section, As i mentioned before once again the code will jump to the starting of the code which is digitalWrite(ledHIGH);. Thus it is clear that the led keeps on blinking with 1000ms interval between on and off.

Uploading the code to the arduino board:

           After the whole code is written its time to check the code by compiling the code. Compiling the code is soo simple what you must do is just to click the verify symbol which is marked in the image below,



          Once the code is verified you will  get a message like "DONE COMPILING" in the bottom dialog box. Now the code is ready to be uploaded to the arduino board.
           
           Make sure that the arduino board is connected to  the pc and the proper drivers are installed as mention above. Now whats left to do is to select the proper arduino board you have used and to select the appropriate com port the arduino board is assigned to. The following images will show you the steps.





          Select the proper arduino board you have used and also the proper com port which will be displayed in the list. Now just with a click of the upload button the code will be uploaded to the arduino board. 




         Now the code will be uploaded to the arduino board and the led will start to blink with the delay specified in the loop section. The delay can be adjusted according to your need. 

         Now you would have understood the basics of an arduino and how to upload a code to an arduino board. I recommend to try the other example codes given in the examples section of the code and try to understand the code and the syntax which are used in it. My next post will come up with a basic project which can be done with a arduino.

If you have any queries or doubts regarding this comment in the comment section i will try my best to clear it and keep in track with this blog to gain technical stuff.

To purchase electronic components visit Jivith Enterprises.

Thats all for now and see you next time...

Comments