Saturday, 31 January 2015

#6 First Program on Arduino Uno- Blinking LED Part 3

This is 8th post on Arduino tutorial.
If you want the list of posts click here.

In the last post we have seen what is minimum requirement in a Arduino program.
At this moment we know about all the Arduino pins, as well as we all have IDE ready.

Let's write our first program to blink a LED (Light emitting diode) using arduino uno.

All you will need is a arduino board,  computer to write a program and burn it on the arduino board, LEDs, resistors and a little patience.

I will be explaining you the code step by step, you can also download the file which has Blinking LED code. Click here for the INO file.

Explanation :-
  • First download the 'ino' file from above and open it, now connect your LED on arduino board as shown in the pic above. [recommended to use a resistor].
  • Next thing is connect your arduino board to computer using USB chord, now in the IDE go to serial port and select appropriate COM port [you might see 2-3 COM ports select the proper one]. Also check if proper board is selected.
  • Now click on verify and upload it, now the IDE will show uploading and you will see Done uploading without any errors [If you see a error in red color the try changing COM port and see if drivers are properly installed].
  • The moment code is uploaded the LED will start blinking, it will stay ON for one second and OFF for another second.
  • Let's see how does the code works,
    • Comments are written by me in the LED_blinking.ino file. you can refer those for quick help. [It's always a good habit to comment about each step, helps us to understand and debug the program, also helps other people to understand your code easily].
    • We have declared a variable named LED and have stored the value of PIN NUMBER that we want to use [or to be specific control].
    • Next thing is doing the board setup so writing the void setup() function and then configuring the PIN No 10 that we will be using.
    • Configuration includes a syntax,

                                  pinMode(pin, mode);

                        e.g. pinMode(10, INPUT); or pinMode(12, OUTPUT);
                         int led1 = 13;
                         int led2 = 9;
                         pinMode(led1, INPUT);      // sets the digital pin as Input
                         pinMode(led2, OUTPUT);   // sets the digital pin as output
    • pinMode is a default function developed by the arduino for configuring the pins on arduino board, we can aslo configure the Analog pins and use them as digital pins using the same method.
                       e.g. pinMode(A0, INPUT); or pinMode(A1, OUTPUT); 

    • The analog input pins can be used as digital pins, referred to as A0, A1, etc.
    • After setting up the Arduino pins, now we need to develop logic and write it in void loop() so that Arduino executes it continuously.
    • Now logic behind Blinking a LED is simple we need to give LED a supply for one second and the cut off the supply for one second. This steps are to be kept in a loop and we have a blinking LED.
    • So looking at the hard ware connection of the LED two things are clear first the negative end of LED is grounded through resistor [220 or similar ohms] and the positive end is connected to pin no 10.
    • When Pin 10 will be 5v the LED will glow and when pin 10 will be 0V Led won't glow. Thanks to arduino they have a ready made function to make a digital pin HIGH or LOW, syntax is as follows :-

                     digitalWrite(pin, Value);

                 e.g. digitalWrite(10,HIGH);

                 The pin number could be directly specified or we can use a variable &
                  the value can be given as either HIGH or LOW. 

    • So now we will make the pins High and Low depending on the code written. Things have become so simple.
    • But when we write just digitalWrite(10, HIGH); and digitalWrite(10, LOW); next to each other and burn the code nothing happens! any guesses ?
    • Actually the Microprocessor runs the Whole code nearly 10^5 (100000) times approx. So the LED is switched ON and OFF in such a great speed we seem it's still off. So how to stop Microprocessor ? Answer is put a delay.
    • delay(ms) is another ready made function we have in Arduino which takes the value of time in milliseconds in the bracket.
                             e.g. delay(1000); //delay of 1 second

                                    delay(5000); //delay of 5 second

    • Now the whole code might seem really easy! It's recommended to play with more Pins, more LED'S and try switching ON and OFF with different styles and create patterns. Believe me you can even create beautiful festival lights with just this much of knowledge :-P 
    • Tip :- Most Arduino Boards have LED connected on pin 13 [if you remember] use that if you don't have a LED and breadboard for now ;)
  • Keep trying and playing with codes & comment if you try something new :)
Visit the next post on 'Arduino UNO - Program to Fade the LED'.
click here.

Follow the blog, 
Hit like on the Facebook page and stay tuned :)
Comments are welcomed :)
Thank You.