Sub topic 9
Debouncing in Software with Timestamps
Assume you can write an Arduino sketch with setup() and loop(), configure pins with pinMode(), read inputs with digitalRead(), and use if statements and Boolean variables. This chapter uses an Uno-style board and a pushbutton connected between pin 2 and GND.
The timestamp method
A mechanical button does not change cleanly from open to closed. Its contacts may touch and separate several times during a few milliseconds. The sketch sees a rapid sequence of HIGH and LOW readings instead of one clean transition.
Software debouncing accepts a new button state only when the raw reading has stayed unchanged for a chosen interval. The interval is usually between 10 and 50 milliseconds.
The algorithm keeps three pieces of information:
lastReading: the most recent raw value fromdigitalRead().stableState: the last value accepted as reliable.lastChangeTime: when the raw reading most recently changed.
When the raw reading changes, restart the timer. When it remains unchanged long enough, accept it as the stable state.
flowchart TD
A[Read button] --> B{Reading changed?}
B -- Yes --> C[Save reading and restart timestamp]
B -- No --> D{Stable for debounce interval?}
C --> E[Continue loop]
D -- No --> E
D -- Yes --> F{Different from stable state?}
F -- Yes --> G[Accept new stable state]
F -- No --> E
G --> H{Stable state is LOW?}
H -- Yes --> I[Handle one press event]
H -- No --> E
I --> EA debounced button
With INPUT_PULLUP, the Arduino uses an internal pull-up resistor. The pin reads HIGH while the button is open and LOW while the button connects the pin to GND. This is called active-low logic.
const uint8_t BUTTON_PIN = 2;
const unsigned long DEBOUNCE_MS = 30;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChangeTime = 0;
bool ledState = LOW;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastChangeTime = millis();
lastReading = reading;
}
if (millis() - lastChangeTime >= DEBOUNCE_MS &&
reading != stableState) {
stableState = reading;
if (stableState == LOW) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
}
}
}The LED changes only when the stable state becomes LOW. Therefore, holding the button down does not repeatedly toggle the LED. Releasing the button changes the stable state back to HIGH, but that transition does not trigger the press action.
| Variable | Meaning |
|---|---|
| `reading` | The current, possibly noisy pin value |
| `lastReading` | The previous raw pin value |
| `stableState` | The last accepted button state |
| `lastChangeTime` | The timestamp of the latest raw transition |
Common errors
Do not reset lastChangeTime on every pass through loop(). The timer must restart only when the raw reading changes. Otherwise, the elapsed time never reaches the debounce interval.
Do not use the raw reading alone to trigger a press. A held button produces LOW on every loop pass. Compare the accepted state with stableState so the action happens once per transition.
Use unsigned elapsed-time arithmetic:
if (millis() - lastChangeTime >= DEBOUNCE_MS) {
// The reading has been unchanged long enough.
}This form remains correct when the millis() counter rolls over. Avoid comparing millis() with a precomputed future deadline.
Exercise
With INPUT_PULLUP, what value represents a pressed button, and why does the example toggle the LED only when the stable state changes to that value?
Show answer
A pressed button reads LOW because it connects the input pin to GND. Comparing the new stable state with the previous stable state detects one transition, so a held button cannot trigger repeated toggles.