Project 1

Blinking LED

Introduction

Hey Guys! I hope all of you are doing really great! Today i’ll be showing you how to Blink the built-in and an external LED connected to the arduino and change it’s blinking pattern easily.

Note: If you are new to IOT and don’t know anything about programming or Hardware, these Blogs are for you as i’ll be starting from very basic and will be explaining everything. So all you need is an urge to learn!

Modules Used:

Image with just alt text

Got everything? Okay! We’re good to go. While we write code, we also think about the design of the Hardware and it’s Function. But for the sake of simplicity we will first Assemble the Hardware and then we will look at the program.

Components Explanation:

Before we start with the assembly of the Hardware, it’s better to understand the use of the components.

Breadboard is a device used for making temporary connections with Arduino. It is used to avoid soldering. Breadboard’s interface is really easy to understand and use. It is divided into three parts. The leftmost and rightmost portions consist of two vertical columns each, with many holes which are connected to each other. That means if we connect one hole of that row to power, the whole vertical row gets connected to power. These holes are labelled as + and -. Generally the + sign row, marked with red is used for providing power to the components and the - sign row, marked with blue is used for grounding the components.

Note: Grounding is connecting the components to Earth in order to prevent overflow of current at the time of accidental cut.

The middle portion consists of horizontal rows with holes in a group of five, which are connected to each other. That means if we connect one hole of that group to certain pin on Arduino, the whole horizontal group of five gets connected to it.

Resistor is used for reducing the current flow by causing resistance to the flow. It is used to prevent our LED from burning in the case of excess current flow.

USB wire is used to power the Arduino. We can also use a power adapter but for our project, power supply through USB is enough. Jumper wires are used to connect the components. This project can also be performed using the built-in LED on the Arduino but an external LED is preferred for a more visible output and for the sake of numerous applications. LED has two legs, one of them is shorter than other. The shorter one is called Cathode (negative) and the longer one is called Anode (positive). The current flows from Anode to Cathode, therefore the orientation of the LED is important.

Hardware Assembly:

Now, let’s start with assembling the Hardware:

Image with just alt text
Image with just alt text
Image with just alt text
Image with just alt text

Hardware Explanation:

As the LED and the Jumper wire are in the same group of holes, therefore the LED will get connected to the pin13 on the Arduino. And as the cathode leg of the LED and the resistor are in the same of group of holes, they get connected. The other side of the resistor is grounded, therefore the LED gets protected from fusing at the time of excess current.

As the assembly of hardware is done, now it’s time for writing the program and uploading it to the Arduino.

Note: For programming the Arduino, C/C++ language is used. If you don’t know anything about C, just make sure to pay more attention to it. I’ve explained the code and also added comments within the program for better understanding.

Program:


int LEDpin = 13;                 
void setup (){                   
  pinMode(LEDpin, OUTPUT);       
}                              

void loop()                     
{                               
  digitalWrite(LEDpin, HIGH);   
  delay(1000);                  
  digitalWrite(LEDpin, LOW);    
  delay(1000);                 
}                               

For reference go to Arduino IDE: file > examples > basics > blink

Program Explanation:

Image with just alt text

In C language, the first step is to declare the constant. Here, by using ‘int LEDpin = 13’ we declare that “LEDpin” is a constant, having an ‘integral’ value 13, which is actually our desired PIN on the Arduino. Now, in Arduino sketch the “void setup” and “void loop” are the two main functions which are mandatory for every Arduino sketch. The ‘Setup’ function is only executed once at the beginning of the program and the ‘loop’ function executes repeatedly. The term ‘void’ here is used to signify that these functions do not return anything and they don’t take any arguments (Note: Argument is as a value provided to the function whenever we call it).

The function ‘pinMode’ is used to declare an INPUT or OUTPUT, which in our case is the OUTPUT at digital pin 13. Now in order to make the LED blink we will use the function ‘digitalWrite’ which basically writes the passed argument to the specified digital pin. Therefore the statement ‘digitalWrite(LEDpin, HIGH);’ makes the LED on pin13 ‘HIGH’ i.e. turn ON. The curly brackets ‘{}’ after any statment is used to define a clear scope and is also used to group statements for better understanding. (Note: an open curly bracket must always be closed after grouping the statements)

Now the function ‘delay’ takes one argument in milliseconds which holds the LED on that state for the specified time. Therefore, the statement ‘delay(1000);’ makes the LED glow and hold it for one second. The statment ‘digitalWrite(LEDpin,LOW);’ does exact same thing as ‘digitalWrite(LEDpin, HIGH);’ but instead of turning the LED ‘ON’, it makes the LED on pin13 ‘LOW’ i.e. turn OFF and ‘delay(1000);’ turns ‘OFF’ the LED and holds it for a second.

You might be wondering that what is the use of a semicolon ‘;’ after every piece of code. Well, in C/C++ semicolon acts as a Terminator and it used to tell the compiler that we have reached the end of the command. Semicolon at the end of a command is mandatory in C/C++ language.

Now we are done with our Program and Hardware. It’s time to upload the sketch to the Arduino.

Note: If you get any errors while compiling the sketch, just make sure the Arduino is connected correctly to the PC and the ‘board’ & ‘port’ is specified in the tools menu. Also check for any errors in the code itself, especially look for unclosed curly brackets or missing semicolons at the end of the statements.

Once the program is successfully verified and uploaded to the Arduino we will see a ‘Done Uploading’ message at the bottom of the screen and the built-in and external LED connected to the Arduino starts blinking.

We can also change the pattern of the blinking LED just by playing around with ‘delay’ and by adding different ‘digitalWrite’ functions.

For ex.


int LEDpin = 13;
void setup (){
  pinMode(LEDpin, OUTPUT);
}

void loop()
{
  digitalWrite(LEDpin, HIGH);
  delay(2000);
  digitalWrite(LEDpin, LOW);
  delay(500);
  digitalWrite(LEDpin, HIGH);
  delay(500);
  digitalWrite(LEDpin, LOW);
  delay(500);
  digitalWrite(LEDpin, HIGH);
  delay(3000);
  digitalWrite(LEDpin, LOW);
  delay(500);
}

//end

Click Here to see the result of the above code

As you can see, now the pattern of blinking LED has been changed.

Conclusion:

In this blog we looked at some C language functions and their uses. We also looked at a basic layout of Arduino sketch and also learnt how to make an external LED blink with different patterns. This was just a basic Example; We will keep stepping up. So for many more such projects, stay tuned!

Thank You!


Share on: