Arduino Lesson 3 – Blink an LED using delay() Function

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

Arduino: Lesson 3 – Blinking an LED with delay()

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 onboard LED”
  • 220 ohm resistor – optional “You can use 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 correspondence of this constant is digital pin 13.

However, you can to light 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 series 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

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

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 power supply of LED

Fig. 4 – Formula to calculate the resistance for 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 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
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 voltage and light 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.
void loop() {                      // The loop structure runs over and over again forever
  digitalWrite(6, HIGH);         // Turn LED ON (HIGH voltage level)
  delay(1000);                  // Wait for 1 second
  digitalWrite(6, LOW);          // Turn LED OFF (LOW voltage level)
  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 tells 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 shown in the sketch below.

// 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 LED ON (HIGH voltage level)
  delay(1000);                  // Wait for 1 second
  digitalWrite(6, LOW);          // Turn 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.
// 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 LED ON (HIGH voltage level)
  delay(1000);                       // Wait for 1 second
  digitalWrite(ledPin, LOW);          // Turn LED OFF (LOW voltage level)
  delay(1000);                       // Wait for 1 second
}
//------------------------------------- www.elcircuits.com --------------------------------------------
In this way, you will use pin 6 identification as ledPin, as we can see in the complete code below.

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!

🔗 Share This Guide

Best regards, The ElCircuits Team ⚡

Share This Guide

Leave a Reply

Your email address will not be published. Required fields are marked *