Button contact bounce counter
Count how many times the Nano thinks a single button press happened, then compare with a version that waits a few milliseconds. You see contact bounce for yourself and learn why every button needs debouncing.
Build it step by step
0 of 6 doneWire the button between D2 and GND
The internal pull-up does the rest.
Upload and open the serial monitor at 115200
The prompt line appears.
Press once
Both counts print.
Press fifty times, evenly
Compare raw and clean.
Change DEBOUNCE_MS to 5, then 50
Upload and repeat.
Try a second button
Note which bounces more.
How do you build a button contact bounce counter?
Count how many times the Nano thinks a single button press happened, then compare with a version that waits a few milliseconds. You see contact bounce for yourself and learn why every button needs debouncing. You need Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 40 minutes.
- Level
- Beginner
- Time
- About 40 min
- Parts
- 4
Parts list
- Development BoardsBuyArduino Nano × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Button leg 1 | Nano D2 | Button input |
| Button leg 2 | Nano GND | Pressing joins D2 to ground |
Code
When the metal contacts inside a push button meet, they do not close cleanly. They bounce for a millisecond or two, and a fast board sees several presses where you made one. The sketch counts two things at once. The raw count adds one for every drop to ground it sees, bounces included. The clean count only adds one when the signal has stayed steady for the debounce time. It waits until things settle before printing, so serial printing does not hide the bounces. Press the button fifty times, as evenly as you can, and note both counts. Then change DEBOUNCE_MS to 0, 5, 20 and 50 and repeat. You should see the clean count match your presses at around ten to twenty milliseconds, and fall short of them if the wait is too long and you press quickly. Different buttons bounce differently, so try two.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const uint8_t BUTTON_PIN = 2;
const unsigned long DEBOUNCE_MS = 20; // try 0, 5, 20 and 50
bool lastRaw = HIGH;
bool lastStable = HIGH;
unsigned long lastChange = 0;
unsigned long rawCount = 0; // every falling edge, bounces included
unsigned long cleanCount = 0; // only edges that stayed put
bool changed = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Press the button. raw = every edge, clean = after debounce.");
}
void loop() {
bool r = digitalRead(BUTTON_PIN);
if (r != lastRaw) { // any change, including bounce
lastRaw = r;
lastChange = millis();
if (r == LOW) rawCount++;
changed = true;
}
if (millis() - lastChange > DEBOUNCE_MS && r != lastStable) {
lastStable = r;
if (r == LOW) cleanCount++;
changed = true;
}
if (changed && millis() - lastChange > 150) { // print once things have settled
Serial.print("raw ");
Serial.print(rawCount);
Serial.print(" clean ");
Serial.println(cleanCount);
changed = false;
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Press the button. raw = every edge, clean = after debounce.
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| One press | Clean count rises by exactly one |
| Raw count | Often higher than your presses |
| DEBOUNCE_MS 0 | Clean count is as wrong as raw |
| DEBOUNCE_MS 20 | Clean count matches your presses |
| DEBOUNCE_MS 50 with fast presses | Some presses are missed |
If it does not work
Nothing prints
Set 115200 baud in the monitor.
Counts rise with no press
Check the button is wired to GND and D2 only.
Raw equals clean
Your button may be a clean one, try another.
Counts jump by two
Press and release evenly.
Upload fails
Choose the Old Bootloader option.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the button contact bounce counter?
Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the button contact bounce counter take to build?
About 40 minutes for a beginner maker, in 6 steps.
How do I know the button contact bounce counter works?
One press: Clean count rises by exactly one. Raw count: Often higher than your presses. DEBOUNCE_MS 0: Clean count is as wrong as raw.
Nothing prints: what should I check?
Set 115200 baud in the monitor.
Counts rise with no press: what should I check?
Check the button is wired to GND and D2 only.
Raw equals clean: what should I check?
Your button may be a clean one, try another.
Counts jump by two: what should I check?
Press and release evenly.
