Recent Posts

6/recent/ticker-posts

Arduino: Lesson 3 - Blinking an LED with delay() function

Fig. 1 - Arduino: Lesson 3 - Blinking an LED with delay()

Welcome to Lesson 3 - Basic Arduino Course

Today we are going to learn how to make our first algorithm, let's learn how to use the blink() function, this function makes your Arduino blink an LED, every time determined in the programming.

Hardware Required

  • Arduino Board
  • LED - optional "You can use the onboard LED"
  • 220 ohm resistor - optional "You can use the onboard resistor"

The Circuit

You can use as an example the built-in LED that most Arduino boards have. This LED is connected to a digital pin 13, however, this number may vary from board type to board type. 

This constant is LED_BUILTIN and allows you to control the built-in LED easily, and the correspondence of this constant is digital pin 13.

However, you can to lit an external LED with this sketch, you  only need to build the circuit showing in Figure 2 below, you can connect one external LED in serie with the resistor to the digital PIN 6

Connect the positive leg of the LED (the positive leg, is the anode) to the other end of the resistor. Connect the negative leg of the LED (the negative leg,  is the cathode) to the GND

Fig. 2 - Arduino Blink LED with delay function - tinkercad.com

Resistor calculation

We need to use a resistor in series with the LED to limit the current in the LED. The value of this resistor depends on the LED we will use.

Considering that we will use an external green LED, let us analyze this situation by following the steps below:

  • The power supply comes from a digital pin on the Arduino (Vdc = 5v). 
  • The LED needs a current of 20mA to be powered. 
  • And a supply voltage of 2.5V.

You can see the voltage requirements of LED in the table in Figure 3 below, which contains the average voltage of most standard color LEDs.

Fig. 3 - Typical Led Voltage Range

Knowing that the LED power supply is 2.5V and 20mA (0.02A), following the table in Figure 3, let's put the data already obtained in the formula in Figure 4 below, and calculate:

Fig. 4 - Formula to calculate the resistance for the power supply of LED

Formula: 

R = (Vps - Vled) / Iled

R = (5 - 2,5)/(0.02)

R = 125

As we can see, the result of the calculation made resulted in a resistance of 125Ω. As 20mA is on average the threshold value of the LED, R = 125Ω will also be its threshold resistance so as not to damage the LED.

The resistor value in series with the LED can be a different value, it can be one of 150 ohms, or more, such as 220 ohms, 330 ohms; the LED will also light up with higher values but with less intensity.

The Code

After building the circuit, connect your Arduino board to your computer, launch the Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first, let's understand the code line by line. 

  • First, in void setup() structure, digital pin 6 is initialized as output pin, as shown in line 4 below 
1
2
3
4
5
// Arduino: Lesson 3 - Blinking an LED with delay()

void setup() { pinMode(6, OUTPUT); // initialize digital pin 6 as an output.
}
  • In the void loop() structure, in line 8, the digitalWrite function, command activates port 6 to HIGH level, it means that it goes from 0V to 5V, which makes the LED receive the voltage and lit up. 
  • In the line 9, the delay() instruction indicates that we will have 1000 milliseconds or 1 second to wait, and then enters the next step which is to turn the LED off.
1
...
7
8
9
10
// Arduino: Lesson 3 - Blinking an LED with delay()

void loop() { // The loop structure runs over and over again forever digitalWrite(6, HIGH); // initialize digital pin 6 as an output.
delay(1000);           // Wait for 1 second
...

  • In the line 12, the digitalWrite function, command activates port 6 to LOW level, it means that it goes from 5V to 0V, Then turn the LED off.
  • In the line 13, the delay() instruction indicates that we will have 1000 milliseconds or 1 second to wait, and then enters the next step which is to turn the LED off.
1
...
7
...
12
13
// Arduino: Lesson 3 - Blinking an LED with delay()

void loop() { // The loop structure runs over and over again forever
digitalWrite(6, LOW); // initialize digital pin 6 as an output.
delay(1000);           // Wait for 1 second
...

This brings the LED pin back to 0 volts, and turns the LED off. Enough time should pass between powering on and powering off, that's long enough to see the LED blink. 

Therefore, the delay() function tell the Arduino board to do nothing for 1000 milliseconds or 1 second. When you use the delay() function, nothing else happens for that amount of time. 

The complete code is showed in the sketch below.

1
2
3
4
5
6
7
8
9
10
11
12
// Arduino: Lesson 3 - Blinking an LED with delay()

void setup() { pinMode(6, OUTPUT); // initialize digital pin 6 as an output.
}

void loop() { // The loop function runs over and over again forever
digitalWrite(6, HIGH); // Turn the LED ON (HIGH voltage level)
delay(1000);           // Wait for 1 second
digitalWrite(6, LOW); // Turn the LED OFF (LOW voltage level)
delay(1000);                 // Wait for 1 second
}
//------------------------------------- www.elcircuits.com --------------------------------------------

We can also use the same code, adding a name to pin 6 of the code above, simply assigning an integer to pin 6.

1
2
3
4
// Arduino: Lesson 3 - Blinking an LED with delay()

int ledPin =  6;      // the number of the LED pin

In this way, you will use pin 6 identification as ledPin, as we can see in the complete code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Arduino: Lesson 3 - Blinking an LED with delay()

int ledPin 6;      // The number of the LED pin

void
setup() { pinMode(ledPin, OUTPUT);
// initialize digital pin 6 "ledPin" as an output.
}

void loop() { // The loop function runs over and over again forever
digitalWrite(ledPin, HIGH); // Turn the LED ON (HIGH voltage level)
delay(1000);           // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED OFF (LOW voltage level)
delay(1000);                         // Wait for 1 second
}
//------------------------------------- www.elcircuits.com --------------------------------------------

This second code will work the same way, however when we have many "Pins" ports in a larger project, and we need to change the pin to another port, in this second code, you simply change line 3 of the code, which assigns port 6, the integer ledPin to another port you need.

Next Lesson

Previous Lesson


Any questions, suggestions, corrections, please leave us in the comments below, which we will soon be answering.

My best Regards to You!!!

Post a Comment

0 Comments