Limit switch or button counts twice: debounce with a time check

Ignore further changes for about 20 to 50 ms after the first one.

What the message means

Mechanical contacts bounce, so one press makes several fast transitions. Code that counts edges counts them all.

Fix it in this order

  1. Store the time of the last accepted change.
  2. Accept a new change only if enough time has passed.
  3. Use millis(), not delay(), so the robot keeps running.
  4. Add a small capacitor across the switch if the bounce is bad.
  5. Use an interrupt only with the same time check inside it.
  6. Test by pressing the switch fast.

Why it happens

A mechanical switch does not change state cleanly. For a few milliseconds the contacts bounce, so the input flickers between high and low and one press is counted several times.

Ignore changes for a short time after the first edge, commonly 20 to 50 ms, using millis() rather than delay().

unsigned long lastChange = 0;
bool lastState = HIGH;
if (digitalRead(PIN) != lastState && millis() - lastChange > 30) {
  lastState = !lastState;
  lastChange = millis();
  // handle the press here
}

Example sketch: change pins and values for your own wiring. It has not been bench-tested on your hardware.

If you see this, do this

You seeDo this
One press counts twiceNo debounce.
Presses are missedTime window too long.

encoder bounce, millis timing.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

The metal contacts of a switch do not close in one clean movement. They hit, spring apart and hit again for a few milliseconds. A microcontroller reads the pin in microseconds, so it sees a burst of transitions where you pressed once. Code that counts each change counts them all.

Another cause is counting both edges. If the interrupt is set to CHANGE, one clean press and release is two events. Touch sensors can chatter too, when the signal sits near the switching threshold, and need a small hysteresis or the same time rule.

How to confirm the cause

  • Print a timestamp for every pin change using micros(). A press that shows several changes within a few milliseconds is bounce.
  • Count the edges for one slow, deliberate press. Two edges for a press and a release is normal; more means bounce or noise.
  • Check the pin mode. A floating input picks up noise; use INPUT_PULLUP and wire the switch to ground.
  • Touch the cable while the switch is untouched. If the count changes, the wire is picking up interference.
  • If you use an interrupt, check which edge it triggers on.

Worked example

A debounce that accepts a new state only after it has stayed the same for 30 ms. The switch connects pin 2 to ground.

const uint8_t PIN = 2;
bool lastRead = HIGH, stable = HIGH;
unsigned long changedAt = 0;
int presses = 0;
void setup() { pinMode(PIN, INPUT_PULLUP); Serial.begin(9600); }
void loop() {
  bool r = digitalRead(PIN);
  if (r != lastRead) { lastRead = r; changedAt = millis(); }
  if (millis() - changedAt > 30 && r != stable) {
    stable = r;
    if (stable == LOW) { presses++; Serial.println(presses); }
  }
}

Common mistakes

  • Using delay(50) after each press, which stops the robot doing anything else.
  • A debounce window longer than the fastest real press.
  • Counting in an interrupt without any time check.
  • Reading the pin without a pull-up, so it floats.
  • Subtracting times with signed variables. Use unsigned long for millis().

If this did not fix it

  • Electrical noise from motors on the same wires. Use shorter or twisted wires and a small capacitor across the switch.
  • Wrong wiring of the normally open and normally closed contacts.
  • A switch that is worn or dirty, which bounces for much longer than normal.