Blinking LEDs – Delay Function

Blinking LEDs With Delay Function

Goal

Our goal in this project is to blink the default Arduino LED and to also toggle a predefined output pin. We will be using the “delay” function for this example.

Concept

In order to blink an LED we will first need to set the pin as an output. This can be achieved by using the pinMode() function.

The following code snippet sets pins 0 (D0) & 13 (built-in LED) as output pins.

void setup() {
  pinMode(0, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}
Defining pins as outputs

Next, we need to set the outputs to logic high (turn them ON) and then set them to logic low (turn them OFF). We also need to add a delay after setting each of the logic states in order for us to view the state. If we do not add this delay, then the Arduino will repeatedly toggle the pin state and we will not be able to see this as the code is executed code much faster than our eyes and brain can process information.

// the loop function runs over and over again forever
void loop() {
  
  digitalWrite(0, HIGH);             // turn the LED on (HIGH is the voltage level)
  digitalWrite(LED_BUILTIN, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(1000);                        // wait for a second

  digitalWrite(0, LOW);              // turn the LED off by making the voltage LOW
  digitalWrite(LED_BUILTIN, LOW);     // turn the LED off by making the voltage LOW
  delay(1000);                        // wait for a second
  
}
Changing pin states
Piksey Blinking LEDs
Source

The complete Arduino project is available for download using the following link. Downloads are restricted to registered users. Registration is and will always be 100% FREE.

Project Source: P1_Arduino_Blinking_LEDs_With_Delay.zip

Dependencies

This project uses standard Arduino libraries and is thus compatible with all Arduino boards that contain the built-in LED (on pin 13) and digital pin 0. Make sure the correct board is selected in the Arduino IDE.

Leave a Reply

  Enable Notifications  
Notify me for