LED Blink Without Delay Code | Arduino Tutorial

Introduction

In this tutorial we learn how to LED Blink Without Delay Code | Arduino Tutorial, The LED Blink Without Delay project offers an alternative method for blinking an LED using the Arduino UNO board. Instead of relying on delay functions, which pause program execution, this tutorial introduces a non-blocking approach that allows for precise timing and multitasking. By utilizing the millis() function, you can achieve more efficient and responsive LED blinking. In this tutorial, we will guide you through the necessary hardware setup and provide a step-by-step explanation of the code implementation.

Hardware Required

To get started with the LED Blink Without Delay Code | Arduino Tutorial, you will need the following components

Components#Buy From Amazon
Arduino UNO1Buy Now
LED1Buy Now
Push Button1Buy Now
Resistor 220Ω1Buy Now
Jumper Wires4Buy Now
Breadboard1Buy Now
9v DC Adapter (Optional)1Buy Now

What is an LED?

LED stands for Light-Emitting Diode, and it is a semiconductor device that emits light when an electric current passes through it. LEDs are commonly used in various applications, such as indicators, displays, and lighting. They come in different colors and easily control using microcontrollers like Arduino.

Pinout

Led-Pinout

Pin Configuration

Pin NamePin Description
( – ) or Short PinCathode
( + ) or Long PinAnode

Specifications

Before we proceed, let’s explore some specifications of the LED used in this project:

  1. Operating Voltage: Typically, LEDs operate at low voltages, usually around 2 to 3.3 volts. However, it is important to check the datasheet or product specifications of your specific LED to ensure the correct voltage range.
  2. Forward Current: The forward current is the amount of current that should flow through the LED to achieve the desired brightness. LEDs have different forward current requirements, usually ranging from 5 to 20 milliamperes (mA).
  3. Viewing Angle: The viewing angle refers to the angle at which the LED emits light. It determines how wide or narrow the light spread is. Common viewing angles for LEDs range from 90 to 120 degrees.
  4. Package Type: LEDs come in various package types, such as through-hole and surface mount. In this project, we will be using a standard through-hole LED.
  5. Color: LEDs are available in a wide range of colors, including red, green, blue, yellow, and more. The color of the LED used in this project can be chosen according to your preference.

Features

Now, let’s explore some key features of LEDs that make them ideal for electronic projects:

  1. Energy Efficiency: LEDs are highly energy-efficient compared to traditional incandescent bulbs. They consume less power and produce very little heat, making them ideal for battery-powered projects.
  2. Long Lifespan: LEDs have a significantly longer lifespan compared to traditional bulbs. They can last for thousands of hours, ensuring durability and longevity for your projects.
  3. Compact Size: LEDs are small and compact, allowing for flexible integration into various projects. Their small size makes them suitable for applications where space is limited.
  4. Instant On/Off: LEDs illuminate instantly without any warm-up time. They can be turned on and off rapidly, making them perfect for creating dynamic lighting effects.

Circuit Connections

To set up the circuit for the LED Blink Without Delay project, follow these connections

  1. Connect the cathode (shorter leg) of the LED to the GND (ground) pin on the Arduino board.
  2. Connect the anode (longer leg) of the LED to one end of the 220-ohm resistor.
  3. Connect the other end of the resistor to digital pin 13 on the Arduino board.
  4. Connect one leg of the button to digital pin 2 on the Arduino board.
  5. Connect the other leg of the button to the GND (ground) pin on the Arduino board.

Circuit Diagram

LED-Blink-Without-Delay-arduino-led-button-wiring-diagram

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Code

Arduino Code – With Delay

//For more Projects visit at: www.arduinocircuit.com

// constants won't change:
const int LED_PIN = 3;     // the number of the LED pin
const int BUTTON_PIN = 7;  // the number of the button pin

const long BLINK_INTERVAL = 1000;   // interval at which to blink LED (milliseconds)

// Variables will change:
int ledState = LOW;   // ledState used to set the LED

int previousButtonState = LOW; // will store last time button was updated

void setup() {
  Serial.begin(9600);

  // set the digital pin as output:
  pinMode(LED_PIN, OUTPUT);

  // set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  // if the LED is off turn it on and vice-versa:
  ledState = (ledState == LOW) ? HIGH : LOW;

  // set the LED with the ledState of the variable:
  digitalWrite(LED_PIN, ledState);

  delay(BLINK_INTERVAL);

  int currentButtonState = digitalRead(BUTTON_PIN);

  if (currentButtonState != previousButtonState) {
    // print out the state of the button:
    Serial.println(currentButtonState);

    // save the last state of the button
    previousButtonState = currentButtonState;
  }

  // DO OTHER WORKS HERE
}

