Sub topic 3
What Contact Bounce Looks Like
This chapter assumes you know the basic Arduino sketch structure and can use pinMode(), digitalRead(), Serial.println(), and if statements. You should also know how to connect a pushbutton to a digital input.
A button does not make one clean transition
A mechanical pushbutton contains metal contacts that touch when you press it. You might expect the electrical signal to change once:
HIGH ────────────────┐
└──────── LOWReal contacts usually do not behave this cleanly. The metal surfaces flex, collide, and separate several times before settling. The input can change rapidly between HIGH and LOW during those few milliseconds.
The Arduino does not know that these transitions came from one physical press. Each transition is an ordinary electrical input state. If your program reacts immediately, it can interpret one press as several presses.
What the sketch sees
Suppose a button uses the internal pull-up resistor. In this wiring, the input reads HIGH while the button is released and LOW while it is pressed.
| Physical action | Possible readings during the action |
|---|---|
| Release button | `HIGH` |
| Press button | `LOW, HIGH, LOW, HIGH, LOW` |
| Hold button | `LOW` |
The exact pattern differs between buttons. Some bounce for less than a millisecond. Others bounce for several milliseconds. A worn or dirty button may bounce for longer.
The speed of your program also affects what you observe. A fast loop may read several bounce states. A slower loop may read only the first and last state. Missing some transitions does not mean that bounce did not happen.
Observing raw input changes
This sketch prints every change that the Arduino notices. Connect the button between pin 2 and GND. The internal pull-up makes the released state HIGH.
const uint8_t BUTTON_PIN = 2;
int previousReading = HIGH;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != previousReading) {
Serial.print("Input changed to ");
Serial.println(reading == HIGH ? "HIGH" : "LOW");
previousReading = reading;
}
}Open the Serial Monitor and press the button repeatedly. You may see several messages for one press. You may also see only one message because the serial output and the loop timing hide some transitions.
For a better view, use an oscilloscope or a logic analyzer. These tools measure the voltage directly and can show transitions that the sketch never samples.
Why repeated actions cause bugs
Consider a program that increments a counter whenever digitalRead(BUTTON_PIN) is LOW. While you hold the button, the loop runs thousands of times, so the counter increases continuously. Bounce creates a different problem: even code that reacts only when the reading changes can still react several times before the signal settles.
A useful model separates two ideas:
- A raw state is the most recent electrical reading.
- A stable state is the state that has remained unchanged long enough to trust.
Debouncing means delaying acceptance of a raw change until it has been stable for a chosen period. The program might require the input to remain unchanged for 20 to 50 milliseconds. The exact value is a trade-off. A shorter period responds faster but may accept bounce. A longer period rejects more bounce but adds response delay.
Exercise
Exercise
With INPUT_PULLUP wiring, what sequence might the Arduino see when one button press bounces, and which reading represents the settled pressed state?
Show answer
It might see LOW, HIGH, LOW, HIGH, LOW. The final LOW is the settled pressed state.