Debouncing a button
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A push button looks like a clean on/off switch, but its metal contacts bounce: they touch, spring apart and touch again for a few milliseconds. A microcontroller is fast enough to see every bounce, so one press can count as 4. Wire a button to pin 2 of an Arduino Uno R3 with a 10 kΩ pull-up (the lesson “Pull-up and pull-down resistors” explains why), watch one press bounce in slow motion, and fix it in firmware by waiting until the pin has been quiet for 50 ms.
A button and a pull-up
The button sits between pin 2 and ground. A 10 kΩ pull-up resistor ties the pin to 5 V, as in the lesson on pull-up resistors. Released, nothing joins the pin to ground, so digitalRead gives HIGH: 1.
board Arduino Uno R3 (ATmega328P) pin 2 pinMode(BTN, INPUT), 10 kOhm pull-up to 5 V button from pin 2 to GND released pin pulled up to 5 V -> HIGH (1)
اقرأ الدرس خطوة بخطوة
A button and a pull-up
The button sits between pin 2 and ground. A 10 kΩ pull-up resistor ties the pin to 5 V, as in the lesson on pull-up resistors. Released, nothing joins the pin to ground, so digitalRead gives HIGH: 1.
board Arduino Uno R3 (ATmega328P) pin 2 pinMode(BTN, INPUT), 10 kOhm pull-up to 5 V button from pin 2 to GND released pin pulled up to 5 V -> HIGH (1)Pressing pulls the pin LOW
Press the button and its contacts join the pin straight to ground. Now the pin reads LOW: 0. Pressed means LOW here. That is called negative logic. So we expect one clean change, 1 to 0, for one press.
pressed contacts touch: pin joined to GND -> LOW (0) negative logic: pressed = LOW what we hope to see: 1 1 1 1 0 0 0 0The contacts bounce
Metal contacts are springy. They hit, jump apart and hit again before they settle. In our example the pin falls to LOW 4 times in 3.1 ms. Ganssle measured 1.557 ms of bounce on average.
bounce the contacts touch, part, touch again example edges at 0, 0.4, 0.9, 1.5, 2, 2.8, 3.1 ms the pin falls to LOW 4 times in 3.1 ms Ganssle 18 switches x 300 presses: average 1557 µs 16 of them never over 6200 µs the figure plays this 1,000 times slowerThe chip sees every bounce
At 16 MHz one millisecond is 16,000 clock ticks. loop() runs many times inside each bounce, so digitalRead catches every short LOW and every short HIGH.
clock 16 MHz -> 16,000 ticks per ms line 2 int r = digitalRead(BTN); again and again reads 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 ... every gap in the contact shows up as a 1One press, counted 4 times
This first loop() adds 1 each time the pin changes from HIGH to LOW. Our one press has 4 such changes, so count jumps to 4. A menu would skip 3 items, or a light would flick on and off.
line 3 if (r != last && r == LOW) line 4 count++; falls at 0, 0.9, 2, 3.1 ms count 4 for ONE pressDebounce: wait for quiet
Debouncing means ignoring the bounce. The new loop() takes a reading only once it has stayed the same for more than 50 ms. 50 ms is the delay in Arduino's own Debounce example.
debounce accept a new reading only after it stops changing rule unchanged for more than 50 ms -> it is real (Arduino Debounce example: debounceDelay = 50) // Arduino Uno R3: button from pin 2 to GND, 10 kOhm pull-up to 5 V const int BTN = 2; int last = HIGH, state = HIGH; // released reads HIGH unsigned long t0 = 0; // millis() is unsigned long unsigned long count = 0; void setup() { pinMode(BTN, INPUT); // the external resistor pulls up } void loop() { int r = digitalRead(BTN); if (r != last) t0 = millis(); last = r; if (millis() - t0 > 50 && r != state) { state = r; if (r == LOW) count++; } }Each bounce restarts the timer
Line 3 notes the time, millis(), whenever the reading changes. The quiet time is millis() minus that note. Every bounce resets it to 0, so during the bounce it never gets near 50.
line 3 if (r != last) t0 = millis(); quiet millis() - t0 changes at 0, 0.4, 0.9, 1.5, 2, 2.8, 3.1 ms -> quiet back to 0 last one at 3.1 ms (millis() was 3 ms after the press)Quiet for 50 ms: count it
After the last bounce the pin stays LOW. At 54 ms the quiet time passes 50, so line 7 takes the new state and line 8 adds 1. One press, count 1.
t0 3 ms (the last change, rounded down by millis()) 54 ms 54 - 3 = 51 > 50 line 7 state = r; state becomes LOW line 8 count++; count 1Why 50 ms?
Long enough: Ganssle found most switches bounce well under 10 ms. Short enough: in his test a 100 ms delay felt slow, but 50 ms seemed instant. Releasing the button bounces too, and the same rule cleans it.
bounce most well under 10 ms (Ganssle); Valvano: usually under 5 ms feel 100 ms noticeable, 50 ms seemed instant (Ganssle) so 50 ms: past the bounce, before you notice release bounces too: state goes HIGH, count stays
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is switch bounce?
The contacts are springy metal. When they meet they jump apart and meet again a few times, usually for a few milliseconds, before they stay together.
02The button connects pin 2 to ground and a 10 kΩ resistor pulls the pin up to 5 V. What does digitalRead give while the button is pressed?
Pressed, the contacts join the pin straight to ground, so it reads LOW. Released, the pull-up holds it HIGH. Pressed = LOW is called negative logic.
03In the example the pin falls from HIGH to LOW 4 times during one press. What does a loop() that adds 1 on every HIGH-to-LOW change count?
loop() runs far faster than the bounce (one millisecond is 16,000 clock ticks at 16 MHz), so it sees all 4 falls and counts 4.
04The last bounce happens 3 ms after the first touch (as millis() sees it). The debounced code accepts the press when millis() - t0 > 50. When is it counted?
t0 = 3. The first whole millisecond where millis() - 3 is more than 50 is 54, so the press is counted once, at 54 ms.
05Why is 50 ms a sensible debounce time?
Ganssle measured most switches bouncing well under 10 ms, and found a 100 ms delay noticeable while 50 ms seemed instant. Arduino's own Debounce example uses 50 ms too.
المسرد
- Switch bounce
- The contacts of a switch touching and parting several times, over a few milliseconds, before they settle.
- Debouncing
- Ignoring the bounce, in software or hardware, so that one press is seen as one change.
- Pull-up resistor
- A resistor from a pin to the supply voltage, so the pin reads HIGH when nothing else drives it.
- Negative logic
- A signal that is LOW when active: with a pull-up, a pressed button reads LOW.
- millis()
- An Arduino function that returns the number of milliseconds since the program started, as an unsigned long.
- Edge
- The moment a digital signal changes: a falling edge goes HIGH to LOW, a rising edge LOW to HIGH.
- Schmitt trigger
- An input with hysteresis: it switches at a higher voltage going up than going down, so a slow or noisy edge gives one clean change.
الملاحظات والمصادر
- The bounce in the figure is one example, drawn to scale as clean steps: 4 touches in 3.1 ms. Real contacts also give fuzzy voltages in between, and every press is different. Ganssle's 18 switches averaged 1557 µs, but two of them bounced for over 6.2 ms.
- millis() counts whole milliseconds since the program started and returns an unsigned long, which wraps to 0 after about 50 days. Writing millis() - t0 with unsigned long variables still gives the right answer across that wrap.
- The card shows only loop(). The whole sketch also declares last, state, t0 and count, and sets pinMode(BTN, INPUT) in setup(), because the 10 kΩ resistor is outside the chip. With no resistor you would use INPUT_PULLUP, which turns on the chip's own pull-up.
- Bounce can also be filtered in hardware: a resistor and capacitor slow the edge down, and a Schmitt trigger input (one with hysteresis) turns the slow edge back into one clean change. Ganssle warns against feeding a slow RC edge into an input without hysteresis.
- The model reads the pin every 0.01 ms. The shortest gap in the example lasts 0.3 ms, which is 4,800 clock ticks at 16 MHz, far longer than one pass of this short loop(), so the board catches the same changes.
- J. G. Ganssle, A Guide to Debouncing (rev. 3, 2008), copy hosted for University of Utah CS 5780
- J. W. Valvano, Embedded Systems: Shape the World, ch. 8: switches and LEDs (UT Austin)
- Arduino built-in example: Debounce.ino
- Arduino language reference: millis() (source)
- Arduino language reference: digitalRead() (source)
- Arduino language reference: pinMode() (source)
- Arduino UNO R3 product datasheet (A000066)
اختبار يتاح بعد الخطوة الأخيرة.