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
Everything below is what the build uses. The links go to the parts we make and test.
- Arduino UNO R4 WiFi
- DIYables STEM V4 IoT, Compatible with Arduino Uno R4 WiFi
- DIYables STEM V4B IoT, Compatible with Arduino Uno R4 WiFi
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (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 R4
- Sensors/Servo Expansion Shield for Arduino UNO R4
- Breadboard Shield for Arduino UNO R4
- Enclosure for Arduino UNO R4
- Power Splitter for Arduino UNO R4
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V4 IoT Starter Kit (Arduino included)
- DIYables STEM V4B IoT Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
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.
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