• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Arduino Interrupt: Blink LED and Beep Every 1 Second, Pauses Upon Button Press

By Abhishek Ghosh May 16, 2024 10:30 pm Updated on May 16, 2024

Arduino Interrupt: Blink LED and Beep Every 1 Second, Pauses Upon Button Press

Advertisement

Though the previous few articles, I have explained some theories required for embedded projects, which include three longer articles on Interrupt – What is Interrupt, How Interrupt Works and Understanding Arduino Interrupts. If you have not read those articles and are not sure what I am talking about, then kindly read the three articles after testing the code in this guide.

Our target of this guide is to blink the LED and beep every 1 second with pauses upon button press. That means, our wish is when we upload the sketch to Arduino UNO, it will start to blink the LED and make a ko ko buzzer sound. When the ko ko buzzer sound will disturb you, pressing the push button once should instantly stop the thing. Again after a 10-second gap, the sketch will blink the LED and make a ko ko buzzer sound.

The thing sounds very easy. But unless you know the theories, I will show you that the thing will not become perfect. Improper programming can make the physical buttons of electronic gadgets not work correctly.

Advertisement

---

Interrupt is analogous to interfering with a human doing something important. Suppose you are reading this webpage and your cat is trying to grab your attention or Amazon delivery is ringing the doorbell. Their action will be interrupted in that case. If there are a lot of delivery men ringing the doorbell and you have a lot of cats, then your main action will become erratic.

For this project, you need:

  1. Arduino UNO R3 development board
  2. One Breadboard
  3. Few jumpers
  4. One LED
  5. One Pushbutton
  6. One 220 Ohm resistor
  7. One 1K Ohm resistor
  8. One buzzer
  9. A computer with Arduino IDE installed

The connection will be like the below illustration:

Arduino Interrupt Blink LED and Beep Every 1 Second, Pauses Upon Button Press

The push button must be connected with digital pin 2 of Arduino UNO. Because the chart of this above-mentioned article tells us that Arduino UNO board provides attachInterrupt function on D2 and D3. So, our option to attach push button is either digital pin 2 or digital pin 3. I have connected the LED to digital pin 13 and the buzzer to pin 8.

Now, play this simulation:

It works exactly in the way we have wanted. This is the correct method and this is the sketch:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Define pin numbers
const int ledPin = 13;        // LED connected to digital pin 13
const int buzzerPin = 8;      // Buzzer connected to digital pin 8
const int buttonPin = 2;      // Push button connected to digital pin 2
 
volatile bool buttonPressed = false;  // Flag to indicate button press
 
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button pin
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING); // Attach interrupt to button pin
}
 
void loop() {
  if (!buttonPressed) {
    // Blink LED and beep every 1 second
    digitalWrite(ledPin, HIGH);   // Turn on the LED
    tone(buzzerPin, 1000);         // Play a 1kHz tone on the buzzer
    delay(500);                    // Wait for 0.5 second
    digitalWrite(ledPin, LOW);     // Turn off the LED
    noTone(buzzerPin);             // Stop the tone
    delay(500);                    // Wait for 0.5 second
  } else {
    // Pause for 10 seconds upon button press
    digitalWrite(ledPin, LOW);   // Turn off the LED
    noTone(buzzerPin);           // Stop the tone
    delay(10000);                // Pause for 10 seconds
    buttonPressed = false;       // Reset buttonPressed flag after pause
  }
}
 
// Interrupt service routine for button press
void buttonInterrupt() {
  buttonPressed = true; // Set the buttonPressed flag
}

In the variable declarations, we define integer variables to store the PINs for the LED, buzzer, and push button. volatile bool buttonPressed is a flag to indicate whether the button has been pressed.

In the setup() function, we set the pinMode for the LED and buzzer pins as OUTPUT and the button pin as INPUT_PULLUP to enable the internal pull-up resistor. We attach an interrupt to the button pin using attachInterrupt() function, specifying the interrupt service routine (ISR) buttonInterrupt() to be called on the falling edge (when the button is pressed).

In the loop function, if buttonPressed is false, indicating no button press, weblink the LED and beep every 1 second. If buttonPressed is true, indicating a button press, we pause for 10 seconds. During the pause, the LED remains off, and the buzzer remains silent. After the pause, we reset buttonPressed to false to prepare for the next button press.

