• 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 » Read a Pushbutton with Arduino with Interrupts and Debounce

By Abhishek Ghosh May 20, 2024 6:51 am Updated on May 20, 2024

Read a Pushbutton with Arduino with Interrupts and Debounce

Advertisement

Pushbuttons are ubiquitous components used for user input in Arduino projects. However, reading pushbutton inputs reliably can be challenging due to issues such as contact bounce and the need for responsive behaviour. In this article, we’ll explore how to read pushbutton inputs using interrupts and debounce techniques, ensuring accurate and responsive interaction with Arduino microcontrollers.

Those who are not completely sure what interrupts and bounce are, must read the below articles:

  • What is Interrupt
  • How Interrupt Work
  • Understanding Arduino Interrupt
  • Contact Bounce of Pushbuttons

Our earlier example projects were examples of utilizing interrupt function:

Advertisement

---

  • Arduino Interrupt: Blink LED and Beep Every 1 Second, Pauses Upon Button Press
  • Arduino 3V DC Mini Pump with Push Button Control and LED Indicators (For Kids)

The second project was intended for the children. I avoided to make the circuit more complicated.

Pushbuttons, also known as momentary switches, are mechanical switches that make or break electrical connections when pressed. They are commonly used for tasks such as toggling LEDs, triggering actions, and navigating menus in Arduino projects. Challenges with Pushbutton Input:

Contact Bounce: When a pushbutton is pressed or released, its contacts bounce against each other multiple times before settling in a stable state. This bouncing phenomenon can lead to multiple false readings, causing erratic behaviour in the Arduino.

Responsiveness: In applications where rapid or precise response is required, such as gaming or real-time control, delays caused by software polling of pushbutton inputs may be unacceptable.

Interrupts offer a more efficient way to handle pushbutton inputs by allowing the Arduino to respond immediately to changes in the button state, without continuously polling the button status in the main loop. Here’s how to use interrupts to read pushbutton inputs:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int buttonPin = 2;  // Pin connected to the pushbutton
volatile bool buttonState = LOW; // Current state of the button
 
void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, CHANGE); // Attach interrupt
}
 
void loop() {
  // Your main loop code here
}
 
void buttonInterrupt() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);
  // Your code to handle button state change here
}

If you use an oscilloscope, you’ll see the theoretical problem as real:

Read a Pushbutton with Arduino with Interrupts and Debounce

When testing our setup, we would expect the variable to increase by one each time. But we will see that it jumps several times. That saw tooth like |||| peaks are the effect of the bounce. It can generate multiple interrupts every time we push the button. The result can kill the reliability of the pushbutton – which can get worse with the ageing of the pushbutton.

This is the reason we need debouncing.

 

How We Can Read a Pushbutton Both with Interrupts and Debounce?

 

Debouncing without using interrupts is just easy. From the above-explained logic, we can not avoid utilizing the interrupt function. But adding software debouncing alone invites a lot of trouble. That is why I will suggest thinking about adding a hardware debounce. It is not only a robust solution but also avoids the disadvantage of increasing the coding complexity of our setup. Of course, we can add software debounce to the above sketch in this way:

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
const int buttonPin = 2;  // Pin connected to the pushbutton
const int debounceDelay = 50; // Debounce delay in milliseconds
unsigned long lastDebounceTime = 0; // Last time the button state changed
volatile bool buttonState = LOW; // Current state of the button
 
void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, CHANGE); // Attach interrupt
}
 
void loop() {
  // Your main loop code here
}
 
void buttonInterrupt() {
  // Read the state of the button
  int reading = digitalRead(buttonPin);
 
  // Check if the button state has changed and debounce
  if (reading != buttonState) {
    lastDebounceTime = millis(); // Update debounce time
  }
 
  if (millis() - lastDebounceTime > debounceDelay) {
    // Update button state only if the button state has been stable for the debounce delay
    buttonState = reading;
    // Your code to handle button state change here
  }
}

The Schmitt trigger works similarly to an analog comparator but compares an input voltage and one of two possible threshold voltages. This makes it a threshold switch: if the upper threshold voltage is exceeded, the output takes on the maximum possible output voltage (HIGH) in the non-inverting version; as a binary number, it is coded as 1 for positive logic and 0 for negative logic. If the threshold voltage falls below the lower threshold, the output assumes the minimum possible output voltage (LOW).

The reverse is true for the inverting Schmitt trigger: If the input voltage exceeds the upper switching threshold of the Schmitt trigger, its output voltage tilts from the maximum voltage value to the minimum voltage value (LOW). If the input voltage then falls below the lower switching threshold, the output voltage tilts back to the maximum output voltage (HIGH).

The easiest way to apply hardware debounce is to place a 1uF capacitor in parallel with the push button when the circuit is not time-critical. In layman’s language – you are shorting the positive and negative ends of the push button with a capacitor. This is the easiest design:

Pushbutton with Arduino with Interrupts and Debounce

The problem of that solution you can say will arise from deliberately holding the push button.

Practically, there is internal resistance in the capacitor plus the wires themselves will have some small resistance. Take it as 1 ohm. When you close the switch you have 3 to 5 volts discharging into 1 ohm = 5A. After one time constant this current drops by 63%. After 5 time constants (50us) the current is zero. Most small circuit wiring can handle this current for this duration. The inductance of the wiring and the capacitor plates will limit any current surge in the circuit.

Just add a 10-ohm resistor in series with the capacitor to the ground to get rid of fear.

Arduino with Interrupts and Debounce

For time-critical operations, it is better to use a 74HC14 chip for using the Schmitt trigger theory.

Tagged With injtk
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 Read a Pushbutton with Arduino with Interrupts and Debounce

  • Understanding Arduino Interrupts

    In the world of embedded systems and microcontroller programming, achieving real-time responsiveness is often a critical requirement. Whether it’s reading sensor data, detecting external events, or controlling actuators, the ability to respond swiftly and accurately can make all the difference. This is where Arduino interrupts come into play, offering a powerful mechanism to enhance control […]

  • 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.

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

    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 […]

  • How Interrupt Works

    In previous article, we have discussed the basics of interrupt. In order to be able to trigger an interrupt, the hardware connected to the main processor (CPU) must be interrupt-capable, i.e. generate an output signal (electrical voltage at an output pin) via the so-called interrupt line when a certain event occurs. The CPU generally has […]

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