Sub topic 9
Why one press can look like many
This chapter explains why a single button press can register as several presses on an Arduino. It assumes you can wire a button to a pin with INPUT_PULLUP and read it with digitalRead. It also assumes you know that a pressed button reads LOW in that wiring. The fix, software debouncing, is the next chapter's topic. This one covers the cause, how to measure it, and how to recognize it.
The symptom
You wire a button to pin 2 with INPUT_PULLUP, write a sketch that counts presses, and press the button once. The counter jumps by 1, 2, 3 or sometimes more. The count is different each time, and some presses count correctly. The sketch compiles, the wiring is right, and the code reads sensibly. The problem is not a bug in your logic. It comes from how the button works physically.
A sketch that only lights an LED while the button is held never shows this problem. Flicker lasting a millisecond or two is invisible, and the final level is what you see. The problem appears only when your code reacts to changes, such as counting, toggling, or stepping through a menu.
What happens inside the switch
A push button is two pieces of metal that touch when you press. They are springy, and they move fast. When the contacts meet, they do not stick at once. They hit, rebound slightly, touch again, and rebound again, with the motion dying out over a short time. Each touch connects the pin to GND (in the pull-up wiring), and each rebound breaks the connection and lets the pull-up resistor drag the pin back to HIGH.
This effect is called contact bounce. The pin sees a rapid series of HIGH and LOW transitions instead of one clean edge. Releasing the button does the same thing in reverse, because the contacts separate unevenly.
Why your code sees every bounce
An Arduino Uno runs at 16 MHz, and one digitalRead takes only a few microseconds. A simple loop() can sample the pin hundreds of times per millisecond. Bounce typically lasts from a fraction of a millisecond to a few milliseconds, so your sketch samples the pin many times while the contacts are still bouncing. Each flip between HIGH and LOW looks like a real change, because the pin really did change level. The microcontroller reads the pin correctly. The signal itself is messy.
Here is a rough timeline for one press of a typical small tactile button:
| Phase | Typical duration | What the pin does |
|---|---|---|
| Before the press | Any length | Steady HIGH |
| Bounce on press | Often under 1 to 5 ms | Flips between HIGH and LOW |
| Held down | Tens to hundreds of ms | Steady LOW |
| Bounce on release | Often under 1 to 5 ms | Flips between LOW and HIGH |
| After release | Any length | Steady HIGH |
These durations vary a lot. Cheap, worn, or large switches can bounce for 10 to 20 ms or more. Treat the numbers as ranges, not constants, and measure your own button.
See the bounce: count the edges
The sketch below counts every HIGH-to-LOW transition (a falling edge) and prints the running total. Open the Serial Monitor at 115200 baud and press the button slowly, once at a time.
const int buttonPin = 2;
int lastLevel = HIGH;
unsigned long fallingEdges = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int level = digitalRead(buttonPin);
if (level != lastLevel) {
if (level == LOW) {
fallingEdges++;
Serial.print("falling edges so far: ");
Serial.println(fallingEdges);
}
lastLevel = level;
}
}Each press prints at least one line. With many buttons you will often see two or three lines for a single press, and sometimes only one. Note that the sketch already does the right thing for edge detection: it compares the new level to the previous one. The counting is correct. The extra counts come from real extra edges on the wire.
The Serial printing itself slows the loop a little, because each line takes time to send. That can hide some of the fastest bounces. So the count you see is a lower bound on what a faster loop would catch.
Measure the bounce: how long does it last?
A better experiment records timestamps. This sketch notes the time of the first edge in a burst and the time of the last one. When the pin has been quiet for 20 ms, it prints the number of edges and the time between the first and the last edge. It prints only after the burst is over, so printing never distorts the measurement.
const int buttonPin = 2;
const unsigned long quietUs = 20000UL; // 20 ms with no edges ends a burst
int lastLevel = HIGH;
unsigned long firstEdgeUs = 0;
unsigned long lastEdgeUs = 0;
unsigned int edges = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int level = digitalRead(buttonPin);
unsigned long now = micros();
if (level != lastLevel) {
if (edges == 0) {
firstEdgeUs = now;
}
lastEdgeUs = now;
edges++;
lastLevel = level;
}
if (edges > 0 && (now - lastEdgeUs) > quietUs) {
Serial.print("edges: ");
Serial.print(edges);
Serial.print(" burst length (us): ");
Serial.println(lastEdgeUs - firstEdgeUs);
edges = 0;
}
}Press and release the button several times and read the output. A clean press gives edges: 1 and a burst length of 0, because the first and last edge are the same edge. A bouncing press gives an odd number of edges for a press (the pin ends LOW) and an even number for a release (the pin ends HIGH). The press and release lines are separate bursts if you hold the button longer than 20 ms.
What the numbers tell you
Run the measurement sketch on your own button and look at three things.
- Edge count per press. Anything above 1 confirms bounce. Counts of 2 to 10 are common on cheap buttons.
- Burst length. The longest burst you see over 20 or 30 presses is the useful number. Your later debounce time must be longer than that, with some margin.
- Release bursts. These often differ from press bursts. A button that looks clean when pressed may bounce on release, and a code path that reacts to release will need the same protection.
Results also change with how you press. A fast, hard press tends to bounce more than a slow, gentle one. A button that has been used a lot often bounces longer than a new one. Test with the kind of press your users will actually make.
Bounce is not the same as other noise
Contact bounce is easy to confuse with two other problems.
- A floating input. If the pin is not tied to any defined level, it picks up stray electrical noise and flips at random, even when nobody touches the button. Bounce happens only around a press or release. If you see activity with no hand on the button, check your pull-up or pull-down first.
- Electrical interference. Long wires, motors, relays, and switching supplies can induce spikes on the pin. These spikes can look like extra presses, and they occur at times unrelated to your finger. A debounce delay helps with short spikes, but the real fix is shorter wires, a clean supply, or an added capacitor.
A quick test: if the extra edges appear only within a few milliseconds of a press or release, it is bounce. If they appear at other times, look for a wiring or noise problem.
Why simple fixes fail
Two quick fixes tempt many beginners, and both have problems.
- Waiting with
delay()after a press. Adelay(50)right after detecting a press does hide the bounce, but it freezes the whole sketch. Nothing else can run during that time: other buttons, sensors, serial input, or animations all stall. This is acceptable only in a sketch that does nothing else. - Assuming the pull-up cures it. The pull-up or pull-down resistor makes the idle level defined. It does nothing for the mechanical flutter. Bounce occurs with every wiring style, including an external pull-down.
The usual approach is to accept a new button state only after the reading has stayed unchanged for a short time, using millis() so the sketch never blocks. That technique comes next.
Summary of the model
- A button's metal contacts rebound when they meet and when they part, so the pin flips several times for one action.
- The Arduino samples much faster than the bounce lasts, so it reads each flip as a real change.
- Code that reacts to levels hides the problem. Code that reacts to changes, such as counting or toggling, exposes it.
- Bounce lasts from well under a millisecond to tens of milliseconds, and you should measure your own button.
- Bounce happens near presses and releases. Activity at other times points to a floating input or noise.
Exercise
A sketch toggles an LED on each falling edge of a button wired with INPUT_PULLUP. Sometimes a press toggles the LED twice, so it seems not to change at all. Explain why this happens. Then say what measurement you would take to choose a safe debounce time.
Show answer
The contacts bounce when they close, so the pin flips LOW, HIGH, LOW within a few milliseconds. The loop runs much faster than that, so it sees two falling edges and toggles the LED twice. An even number of toggles leaves the LED in its old state. To pick a debounce time, run the burst-measuring sketch, press the button 20 to 30 times (including fast, hard presses), and note the longest burst length on both press and release. Choose a debounce time somewhat longer than that longest burst, for example 1.5 to 2 times as long.
Exercise
You run the burst-measuring sketch and see edges: 3 after one press. Is the button now held down or released, and how do you know? What would you suspect if you also saw bursts appear when nobody touches the button?
Show answer
The button is held down. Wired with INPUT_PULLUP, the pin starts HIGH, so an odd number of edges leaves it LOW, which means pressed. Bursts with nobody touching the button are not contact bounce. Suspect a floating input (a missing or loose pull-up or pull-down connection) or electrical noise picked up by long wires or nearby motors and relays.