Button Interrupt Function: The buttonInterrupt() function is an interrupt service routine (ISR) called whenever the button is pressed. Inside this function, we set the buttonPressed flag to true to indicate that the button has been pressed.

If we add millis() in a wrong manner, then the below will be the sketch. Test the sketch, it will behave erratically. It is not wrong to use millis(), micros() or delay() within an interrupt routine. The previous usage was logically simple, that is why it worked.

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Define pin numbers
const int ledPin = 13;        // LED connected to digital pin 13
const int buzzerPin = 8;      // Buzzer connected to digital pin 8
const int buttonPin = 2;      // Push button connected to digital pin 2
 
volatile bool buttonPressed = false;  // Flag to indicate button press
unsigned long previousMillis = 0;     // Variable to store previous millis value
const unsigned long interval = 1000;  // Interval for LED blinking and beeping (1 second)
const unsigned long pauseDuration = 10000;  // Duration of pause upon button press (10 seconds)
 
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for button pin
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING); // Attach interrupt to button pin
}
 
void loop() {
  unsigned long currentMillis = millis(); // Get the current time
  if (!buttonPressed) {
    // Blink LED and beep every 1 second
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;  // Save the last time we blinked the LED
      digitalWrite(ledPin, !digitalRead(ledPin));  // Toggle the LED state
      if (digitalRead(ledPin)) {
        tone(buzzerPin, 1000);  // Start playing a tone
      } else {
        noTone(buzzerPin);  // Stop playing the tone
      }
    }
  } else {
    // Pause for 10 seconds upon button press
    if (currentMillis - previousMillis >= pauseDuration) {
      previousMillis = currentMillis;  // Reset the previousMillis
      buttonPressed = false;  // Reset the buttonPressed flag
    }
  }
}
 
// Interrupt service routine for button press
void buttonInterrupt() {
  buttonPressed = true; // Set the buttonPressed flag
}

Because Arduino’s programming language (that is an implementation of C++) has a lot of abstraction, it is not always possible to explain what is happening (by people who are not engineers related to this field).
Probably before an interrupt handler begins, AVR hardware disables interrupts. Interrupts are written in C code in the Arduino and are not capable of correctly handling multiple overlapping executions within the same handler (that is called reentrant). The correct method is to rewrite the library in C to solve your issues, but that approach will consume time.

If you think that you’ll avoid interrupting and write a simple code, that will also behave erratically.

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Arduino Interrupt: Blink LED and Beep Every 1 Second, Pauses Upon Button Press

  • Arduino Blink LED With Pushbutton Control to Turn ON and Off

    Arduino Blink LED With Pushbutton Control to Turn ON and Off is Few Steps Higher Than Basic Example. There is Matter of Repeat Checking by Microcontroller.

  • How to Blink an LED Only Twice After a Button Press Using Arduino

    Blinking an LED is one of the most basic projects for Arduino beginners. Previously, we have demonstrated how to Blink LED and Beep Every X Seconds Upon Push Button Press. However, adding a condition to blink the LED only a certain number of times after a button press introduces a new level of complexity. In […]

  • Make Beep Sound in Arduino Project Upon Push Button Press

    Here is How to Make Beep Sound in Arduino Project Upon Push Button Press. It is Practical For Many Projects Including LED Clocks.

  • LED Chaser Effect With PWM Using Arduino

    In our earlier article, We have discussed PWM and informed you that this PWD technology is used in particular for light-emitting diodes (LEDs), as they are often used as backlights on mobile phones or brake lights in newer motor vehicles. Our older article on Breathing LED also uses PWM. This was the sketch, LED is […]

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

vpsdime

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Cloud-Powered Play: How Streaming Tech is Reshaping Online GamesSeptember 3, 2025
  • How to Use Transcribed Texts for MarketingAugust 14, 2025
  • nRF7002 DK vs ESP32 – A Technical Comparison for Wireless IoT DesignJune 18, 2025
  • Principles of Non-Invasive Blood Glucose Measurement By Near Infrared (NIR)June 11, 2025
  • Continuous Non-Invasive Blood Glucose Measurements: Present Situation (May 2025)May 23, 2025
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2026 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy