0
0
AutocadHow-ToBeginner · 4 min read

How to Debounce Button in Arduino: Simple Guide and Example

To debounce a button in Arduino, use a small delay or check the button state after a short pause to ignore rapid changes caused by mechanical bounce. Implement this by reading the button state, waiting about 50 milliseconds, and then reading it again to confirm the press with millis() timing or a simple delay.
📐

Syntax

Debouncing a button typically involves reading the button state, waiting a short time, and then reading it again to confirm the press. The common pattern uses variables to track the button state and timing with millis().

  • buttonPin: The Arduino pin connected to the button.
  • buttonState: Current reading of the button.
  • lastButtonState: Previous reading to detect changes.
  • lastDebounceTime: Time when the button state last changed.
  • debounceDelay: Delay time to ignore bouncing (usually 50 ms).
arduino
const int buttonPin = 2;          // Pin connected to the button
int buttonState = LOW;            // Current button state
int lastButtonState = LOW;       // Previous button state
unsigned long lastDebounceTime = 0;  // Last time button state changed
unsigned long debounceDelay = 50;    // Debounce delay in milliseconds
💻

Example

This example shows how to debounce a button connected to pin 2. It prints "Button pressed" only once per press, ignoring the noise caused by bouncing.

arduino
const int buttonPin = 2;
int buttonState = LOW;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop() {
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        Serial.println("Button pressed");
      }
    }
  }

  lastButtonState = reading;
}
Output
Button pressed
⚠️

Common Pitfalls

Common mistakes when debouncing buttons include:

  • Not using any debounce logic, causing multiple triggers from one press.
  • Using delay() in the main loop, which can block other code.
  • Not tracking previous button state, so repeated presses are missed or misread.

Always use millis() for non-blocking debounce timing and compare current and previous states.

arduino
/* Wrong way: Using delay() blocks other code and can miss presses */
void loop() {
  if (digitalRead(buttonPin) == HIGH) {
    delay(50); // blocks program
    if (digitalRead(buttonPin) == HIGH) {
      Serial.println("Button pressed");
    }
  }
}

/* Right way: Use millis() to debounce without blocking */
// See example section for correct code
📊

Quick Reference

Tips for debouncing buttons in Arduino:

  • Use a debounce delay of about 50 milliseconds.
  • Track previous and current button states to detect changes.
  • Use millis() for timing to avoid blocking code.
  • Confirm button state after debounce delay before acting.
  • Test with your specific button and wiring as bounce times vary.

Key Takeaways

Use a debounce delay (around 50 ms) to ignore rapid button state changes caused by mechanical bounce.
Track both current and previous button states to detect real presses.
Use millis() for timing to avoid blocking other code with delay().
Confirm the button state after the debounce delay before triggering actions.
Test your debounce code with your actual hardware for best results.