How to Blink an LED with Arduino

Blinking an LED with Arduino

How to Blink an LED with Arduino

Watch the Video Tutorial

A Light Emitting Diode (LED) is a semiconductor light source, when forward biased, it emits light. 
LEDs are used mainly to indicate the status of electronic circuits, for example to indicate that power is on or off but nowadays they are used in many applications including lighting and beam detection. 
Early LEDs emitted low-intensity red light, but today high brightness and many colour LEDs are available (white, blue, red, green, yellow or even infrared). 
LEDs have many advantages over the traditional lights (the incandescent and neon light bulbs)  such as: low voltage of operation, very low  energy consumption, smaller size, longer lifetime, available in many colours etc. In many energy efficient applications, the LEDs are tending to replace the traditional light source.

All of us start learn a new programming from Hello World. In microcontroller fields the Hello World example is usually blinking an led, in this article we will learn how to Blink an LED with Arduino. 

Connecting LEDs

An LED is similar to a diode, it has two legs: the longer leg is the anode (+) and the shorter leg the cathode (-). The cathode is also identified by a flat side on the body. 
The intensity of the light emitted by an LED depends on the amount of forward current passed through the device but we must take attention not to exceed the maximum allowable forward current or draw more current than the Arduino output pin can handle. or this reason it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1k resistors, unless maximum current draw from the pins is required for a particular application.

Figure 1 below shows an LED on Breadboard connected to pin D7 of the Arduino. This is the beauty of Arduino, it make prototyping very easy. With a Breadboard and an arduino board, you can do wonders. The Anode pin of the LED is connected to D7 with a 470Ω current limiting resistor.

Figure 1: LED connected to pin D7

Before reading further, please read this tutorial first:

Getting Started with Arduino

Buy LEDs and Arduino Boards from our Online Shop

Writing the Sketch

Now let us write a simple Sketch to blink an external LED connected to a digital pin (say 7) of Arduino. The ON time will be 2 seconds and the OFF time 1 second.

Figure 2: Blinking an LED connected to pin D7

Arduino always expects that you have created two functions: the setup() and one the loop() functions. To write a function, you give it a name followed by two closed brackets and two curly braces to group a block of codes together. setup() is where you put all the code that you want to execute once at the beginning of your program, and loop() contains the core of your program, which is executed over and over again. This is done because Arduino like other microcontrollers, is not like a regular computer, it can’t quit while running a program, it has to execute a code over and over again. When you power up the board, the code runs, when you want to stop, you just turn it off.

Any text beginning with // or anything between  /*   */ is ignored by Arduino. These lines are comments, which are notes that you leave in the program for yourself, so that you can remember what you did when you wrote it, or for somebody else to understand your code. Let us explain our code:

void setup(): This line tells Arduino that the next block of code will be a function named setup(). With this opening curly brace curly brace: { a block of code begins:

pinMode(7, OUTPUT); pinMode() tells Arduino how to configure a certain pin. Digital pins can be used either as input or output, but we have to specify it first. This function needs two arguments, the first argument tells pinMode() which pin we are configuring and the second argument tells whether we want to use that pin as an input or output. INPUT and OUTPUT are predefined constants in the Arduino language, In this case, we need to set D7 as output pin to control our LED.

void loop() {: this is where you specify the piece of code that will be repeated over and over again until you remove power from the board.

digitalWrite(7, HIGH); digitalWrite() turns on (or off) any pin that has been configured as an output. Just as pinMode() function, digitalWrite() expects two arguments the first argument specify the pin we are controlling and the second argument tells digitalWrite() whether to set the voltage level to 0 (LOW) or to 5 V (HIGH). LOW will switch OFF our LED and HIGH will switch it ON.

delay(1000); delay() makes the microcontroller sit there and do nothing for the amount of milliseconds that you pass as an argument. Milliseconds are thousandths of seconds, therefore, 1000 milliseconds equals 1 second.

Verify and upload the code into your arduino board.

Simulate Arduino with Proteus ISIS

Proteus is the most powerful tool in simulating microcontroller circuits. Follow these few steps to simulate your Arduino circuit with Proteus:

  • Run ARDUINO software. from FILE menu, select PREFERENCES, and adjust ARDUINO software setting like in the figure 3 below. This will enable us to get the .HEX file after compiling your sketch.

Figure 3: Preferences

  • Compile your sketch and copy the address of the generated .HEX file by pressing (CTRL + C)

Figure 4: Copying the location to the .HEX file

  • Right-click the Arduino board in PROTEUS and paste the .HEX file path link that you copied into the Program File section as shown on figure 5 below.

Figure 5: Copying the .HEX file

  • Now you can run your simulation as shown below on figure 6.

Figure 6: Running the simulation

You can download the full project files (Arduino and Proteus project) below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download: Blink_LED_Arduino_Project

Download: Arduino_Blink_Proteus

Share this post

has been added to your cart:
Select your currency