Adding More Tasks

The below code blinks two LEDs with various intervals and checks the state of the button.

Code

Arduino Code – With Delay

//For more Projects visit at: www.arduinocircuit.com

// constants won't change:
const int LED_PIN_1 = 3;            // the number of the LED 1 pin
const int LED_PIN_2 = LED_BUILTIN;  // the number of the LED 2 pin
const int BUTTON_PIN = 7;           // the number of the button pin

const long BLINK_INTERVAL_1 = 1000;  // interval at which to blink LED 1 (milliseconds)
const long BLINK_INTERVAL_2 = 500;   // interval at which to blink LED 2 (milliseconds)

// Variables will change:
int ledState_1 = LOW;   // ledState used to set the LED 1
int ledState_2 = LOW;   // ledState used to set the LED 2

int previousButtonState = LOW; // will store the last time the button was updated

unsigned long previousMillis_1 = 0;   // will store the last time LED 1 was updated
unsigned long previousMillis_2 = 0;   // will store the last time LED 2 was updated

void setup() {
  Serial.begin(9600);

  // Set the digital pins as outputs:
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  
  // Set the digital pin as an input:
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  // Check to see if it's time to blink LED 1
  if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) {
    // If the LED is off, turn it on and vice-versa:
    ledState_1 = (ledState_1 == LOW) ? HIGH : LOW;

    // Set the LED with the ledState variable:
    digitalWrite(LED_PIN_1, ledState_1);

    // Save the last time you blinked the LED
    previousMillis_1 = currentMillis;
  }

  // Check to see if it's time to blink LED 2
  if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) {
    // If the LED is off, turn it on and vice-versa:
    ledState_2 = (ledState_2 == LOW) ? HIGH : LOW;

    // Set the LED with the ledState variable:
    digitalWrite(LED_PIN_2, ledState_2);

    // Save the last time you blinked the LED
    previousMillis_2 = currentMillis;
  }

  // Check if the button state has changed
  int currentButtonState = digitalRead(BUTTON_PIN);

  if (currentButtonState != previousButtonState) {
    // Print out the state of the button:
    Serial.println(currentButtonState);

    // Save the last state of the button
    previousButtonState = currentButtonState;
  }

  // Perform other tasks here
}

Code Explanation

The LED Blink Without Delay project utilizes a code snippet that allows for non-blocking LED blinking. Here’s a breakdown of the code:

  1. Variable Declaration: Declare the necessary variables, including an integer variable to store the LED pin number, a boolean variable to track the LED state, and unsigned long variables to hold the previous time and the interval between LED states.
  2. Initialization: In the setup() function, initialize the LED pin as an output and set the initial LED state to off. Additionally, set the button pin as an input.
  3. Main Loop: In the loop() function, check if the button is pressed. If the button is pressed and released, toggle the LED state and update the previous time to the current time using the millis() function.
  4. LED Blinking: Within the main loop, use an if statement to check if the elapsed time since the last LED state change exceeds the predefined interval. If it does, toggle the LED state and update the previous time.
  5. Delay: Finally, add a small delay (e.g., 10 milliseconds) to reduce the potential for button bounce or rapid state changes.

Applications

  1. Visual Indication: The LED blinking technique uses to provide visual indications in various applications. For example, it can be implemented in alarm systems, notifying users about specific events such as an intrusion or low battery.
  2. User Interface Feedback: In user interface design, LED blinking can be utilized to provide feedback to users. It can indicate the status of certain actions or processes, such as confirming a successful operation or notifying an error.
  3. Signaling Systems: LED blinking can be incorporated into signaling systems, such as traffic lights, pedestrian crossings, or railway signals. It ensures clear and recognizable visual cues to guide users or drivers.
  4. Prototyping and Testing: The LED Blink Without Delay project is a great way to practice coding and gain experience with Arduino. It uses as a starting point for prototyping and testing various concepts before implementing them in larger projects.
  5. Educational Purposes: This project is also useful for educational purposes, as it introduces the concept of non-blocking programming and the millis() function. It helps beginners understand the importance of timing in programming and how to overcome the limitations of delay-based approaches.

Conclusion

The Arduino LED Blink project is a captivating introduction to the world of Arduino programming and electronics. By connecting an LED to the Arduino UNO board and writing a simple code, you can bring life and visual appeal to your projects. Through this project, you have learned about the specifications and features of LEDs, the working principle of LED blinking, and the various applications it finds in different domains. So, grab your Arduino board, and an LED, and start experimenting with LED blinking to illuminate your projects with creativity and innovation. Enjoy your journey into the fascinating realm of Arduino!

Leave a Comment


error: