Arduino LED Blinking

Introduction: Arduino LED Blinking

In this tutorial we are going to design a simple external circuitry with Arduino and programme it to blink the LED with a delay of 1 second. The entire process will be a simulation and we will be using Proteus for that.

Supplies

  1. Arduino IDE
  2. Proteus
  3. Proteus Arduino Library

Step 1: Setting Up the Circuit

The LED is connected to pin 11 of the Arduino. Note that I have used a current limiting resistor. Now that the circuit is all set ready, the next thing to focus on is the code. We want our LED to turn on and off with a delay of 1 second, in other words the LED will be on for 1 second and off for 1 second. So let's get started with the code.

Step 2: Understanding Arduino Sketch

This how the Arduino sketch window will look like, when you open it for the first time. So as you can see there are two sections in it. The code inside void setup will only be executed once and it will be executed at the beginning of your program and the code inside void loop will be executed over and over again. So usually we use void setup to define few meanings and functions which we tend not to change in the rest of the code. Now let's use these facts to write our code.

Step 3: Writing the Code

As you can see we have used the function pinMode inside void setup(), the pinMode function takes in two arguments, the first one is the pin number and the second one is input/output. In our circuit we have connected our LED to pin 11 and it is an output, hence we use the code:

pinMode(11, OUTPUT);

Inside void loop() we have used function digitalWrite, this function also takes in two arguments, the first one is the pin number and the second one is the value we want to give it (since it is a digital pin and we are using digitalWrite function, the value is either HIGH (5v) or LOW (0v).) we want the LED to blink, therefore first we set it to HIGH and then LOW with an interval of 1 second by using the code below:

digitalWrite(11, HIGH);

delay(1000);

digitalWrite(11, LOW);

delay(1000);

The 1000 inside delay function means 1000 milliseconds in other words 1 second. Since the above code is contained inside void loop(), the code will be executed over and over hence the LED will blink endlessly.

Now imagine if you want to remove the LED and connect it to pin 13, in that case you will have to change the pin number from 11 to 13 in your code, over here you will have to replace this three times (once in void setup() and twice in void loop()). If your code was even lengthier and if you have mentioned the pin number several times then it would be a pain to make the modification and also if you are giving this code to another person, he/she won't be able to understand it without looking at your circuit. To avoid this kind of issues, you can use a variable to define the LED pin and use that variable to specify that pin wherever needed. Take a look at the code below.

Step 4: Modifying the Code

We have use the variable LED_Pin to specify pin 11, if we want to change the pin number, we have to change it only once in the code no matter how lengthier the code is. Note that we have used "int" before LED_Pin because we need to tell the Arduino what kind of a data type this variable is, our variable is an integer hence we use "int".

I hope this tutorial was useful for you. In the next tutorial we will connect a button to the above circuit and try to control the behavior of the LED through that.

Be the First to Share

    Recommendations

    • Audio Challenge

      Audio Challenge
    • Lamps and Lighting Contest

      Lamps and Lighting Contest
    • Pumpkins & Gourds Speed Challenge

      Pumpkins & Gourds Speed Challenge

    Comments