Debouncing library for Arduino and Wiring by Thomas Ouellet Fredericks with many contributions from the community : https://github.com/thomasfredericks/Bounce2/graphs/contributors
Basically, the mechanical part of buttons and switches vibrate slightly when closed or opened causing multiple undesired false states (similar to noise). This library filters out these undesired state changes. More about debouncing:
See the bottom of this page for a basic usage example and the "examples" folder for more.
The library is composed of three classes:
- Debouncer : The code that does the actual debouncing. Only advanced users should play with this class.
- Bounce : This is the general use library. It links the Debouncer to a hardware pin on your board.
- Button : A special version of Bounce for buttons that are pressed.
INSTALLATION & DOWNLOAD
Install through your software's Library Manager or download the latest version here and put the "Bounce2" folder in your "libraries" folder.
Please note that the original version of this library (Bounce 1) is included in the "extras" folder of the download but not supported anymore.
BASIC USE
INSTANTIATE
#include <Bounce2.h>
The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named,...
Definition: Bounce2.h:181
SETUP
b.
attach ( <PIN> , <PIN MODE> );
void attach(int pin, int mode)
Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
Definition: Bounce2.cpp:153
void interval(uint16_t interval_millis)
Sets the debounce interval in milliseconds.
Definition: Bounce2.cpp:14
LOOP
int deboucedValue = b.
read();
}
bool changed() const
Returns true if the state changed on last update.
Definition: Bounce2.h:130
bool update()
Updates the pin's state.
Definition: Bounce2.cpp:32
bool read() const
Returns the pin's state (HIGH or LOW).
Definition: Bounce2.cpp:122
BOUNCE EXAMPLE
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
int ledState = LOW;
void setup() {
b.
attach(BUTTON_PIN,INPUT_PULLUP);
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
ledState = !ledState;
digitalWrite(LED_PIN,ledState);
}
}
bool fell() const
Returns true if pin signal transitions from high to low.
Definition: Bounce2.cpp:132
BUTTON EXAMPLE
#include <Bounce2.h>
#define BUTTON_PIN 39
#define LED_PIN 13
bool ledState = LOW;
void setup() {
button.
attach( BUTTON_PIN, INPUT );
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
ledState = !ledState;
digitalWrite(LED_PIN,ledState);
}
}
DOCUMENTATION
The complete class documentation can be found in the "docs" folder or online here.
GITHUB PAGE (SOURCE CODE)
https://github.com/thomasfredericks/Bounce2
HAVE A QUESTION?
Please post your questions here.
ALTERNATE DEBOUNCE ALGORITHMS FOR ADVANCED USERS AND SPECIFIC CASES
STABLE INTERVAL
By default, the Bounce library uses a stable interval to process the debouncing. This is simpler to understand and can cancel unwanted noise.

LOCK-OUT INTERVAL
By defining "#define BOUNCE_LOCK_OUT" in "Bounce.h" (or in your code before including "Bounce.h") you can activate an alternative debouncing method. This method is a lot more responsive, but does not cancel noise.

WITH PROMPT DETECTION
By defining "#define BOUNCE_WITH_PROMPT_DETECTION" in "Bounce.h" (or in your code before including "Bounce.h") you can activate an alternative debouncing method. Button state changes are available immediately so long as the previous state has been stable for the timeout period. Otherwise the state will be updated as soon as the timeout period allows.
- Able to report acurate switch time normally with no delay.
- Use when accurate switch transition timing is important.
#define BOUNCE_WITH_PROMPT_DETECTION