ENع
→Debouncing
الدرس 11 · Digital pins: inputs and outputs

Debouncing a button

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

10 دقائق9 خطوات5 أسئلةBeginner
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 11 · تفاعلي
01 / 09

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  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)
  2. 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 0
  3. The 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 slower
  4. The 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 1
  5. One 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 press
  6. Debounce: 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++;
      }
    }
  7. 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)
  8. 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     1
  9. Why 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

اختبار يتاح بعد الخطوة الأخيرة.