How to Debounce Keypad in Embedded C: Simple Guide
To debounce a keypad in embedded C, use a small delay after detecting a key press or check the key state multiple times before confirming the press. This prevents multiple detections caused by mechanical bouncing of the keys using simple
delay() or state verification loops.Syntax
Debouncing a keypad typically involves reading the key state, waiting for a short delay, and then reading the state again to confirm the key press.
Basic steps:
read_keypad(): Function to read the current key pressed.delay(ms): Wait for a specified time in milliseconds.- Compare initial and delayed readings to confirm stable key press.
c
char debounce_keypad() {
char key1 = read_keypad();
if (key1 != '\0') { // Key pressed
delay(20); // Wait 20 ms for debounce
char key2 = read_keypad();
if (key1 == key2) {
return key1; // Confirmed key press
}
}
return '\0'; // No valid key press
}Example
This example shows how to debounce a keypad by reading the key, waiting 20 milliseconds, and confirming the key is still pressed before accepting it.
c
#include <stdio.h> #include <stdbool.h> #include <time.h> // Simulated keypad read function char read_keypad() { // For demo, returns 'A' once, then '\0' static bool pressed = false; if (!pressed) { pressed = true; return 'A'; } return '\0'; } // Simple delay function in milliseconds void delay(int ms) { clock_t start = clock(); while ((clock() - start) * 1000 / CLOCKS_PER_SEC < ms) {} } char debounce_keypad() { char key1 = read_keypad(); if (key1 != '\0') { delay(20); // debounce delay char key2 = read_keypad(); if (key1 == key2) { return key1; } } return '\0'; } int main() { char key = debounce_keypad(); if (key != '\0') { printf("Key Pressed: %c\n", key); } else { printf("No valid key press detected.\n"); } return 0; }
Output
Key Pressed: A
Common Pitfalls
Common mistakes when debouncing keypad inputs include:
- Not adding any delay, causing multiple detections of one press.
- Using too short or too long delay, either missing presses or slowing response.
- Not confirming the key state after delay, leading to false triggers.
Always confirm the key state after a short delay to ensure stable input.
c
/* Wrong way: No debounce delay, causes multiple detections */ char read_keypad_no_debounce() { return read_keypad(); // Direct read without delay } /* Right way: Add delay and confirm key state */ char read_keypad_with_debounce() { char key1 = read_keypad(); if (key1 != '\0') { delay(20); char key2 = read_keypad(); if (key1 == key2) { return key1; } } return '\0'; }
Quick Reference
- Use a 10-50 ms delay after detecting a key press.
- Confirm the key state after delay to avoid false triggers.
- Keep debounce logic simple for responsiveness.
- Test with your specific keypad hardware as bounce times vary.
Key Takeaways
Add a short delay (10-50 ms) after detecting a key press to debounce.
Confirm the key state after the delay to ensure the press is stable.
Avoid reading the keypad only once without delay to prevent multiple detections.
Adjust debounce delay based on your keypad's mechanical characteristics.
Keep debounce code simple for better responsiveness in embedded systems.