Arduino #3 - Blink Example Breakdown | First Arduino Program


Hello friends, welcome to the another tutorial of How to Electro. In this tutorial, we will start our first level of android programming. We will start it with blink example, which is a easiest and generally used example for beginners learning. We will get to know about many components of arduino programming, which is an essential part while programming, by the end of this tutorial. So friends, lets begin.....

Blink Example

Blink example is an example, which is a generally used example for beginners. It is used to Blink an LED  with some delay, which is connected to pin no. 13 of Arduino. Now let's start to know about the blink Example in details.

Comments :-

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

The Blink Example starts with the above line of code. It is called comments in arduino. It is ignored by the arduino, when uploading sketch to arduino. It is used to explain the program, that which function does which work. It also defines, how a function works or why it is written in that way.  Combining all these, we can understand that, the comments are used in arduino to elaborate code. So that, another person can easily understand the code, who reads your code. It also helps the coder in many ways, as when the coder writes program, he/she can mark important functions/ problems with the help of comments. So that, it can be easily sort out in future.

In arduino there are 2 types of comments namely, multi line comments and single line comments. Now let's know about these one by one :-
  1. multi-line comments :- It is used to add multi-line text description of a function, as for defining the working of a particular function.
  2. single-line comments :- It is used to add single line brief description of any particular line of code/function.
The syntax of using these comments are given below :-
1. multi-line comments :- 

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

The syntax of multi-line comments is given above. The multi-line comments starts with /*
and ends with */ . All the content / description of the code comes between these two symbols. In the above code, we can easily see that the comments starts with /* and ends with */ . The additional stars except of the starting symbol and end symbol is given only for making code look attractive. There is no any useful purpose behind it.

2. single-line comments :-

int ledPin = 13;// LED connected to digital pin 13

The syntax of using single-line comments is given above. It starts with // and ends with the end of brief description in single line. It is helpful where there is need to describe single-line or a part of code.

Variables :-

Variable is a useful component of any code, because it is used to store a piece of data, which is accessed at any time and at any place within the code. And the most important work of this is that the piece of data can be modified in the runtime, which helps a developer in many ways , as if someone has to check the light intensity and to give certain outputs with different intensity, he/she will first initialize a int variable with value '0' and, it changes in runtime according to different types of light conditions, which we can access at any place for giving desired output. The variable also reduces the code size, as if any variable is assigned, we can easily use it from the unique name, there is no need to write the value again and agian. Now let's know, how to initialize a variable :-

int ledPin = 13;// LED connected to digital pin 13

The variable is initialized by first declaring the type of variable. In the Blink example, integer type variable is used, which is used to declare the led pin no.13 to which the LED is connected to. We will know about more variables types in upcoming tutorials. After the type, we had to give any unique name to variable, which we use to access it in the code. As in the blink example, the "ledPin" name is used for the variable. Now the final step is to, add some value to the variable which is accessed with the unique name in the code. In the Blink example, the 'ledPin' has been assigned a value equal to 13, which is the pin number, where the LED is connected to.

Functions :-

The functions is an important part of any program because it makes the program alive. As in the blink example, the led blinks which is only possible because of the function. We can define function as a piece of code, which is uniquely named, and performs some work as output, processing or input, and can be accessed with the help of unique name anywhere in the code, is called function. Now let's understand about some of the functions used in blink code.

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

The setup() function is one of the basic structural code of the arduino. It is used as a initializer place for variables, setting pinmodes, starting communication etc. Now let's know how a function is created with the help of the above given function.

First step is to write the type of the function, as in the above function, void is written which means it is a function, which doesn't return any value. We will know more about the functions in the upcoming tutorials. After that, we had to give some unique name to function, which we use to access anywhere in the code. In the above function, setup is a unique name given to the function. After this, we had to give two small brackets which is a constructor of function. If we had to pass any value to the function, which the function uses for any purpose is first initialized here with some variable , and it is accessed within the function. And when we call the function at any place, we had to pass required value to the function. We will know more about this in the functions tutorial.

In the blink example, we had not to use any value in the function, so, we had not use any variable within the brackets. At the end, we had to add two reversed- curly brackets and, hence, your function is ready. The content within the brackets are called body of the function, which is processed, when a function is called. In the above given code, the body of the function contains "pinMode(ledPin,OUTPUT)" , which is a function for setting pin as input pin or output pin. Here, the function takes some value, which means that this function requires some value for the body's code to work properly.
void loop()
It is the another important basic structure function of arduino. In the code, if we don't need any of these basic structural functions, still we had to add it, otherwise the code will generate a error. As we had already learnt that, how a function works, how it is created and so on. So, under this topic, we had to only understand the function of the loop() function. This function is intended for processing inputs and giving outputs. In the blink example, we had write the code in the body of this function, which gives output i.e. blink an LED. It is a function which loops consecutively, and hence the code inside the loop() function runs again and again, So all the code, which is responsible for receiving inputs, processing it and giving outputs is written in the body of the loop() function.
In the blink example, there are some important functions used in the loop() and setup() function. Now let's know about these functions :-
1. pinMode(pin number, INPUT/OUTPUT) :- It is a function, which is used for defining arduino pins as input or output. It takes two parameters, one is the pin number, and another is the mode either INPUT/OUTPUT.
2. digitalWrite(pin number, HIGH/LOW) :- It is a function, which is used to outputs a value on a pin, either HIGH or LOW. It also takes two parameters, one is pin number, and another is Value either HIGH/LOW. The HIGH means that, 5v is supply to the pin, whereas, LOW means that, 0v is supplied to the pin.
3. delay (number of milliseconds) :- It is a function, which adds delay between the interpretation of two line of codes i.e. waits the arduino for specified number of milliseconds in the parameter before continuing to the next line of code.

Full working :- 

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

The LED pin is first initialized with "ledPin" as its name with value 13, which is the pin number to which LED is connected to. After that, we had to set the pin mode of the "ledPin" variable to output with the help of pinMode() function, because the LED will receive output in this case. Now after initializing everything in the setup() function, we had to go to the loop() function for adding the code, which will loop consecutively.

In the loop function, first, with the help of digitalWrite() function, set the output of "ledPin" to high, which glows the led. After that add a delay of 1000 millisecons, so that the LED will glow continuously for 1 seconds (1000 milliseconds = 1 second). and we know that the delay() function will prevent the code from going to second line of code, before the delay ends. So after 1 second, set the output of led to low using digitalWrite(), so that the led will stop glowing.

After that , again add a delay of 1 second, so that LED will not glow for 1 second. Now this process will go on continuosly because of the loop() function. which as a result blinks the LED.

Note :- when writing some function or initializing variable or writing any piece of code in the body of the function, never forget to add the semicolon to the end of the function or variable or piece of code.

So friends, that's all for this tutorial. Hope u enjoyed it. If you have any question regarding this tutorial, then ask me in comment Box. I will try to answer it as soon as possible. Also please, share this post to that person, who want to learn programming, and also subscribe to the newsletter for the direct notifications of new post directly to your inbox.
Thanks -
How to Electro (Samridh Sharma).

Comments