Sub topic 3
What contact bounce looks like
This chapter assumes you can write a basic sketch with setup() and loop(), and that you can wire a pushbutton and an LED. It also assumes you know pinMode(), digitalRead() and digitalWrite(). You should be comfortable with if statements, booleans and integer types in C++. You should also know the idea behind INPUT_PULLUP: it keeps the pin at HIGH until the button pulls it to GND. The next chapters use these facts, but this chapter focuses on what the pin does while the button moves.
What a button really does
A pushbutton is two pieces of metal that touch when you press it. In a drawing, the signal goes from HIGH to LOW in one clean step. In real hardware, the metal pieces are springy. When they meet, they hit, rebound, and hit again, several times, before they settle.
Each tiny contact and break makes the voltage on the pin jump between the two logic levels. The pin does not see one press. It sees a short burst of presses and releases. This effect is called contact bounce, or switch bounce.
The same thing happens when you release the button. The metal pieces separate, and they can touch again briefly as the spring pushes them apart.
What the pin sees
Suppose a button connects pin 2 to GND, with INPUT_PULLUP enabled. The idle level is HIGH. A single physical press might produce this sequence on the pin:
| Time after first contact | Pin level | What is happening |
|---|---|---|
| 0 µs | LOW | Contacts touch for the first time |
| 40 µs | HIGH | Contacts spring apart |
| 90 µs | LOW | Contacts touch again |
| 300 µs | HIGH | Another rebound |
| 450 µs | LOW | Contacts touch again |
| 2 ms onward | LOW | Contacts settle and stay closed |
These numbers are only an example. Every switch is different, and the same switch behaves differently from one press to the next.
How long does bounce last?
For common small tactile switches, the bouncing usually lasts from a few hundred microseconds to a few milliseconds. Larger or worn switches can bounce for 10 ms or more. Datasheets often list a maximum bounce time, so check yours if you have one.
The length varies, which is why debounce code uses a margin. A 20 to 50 ms wait is a common choice. It is far longer than the bounce but still shorter than any human can notice.
Bounce is faster than your eyes, but not faster than the board. An Arduino Uno runs at 16 MHz, and one pass through a short loop() can take only a few microseconds. That is quick enough to read every one of the transitions in the table above.
Why this breaks sketches
Consider the most obvious approach: count presses by watching the pin.
const uint8_t BUTTON_PIN = 2;
int lastReading = HIGH;
int pressCount = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastReading = reading;
if (reading == LOW) { // button pressed
pressCount++;
Serial.println(pressCount);
}
}
}This code does detect edges correctly. It reports a count only when the pin changes from HIGH to LOW. The problem is that bounce creates several HIGH to LOW changes for a single press.
Open the Serial Monitor and press the button once. On many switches you will see 1, and on others 1, 2, 3. The result differs from press to press. A loop that toggles an LED will look random: sometimes the LED changes state as you expect, and sometimes it appears to ignore you, because an even number of bounces toggled it twice.
Bounce on release too
Release bounce is easy to forget. If your code reacts to both edges, or if it reacts to the HIGH level in some way, the bounce on release can cause the same kind of error as the bounce on press.
Most simple sketches react only to the press edge, so release bounce does no harm there. A debounce routine must still handle it. Otherwise a bounce on release can look like a new press when the pin briefly goes LOW again.
A bounce is not noise from the wiring
Bounce is a mechanical effect, so it is different from electrical noise. Noise comes from long wires, nearby motors or floating pins. Bounce comes from the switch itself, and it happens even on a short, clean wire.
The two can look alike in the Serial Monitor, so it helps to rule out the other causes first:
- A floating input has no pull-up or pull-down. It gives random readings even when nobody touches the button. Enabling
INPUT_PULLUPfixes this. - Long wires pick up interference. Keep the button wires short while you test.
- A loose breadboard contact can behave like a very bad switch. Press the pieces into the board firmly.
If the pin reads a steady HIGH while the button is untouched, and you only see extra transitions around the moment of a press, you are looking at bounce.
Seeing the bounce yourself
You can count the raw transitions around one press. This sketch uses an interrupt to count every falling edge. It prints the count after the pin has been quiet for a moment. It assumes a board where pin 2 supports external interrupts, such as an Uno.
const uint8_t BUTTON_PIN = 2;
const unsigned long QUIET_MS = 200;
volatile unsigned int edgeCount = 0;
volatile unsigned long lastEdgeTime = 0;
void onEdge() {
edgeCount++;
lastEdgeTime = millis();
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onEdge, FALLING);
}
void loop() {
noInterrupts();
unsigned int count = edgeCount;
unsigned long last = lastEdgeTime;
interrupts();
if (count > 0 && millis() - last >= QUIET_MS) {
Serial.print("Falling edges for one press: ");
Serial.println(count);
noInterrupts();
edgeCount = 0;
interrupts();
}
}Press the button once, slowly and firmly, then try a quick tap. You will often see counts greater than 1, and the number changes from press to press. The interrupt catches every edge, so this sketch shows what a plain digitalRead() loop can miss or catch depending on timing.
The interrupt code copies the shared variables inside noInterrupts() and interrupts(). A volatile variable that is larger than one byte can change halfway through a read on an 8-bit board, so a protected copy keeps the value consistent. A later chapter covers interrupts and their pitfalls in more detail.
What a fix has to do
Every debounce method, in software or hardware, follows one idea: do not trust a change until it has lasted long enough. The methods differ in where they apply that rule.
| Approach | How it ignores bounce | Cost |
|---|---|---|
| Software timestamp | Accepts a new level only after it has been stable for a set time | A few variables and no extra parts |
| RC filter | Smooths the voltage so that quick spikes never cross the threshold | A resistor and a capacitor per button |
| Schmitt trigger | Gives a clean digital edge from a slowly changing voltage | An extra chip |
| Interrupt with timestamp | Ignores new edges that arrive too soon after the last one | Careful handling of shared variables |
The next chapters build the software version. It needs a way to measure elapsed time without stopping the sketch, which is what millis() provides. Before that, the chapter on delay() explains why the simplest wait is a poor choice.
Common mistakes when you first meet bounce
- Assuming the wiring is wrong. Intermittent double counts usually point to bounce, not a bad connection.
- Using a very short wait. A wait of 1 ms can be shorter than the bounce of a worn switch.
- Using a very long wait. A wait of 500 ms makes fast presses feel ignored.
- Debouncing only the press. The release can bounce as well.
- Testing once. Bounce varies, so press the button many times and look at the pattern.
Exercise
A sketch counts falling edges on a pin wired with INPUT_PULLUP, and one press prints a count of 3. The button wires are short and the pin reads a steady HIGH when you do not touch the button. What is the most likely cause, and why does the count change from press to press?
Show answer
The cause is contact bounce. The metal contacts rebound several times before they settle, so the pin goes HIGH to LOW more than once for a single press. The steady HIGH at rest rules out a floating input and wiring noise. The count changes because the number and timing of the rebounds depend on the force of the press, the switch's wear and other physical factors, so each press bounces differently.
Exercise
Why is a debounce wait of about 20 to 50 ms a reasonable choice for a tactile switch that bounces for roughly 2 ms?
Show answer
The wait must be longer than the worst-case bounce so the signal has settled, and bounce time varies between presses and between switches, so a margin is needed. It must also be short enough that a person cannot notice the delay before the sketch reacts. 20 to 50 ms meets both conditions.