Arduino UNO R4 - Blink LED Without Delay

by DIYables in Circuits > Arduino

71 Views, 0 Favorites, 0 Comments

Arduino UNO R4 - Blink LED Without Delay

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-blink-led-without-delay

This build blinks an LED on the Arduino UNO R4 while also watching a push button, without using delay(). A delay()-based blink can block the processor long enough to miss a button press. Here you wire an LED and a button, then compare a delay() version against a non-blocking version so the Arduino catches every press while still blinking the LED on schedule.

Supplies

Wiring

Connect the LED to pin 3 of the Arduino UNO R4, through a 220Ω resistor if you are using a bare LED (an LED module with a built-in resistor skips this part). Connect the button to pin 7. The button pin is configured with INPUT_PULLUP in code, so no external pull-up resistor is needed. Power the board from your computer over USB.

How It Works

The first sketch toggles the LED and then calls delay(1000) before checking the button. Any button press that happens during that one-second delay is missed, because the processor is stuck waiting. The second sketch avoids delay() so the loop keeps running continuously, letting it read the button on every pass while still blinking the LED at the same one-second interval. This is the difference between blocking and non-blocking code on the same hardware.

Code

The delay() version flips led_state, writes it to LED_PIN, waits 1000ms with delay(), then reads BUTTON_PIN and prints the state to Serial only when it changes. The non-blocking version restructures this logic so the LED timing and the button check both happen inside a loop that never stalls, so presses during the blink interval are not lost. A further example extends this pattern to blink two LEDs on different schedules while also checking the button, showing how to add more tasks to the same non-blocking loop.

arduino-uno-r4-blink-led-without-delay.ino - the sketch from the tutorial, unchanged.

/*
* This Arduino UNO R4 code was developed by newbiely.com
*
* This Arduino UNO R4 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-blink-led-without-delay
*/

#define LED_PIN 3 // The Arduino UNO R4 pin connected to the LED
#define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button

#define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds)

int led_state = LOW; // led_state used to set the LED
int prev_button_state = 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_PULLUP);
}

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

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

delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect

int button_state = digitalRead(BUTTON_PIN);

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

// save the last state of button
prev_button_state = button_state;
}

// DO OTHER WORKS HERE
}

Things That Go Wrong

With the delay() sketch, press the button four times during testing and watch the Serial Monitor: some presses will not appear because the Arduino was inside delay() and could not read the pin. If you see fewer printed states than button presses, that confirms the blocking behavior. Switching to the non-blocking sketch and repeating the test should show all four presses logged.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-blink-led-without-delay