Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Digital I/O

Pull-Up Resistors and Debouncing

Understand pull-up and pull-down resistors, choose resistor values, and eliminate contact bounce with a time-based debounce routine that works without delay().

Two ideas turn a flaky button into a reliable one: pull resistors, which give an input a defined level, and debouncing, which ignores the rapid chatter that real switch contacts produce. You met both briefly in digitalRead and Buttons. Here we look at the numbers and write a proper debounce routine.

Pull-up and pull-down

A pull resistor is a large resistor (typically 10 kฮฉ) that connects an input pin to a power rail. With nothing else connected, almost no current flows, so the pin sits at the rail voltage. When a switch connects the pin directly to the opposite rail, the low-resistance switch wins over the pull resistor.

Pull-up Pull-down
Resistor goes to +5 V GND
Switch connects pin to GND +5 V
Released reads HIGH LOW
Pressed reads LOW HIGH

The Arduino's internal pull-up is 20 to 50 kฮฉ; enable it with pinMode(pin, INPUT_PULLUP). There is no internal pull-down on the classic AVR boards, so use an external 10 kฮฉ resistor if you need one.

Why 10 kฮฉ?

The resistor must be small enough to win against leakage and noise, but large enough not to waste current when the switch closes. With a switch closed, the current through the pull-up is V / R: 5 V / 10 kฮฉ = 0.5 mA, which is negligible. With 1 kฮฉ it would be 5 mA, a waste of battery. With 1 Mฮฉ the pin would be too weak against noise. Run the numbers:

C Stand-in: current and logic level for pull resistors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Contact bounce

A mechanical switch is two metal contacts that spring together. On closing, they bounce for a few milliseconds, making and breaking the connection many times before they settle. A microcontroller reading a pin every few microseconds sees every bounce as a separate press.

TEXT
ideal:     ______|โ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พโ€พ
real:      ______|โ€พ|_|โ€พโ€พ|_|โ€พโ€พโ€พโ€พโ€พโ€พ
                  ^ bounces, 1-20 ms

If your sketch counts presses by edge detection, one push can count as three or five. Debouncing cures it. There are hardware fixes (a 100 nF capacitor across the button with the pull-up forms an RC filter, or a Schmitt trigger chip) and software fixes, which cost nothing.

Software debounce with a time threshold

The usual method: accept a new stable state only after the reading has stayed the same for a minimum time (typically 20 to 50 ms). Do it without delay() so the rest of the sketch keeps running:

C++
const int buttonPin = 2;
const unsigned long debounceMs = 30;

int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChange = 0;
int presses = 0;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(buttonPin);
  if (reading != lastReading) {
    lastChange = millis();          // the signal moved: restart the timer
  }
  if (millis() - lastChange > debounceMs && reading != stableState) {
    stableState = reading;
    if (stableState == LOW) {
      presses++;
      Serial.println(presses);
    }
  }
  lastReading = reading;
}

The stand-in below runs the same algorithm over a noisy recording. The time is in milliseconds and samples come every 2 ms.

C Stand-in: debounce a bouncing button

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The naive method counts every bounce (including the release bounce) while the debounced version reports the one press you meant. Try shrinking debounceMs to 4 and watch the count rise.

A reusable debouncer

Wrap the state in a small struct so you can debounce several buttons:

C Stand-in: a reusable debouncer for several buttons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Libraries and other tricks

The Bounce2 library from the Library Manager wraps all this: bounce.update(); if (bounce.fell()) { ... }. For interrupts, debounce inside the handler by ignoring triggers closer together than your threshold. Capacitor-based hardware debouncing is a good idea in noisy environments or when your loop is slow.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a pull-up resistor do?

    It connects the pin to +5 V through a large resistor, defining the idle level.
  2. 2. Why is 10 kohm a common pull resistor value?

    10 kohm at 5 V is 0.5 mA, a small current, yet strong enough to hold the pin.
  3. 3. What causes contact bounce?

    The physical contacts rebound before settling.
  4. 4. In a time-based debounce, when do you accept a new state?

    You wait until the signal stops changing for about 20 to 50 ms.

Summary

  • Pull-up and pull-down resistors give inputs a defined idle level; INPUT_PULLUP is built in.
  • Typical external pull resistors are 10 kฮฉ.
  • Mechanical switches bounce for milliseconds and cause false multiple presses.
  • Debounce in software by requiring a stable reading for 20 to 50 ms, using millis(), not delay().
  • Wrap the logic in a function or library to reuse it.

Next: Using millis() Instead of delay()