0
0
Embedded Cprogramming~10 mins

Button debouncing in software in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Button debouncing in software
Button Press Detected
Start Debounce Timer
Wait for Timer to Expire
Read Button State Again
Ignore Noise
Process Button Press
End
This flow shows how software waits after a button press to ignore quick noise, then confirms the press before acting.
Execution Sample
Embedded C
if (button_pressed()) {
  delay_ms(20); // debounce delay
  if (button_pressed()) {
    handle_press();
  }
}
This code waits 20 ms after detecting a press, then checks again to confirm the press before handling it.
Execution Table
StepActionButton State ReadTimerDecisionOutput
1Detect button pressPressedTimer started (20 ms)WaitNone
2Wait 20 msN/ACounting downWaitNone
3Read button state againPressedTimer expiredStable press confirmedCall handle_press()
4Process pressPressedTimer stoppedHandledButton press processed
5Next loopReleasedTimer readyNo pressNo action
💡 Button state stable after debounce delay, press confirmed and processed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
button_stateReleasedPressedPressedPressedPressedReleased
debounce_timer020 ms startedCounting downExpiredStopped0
press_confirmedFalseFalseFalseTrueTrueFalse
Key Moments - 2 Insights
Why do we check the button state twice with a delay in between?
Because the button can 'bounce' causing quick on/off signals. The first check starts the timer, the second confirms the press after noise settles (see execution_table steps 1 and 3).
What happens if the button state changes during the delay?
If the button is not stable after the delay, the press is ignored to avoid false triggers (see execution_table step 3 decision).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the button state at Step 3 when the press is confirmed?
AReleased
BPressed
CUnknown
DFloating
💡 Hint
Check the 'Button State Read' column at Step 3 in the execution_table.
At which step does the debounce timer expire?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Timer' and 'Decision' columns to find when the timer expires.
If the button state was 'Released' at Step 3, what would happen?
Ahandle_press() would be called
BTimer would restart
CThe press would be ignored
DProgram would crash
💡 Hint
Refer to the 'Decision' column at Step 3 and the explanation in key_moments.
Concept Snapshot
Button debouncing avoids false triggers from noisy button signals.
Detect press, start a short delay (e.g., 20 ms).
After delay, read button again.
If stable pressed, confirm and handle press.
If not stable, ignore noise.
Simple software delay and double-check method.
Full Transcript
Button debouncing in software means ignoring quick, noisy changes when a button is pressed. The program first detects a press, then waits a short time (like 20 milliseconds) to let the noise settle. After waiting, it checks the button again. If the button is still pressed, it confirms the press and runs the code to handle it. If the button is not pressed anymore, it ignores the noise and does nothing. This prevents the program from reacting to false presses caused by the button's mechanical bounce.