Sub topic 9
Why one press can look like many
Assume you know how to use setup() and loop(), configure a pin with pinMode, read it with digitalRead, and use INPUT_PULLUP. The button is wired between pin 2 and GND, so an unpressed button reads HIGH and a pressed button reads LOW.
A push button is a mechanical switch. When you press it, two metal contacts should close and create one clean electrical connection. In practice, the contacts hit, separate, and hit again several times before settling. This rapid switching is called contact bounce.
The microcontroller reads much faster than a person can press a button. A single physical press can therefore appear as several changes between HIGH and LOW.
The exact waveform depends on the switch. Bounce often lasts for a few milliseconds, but it can last longer. The important point is that the electrical signal is temporarily ambiguous even though the user intended one action.
What the Arduino sees
Suppose the button uses INPUT_PULLUP. During one press, the pin might produce this sequence:
| Time | Pin reading | Possible interpretation |
|---|---|---|
| 0 ms | HIGH | Button released |
| 10 ms | LOW | Button pressed |
| 11 ms | HIGH | Button released |
| 13 ms | LOW | Button pressed |
| 18 ms | HIGH | Button released |
| 24 ms | LOW | Button finally stable |
Code that reacts to every change may treat this as three presses. Code that increments a counter on every LOW reading may count many presses while the button is held.
This sketch prints every detected transition. Use it to observe how your particular button behaves in the Serial Monitor.
const byte buttonPin = 2;
int previousReading = HIGH;
unsigned long transitionCount = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != previousReading) {
transitionCount++;
Serial.print("time = ");
Serial.print(millis());
Serial.print(" ms, reading = ");
Serial.print(reading == LOW ? "LOW" : "HIGH");
Serial.print(", transitions = ");
Serial.println(transitionCount);
previousReading = reading;
}
}The sketch does not remove bounce. It only exposes the raw signal. The first transition may happen when the contacts initially meet, and later transitions may be caused by the contacts bouncing.
Why the problem affects output code
Consider code that toggles an LED whenever the button reads LOW. The LED can toggle repeatedly during one press. Even code that detects a transition can fail if it accepts every rapid transition as a real event.
The usual solution is debouncing. Debouncing means accepting a new button state only after the reading has remained unchanged for a short period. A common interval is 20 to 50 milliseconds. The program records when the raw reading changed, waits for the interval, and then accepts the state as stable.
A delay can sometimes hide bounce, but it stops the entire loop() during the delay. A timer based on millis() can track stability while the rest of the sketch continues running. The next step is to use that non-blocking timer to accept only stable button states.
Exercise
With INPUT_PULLUP, a button press changes the pin from HIGH to LOW. If the raw readings are HIGH, LOW, HIGH, LOW, LOW for one press, how many LOW-to-HIGH or HIGH-to-LOW transitions occurred after the initial HIGH?
Show answer
Three transitions occurred: HIGH to LOW, LOW to HIGH, and HIGH to LOW. The final LOW is a stable reading, not another transition.