# 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.Â

## 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Â
 12345 //Â Arduino: Lesson 3 - Blinking an LED with delay()void setup() {Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â // This functionÂ is called once when the program starts 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...78910 //Â 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...1213 //Â 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() {Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â  // This functionÂ is called once when the program starts 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 seconddigitalWrite(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.

 1234 //Â 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.

 123456789101112131415 //Â Arduino: Lesson 3 - Blinking an LED with delay()int ledPinÂ =Â  6;Â  Â  Â  //Â The number of the LED pinvoid setup() {Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â // This functionÂ is called once when the program starts 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 seconddigitalWrite(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.

Reactions