Bounce, and how to ignore it
Touch a wire to GND once and the pin goes LOW, HIGH, LOW several times in the first millisecond, because metal meeting metal bounces. A loop fast enough to see it counts one touch as several. A 50 ms lockout with millis() counts it once.
One touch is several edges
A jumper wire touched to a GND hole does not make one clean contact. The two metal surfaces meet, spring apart a fraction of a millimetre, meet again, and settle, usually within a millisecond or two. That is contact bounce. Every switch, button and relay does it.
To a slow reader it does not matter. To loop() it does. A loop that does
nothing but digitalRead goes round many thousands of times a second, often
enough to see each make and break as its own falling edge.
Watching it happen
With LOCKOUT_MS set to 0 the sketch counts every HIGH-to-LOW change on D2, and
a light tap prints two, three, sometimes more counts. The pin-13 lamp toggles
once per count,
so an even number of bounces leaves it where it was. Nothing is wrong with the
sketch: it is reporting what the pin did.
A lockout with millis()
The fix is to ignore the pin for a while after it changes. The sketch keeps the
time of the last edge it saw, and counts a falling edge only if the pin had been
quiet for at least LOCKOUT_MS before it:
unsigned long t = millis();
bool quiet = t - lastEdge >= LOCKOUT_MS;
lastEdge = t; // every edge restarts the lockoutEvery edge restarts the window, rising ones too, so the bounce when the wire is
lifted off is ignored as well. 50 ms is far longer than any bounce and far
shorter than anyone can tap twice. The subtraction t - lastEdge stays correct
when millis() wraps after about 49 days, which is why it is written that way
round, as millis() instead of delay() explains.
The sketch never calls delay(), so the loop keeps reading the pin the whole
time. A delay(50) after each count would also hide the bounce, and stop
everything else the sketch does for 50 ms.
Or use a library
Debouncing is common enough to have libraries. Bounce2 is one of the
best-known for Arduino; it wraps the same idea in an object with update() and
fell(). It is in the Library Manager under
that name. Writing the ten lines yourself once is worth it, so the library is
not a mystery when you use it.
The code
Counts touches of a jumper wire from D2 to GND, prints the count and toggles the pin-13 lamp on each one. Upload it, put one end of the wire in the 2 hole, and tap the other end into a GND hole.
// Count_Touches.ino — counts touches of a wire from D2 to GND
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Wiring: one jumper wire from the 2 hole, touched to a GND hole.
// D2 is INPUT_PULLUP, so the wire is safe.
//
// Set LOCKOUT_MS to 0 to count every bounce instead.
const byte PIN = 2;
const byte LAMP = 13;
const unsigned long LOCKOUT_MS = 50;
int last = HIGH;
unsigned long lastEdge = 0;
unsigned int count = 0;
void setup() {
pinMode(PIN, INPUT_PULLUP);
pinMode(LAMP, OUTPUT);
Serial.begin(9600);
Serial.println("touch the wire to GND");
}
void loop() {
int now = digitalRead(PIN);
if (now == last) return; // no edge this time round
last = now;
unsigned long t = millis();
bool quiet = t - lastEdge >= LOCKOUT_MS;
lastEdge = t; // every edge restarts the lockout
if (now == LOW && quiet) { // a falling edge after a quiet spell
count++;
digitalWrite(LAMP, count % 2); // lamp 13 flips every count
Serial.println(count);
}
}Set LOCKOUT_MS to 0 and upload again to see the bounce: one tap then counts two, three or more. D2 is INPUT_PULLUP, so the wire is safe on it.
When it does not work
Bounce varies with the wire, the hole and how hard you press, and a clean firm touch sometimes makes one edge. Tap lightly, or scrape the wire end across the GND hole, and the extra counts appear.
Any edge within LOCKOUT_MS of the last one is ignored, so two real touches closer than 50 ms count once. Nobody taps that fast by hand; for a faster signal, shorten the lockout to just longer than the bounce.
Lifting the wire bounces too, and its bounce includes falling edges. The sketch restarts the lockout on every edge, rising ones included, so release bounce is ignored. If you changed it to restart only on a counted edge, that is the cause.
Serial.println only fills a 64-byte buffer and returns, so a short line costs microseconds, not milliseconds. A sketch that prints on every pass of loop() can fill the buffer and then waits a millisecond a character at 9600 baud. Print only when something changes.
Let the chip watch the pin, and count edges even inside a delay().
Interrupts on D2 and D3 →Edit this page — content/books/atmega328p/debouncing.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.