How to Debounce a Button in Arduino (Software & Hardware Methods)?

 Button debouncing is essential to avoid false triggers caused by mechanical switch noise. Below are 3 reliable debouncing techniques for Arduino, from simplest to most robust.




✅ Method 1: Simple Delay-Based Debouncing (Easy but Blocking)

Best for: Quick prototyping (not for real-time systems).

cpp

const int buttonPin = 2;  
int buttonState = HIGH;  

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

void loop() {  
  int reading = digitalRead(buttonPin);  
  if (reading != buttonState) {  
    delay(50); // Debounce delay (adjust as needed)  
    if (reading == digitalRead(buttonPin)) {  
      buttonState = reading;  
      if (buttonState == LOW) {  
        Serial.println("Button pressed!");  
      }  
    }  
  }  
}  

Pros:
✔ Simple to implement.
Cons:
❌ Blocks code execution during delay().



✅ Method 2: Non-Blocking Millis() Debouncing (Better for Real-Time)

Best for: Projects needing multitasking.

cpp

const int buttonPin = 2;  
int buttonState = HIGH;  
int lastButtonState = HIGH;  
unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50; // ms  

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

void loop() {  
  int reading = digitalRead(buttonPin);  
  if (reading != lastButtonState) {  
    lastDebounceTime = millis(); // Reset timer on change  
  }  

  if ((millis() - lastDebounceTime) > debounceDelay) {  
    if (reading != buttonState) {  
      buttonState = reading;  
      if (buttonState == LOW) {  
        Serial.println("Button pressed!");  
      }  
    }  
  }  
  lastButtonState = reading;  
}  

Pros:
✔ Non-blocking (uses millis()).
✔ Works well with other tasks.



✅ Method 3: Hardware Debouncing (Most Reliable)

Best for: Noise-sensitive or high-reliability applications.

Option A: RC Filter (Low-Cost)

  • Add a 0.1µF capacitor between button and GND.

  • Use a 10kΩ pull-up resistor (or INPUT_PULLUP).

Option B: Schmitt Trigger (Advanced Noise Immunity)

  • Use a 74HC14 (hex inverter with hysteresis).

Circuit Example:


[Button] --> [10kΩ Resistor] --> [Arduino Pin]  
               |  
              [0.1µF Cap] --> GND  

Pros:
✔ Eliminates software delays.
✔ Handles extreme noise better.



🔍 How Debouncing Works

  1. Mechanical switches "bounce" (generate noise for ~1–50ms).

  2. Software filtering ignores rapid changes.

  3. Hardware filtering (RC circuit) smooths the signal.



🚫 Common Mistakes to Avoid

❌ Using delay() in real-time systems → Freezes the MCU.
❌ Too short debounce time → Still detects noise.
❌ No pull-up resistor → Floating pin causes erratic readings.



🎯 Which Method Should You Use?

ScenarioRecommended Method
Quick prototypingMethod 1 (Delay)
Multitasking projectsMethod 2 (Millis)
High-reliability systemsMethod 3 (Hardware)


💡 Pro Tip:

For multiple buttons, use a library like:

评论

此博客中的热门博文

What is a DSP? (Digital Signal Processor)

How To Connect Stm32 To PC?

MCU clock configuration and external crystal oscillator selection