0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Debounce Button in Embedded C: Simple Guide

To debounce a button in embedded C, use a small delay after detecting a button press or check the button state multiple times before confirming the press. This prevents multiple triggers caused by mechanical bouncing of the button contacts.
📐

Syntax

Debouncing typically involves reading the button input, waiting for a short delay, and then reading it again to confirm the state. The basic pattern is:

  • read_button(): Read the current button state.
  • delay_ms(time): Wait for a short time (e.g., 10-50 ms).
  • Check the button state again to confirm it is stable.
c
int read_button();
void delay_ms(int ms);

int debounce_button() {
    if (read_button() == PRESSED) {
        delay_ms(20); // wait 20 milliseconds
        if (read_button() == PRESSED) {
            return 1; // confirmed press
        }
    }
    return 0; // no press
}
💻

Example

This example shows a simple debounce function that reads a button connected to a microcontroller pin. It waits 20 ms after detecting a press and checks again to confirm the press.

c
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h> // for usleep

#define PRESSED 0
#define RELEASED 1

// Simulated button state (0 = pressed, 1 = released)
int button_state = RELEASED;

// Simulate reading the button pin
int read_button() {
    return button_state;
}

// Delay function in milliseconds
void delay_ms(int ms) {
    usleep(ms * 1000); // usleep takes microseconds
}

// Debounce function
int debounce_button() {
    if (read_button() == PRESSED) {
        delay_ms(20); // wait 20 ms
        if (read_button() == PRESSED) {
            return 1; // button press confirmed
        }
    }
    return 0; // no press
}

int main() {
    // Simulate button press
    button_state = PRESSED;
    if (debounce_button()) {
        printf("Button Press Detected\n");
    } else {
        printf("No Button Press\n");
    }
    return 0;
}
Output
Button Press Detected
⚠️

Common Pitfalls

Common mistakes when debouncing buttons include:

  • Not adding any delay, causing multiple triggers from one press.
  • Using too short or too long delay, either missing presses or slowing response.
  • Checking the button state only once without confirmation.

Always confirm the button state after a short delay to ensure stable input.

c
/* Wrong way: No delay, causes multiple triggers */
int debounce_wrong() {
    if (read_button() == PRESSED) {
        return 1; // triggers multiple times due to bounce
    }
    return 0;
}

/* Right way: Confirm after delay */
int debounce_right() {
    if (read_button() == PRESSED) {
        delay_ms(20);
        if (read_button() == PRESSED) {
            return 1;
        }
    }
    return 0;
}
📊

Quick Reference

Tips for button debouncing in embedded C:

  • Use a delay of 10-50 ms after detecting a press.
  • Check the button state before and after the delay.
  • Consider using hardware debouncing or interrupts for advanced cases.
  • Keep debounce code simple and test on your hardware.

Key Takeaways

Always confirm button press after a short delay to avoid false triggers.
Use a delay between 10 and 50 milliseconds for effective debouncing.
Avoid checking button state only once without confirmation.
Test debounce timing on your actual hardware for best results.
Simple delay-based debounce works well for most embedded button inputs.