Arduino Bounce2 Button Debounce: Wiring, Code and Simulator
To build this Arduino Bounce2 Button Debounce project you need an Arduino Uno and a push button. Bounce2 debounces a push button so each press counts once. Make the 2 connections in the wiring table, upload the Arduino C++ code, and press the button quickly several times: the count never jumps by two.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | push button | Connects 1 to 2 while held. Pair with INPUT_PULLUP and wire the other side to GND. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 2 | push button 2.l |
| 2 | Arduino Uno GND | push button 1.r |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a push button from the parts palette.
- Wire 1. Connect Arduino Uno pin 2 to push button 2.l.
- Wire 2. Connect Arduino Uno GND to push button 1.r.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Press the button quickly several times: the count never jumps by two.
Arduino C++ code
// Bounce2: count clean button presses on pin 2 (button to GND, internal pull-up).
#include <Bounce2.h>
Bounce2::Button button;
int presses = 0;
void setup() {
Serial.begin(9600);
button.attach(2, INPUT_PULLUP);
button.interval(10);
button.setPressedState(LOW);
}
void loop() {
button.update();
if (button.pressed()) {
presses++;
Serial.print("Presses: ");
Serial.println(presses);
}
}
What to try
Press the button quickly several times: the count never jumps by two.
Frequently asked questions
How do I wire the Bounce2 Button Debounce project on an Arduino Uno?
Make 2 connections: Arduino Uno pin 2 to push button 2.l; Arduino Uno GND to push button 1.r.
What parts do I need for this Arduino project?
An Arduino Uno and a push button.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
Bounce2 debounces a push button so each press counts once.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator