Arduino - LED - Blink Without Delay
by DIYables in Circuits > Arduino
13 Views, 0 Favorites, 0 Comments
Arduino - LED - Blink 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://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay
This build blinks an LED on a fixed interval while also watching a button, without using delay(). A delay()-based blink blocks Arduino from checking the button during the wait, so presses get missed. This guide wires up the same LED and button and shows the millis()-based approach that lets Arduino do both tasks without blocking.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- LED Kit
- LED (red)
- LED Module
- Button and LED Kit
- 220Ω Resistor
- Breadboard-mount Button with Cap
- Breadboard-mount Button Kit
- Panel-mount Push Button
- Push Button Module
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Connect the LED to pin 3 and the button to pin 7. Using an LED module with a built-in resistor simplifies the wiring since you don't need a separate 220Ω resistor on the breadboard. Wire the button so its state can be read as HIGH or LOW on pin 7.
How It Works
With delay(), Arduino turns the LED on or off, then calls delay(1000) before checking the button. Any press that happens during that one-second delay is missed because the processor is stuck waiting. The without-delay version uses millis() to track elapsed time instead of blocking, so the loop keeps running continuously and can check the button on every pass, catching every press while still blinking the LED on schedule.
Code
The delay() sketch toggles ledState, writes it to LED_PIN, then calls delay(BLINK_INTERVAL) before reading the button and printing its state if it changed. The without-delay sketch replaces that blocking delay with a millis()-based timer: it compares the current time against the last time the LED changed, and only toggles the LED once BLINK_INTERVAL has passed, while the button is read every loop iteration regardless of the blink timing. A follow-on example extends this same pattern to blink two LEDs at different intervals while still checking the button.
arduino-led-blink-without-delay.ino - the sketch from the tutorial, unchanged.
Things That Go Wrong
If you press the button during the delay() version's one-second delay, that press won't show up in the Serial Monitor since the processor isn't reading the pin at that moment. If you see missed presses when testing with four button pushes, you're running the delay() version; switch to the millis() version and all four presses should register.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay