The Bounce2 Library

About | Install using Arduino | List of Methods

About the Bounce2 Library

Anytime I rely on buttons or switches in my programs, especially projects where I want something to happen when a state changes, I use the Bounce2 library.

Bounce2 is the second iteration of Thomas Frederick’s debouncing library for Arduino (or Wiring), with contributions by Eric Lowry, Jim Schimpf, and Tom Harkaway.

Some projects require a debouncer to prevent chatter than can occur when flipping a switch, or tapping a button. The springy metals inside electrical hardware can bounce apart in very fast succession, which may trigger multiple events in the Arduino that should have acted as one.

Snapshot from an oscilloscope of a bouncy signal of a switch.

Snapshot from an oscilloscope of a bouncy signal of a switch.

This contact bouncing only occurs in first moments that the switch/button has been hit, with a steady signal after. A debouncer waits a few milliseconds for the signal to smooth out, then reads the signal, preventing the bounciness from causing havoc with your readings.

Install using Arduino

Installing the Bounce2 library is simple though the Arduino program. You can do this by going to Sketch > Include Library > Manage Libraries…

Bounce2 Manage Libraries

A list of libraries contributed to the Arduino site will appear.

Bounce2 Libraries

Use the Filter in the top of the window to search for “Bounce”. The latest Bounce library should show up: Bounce2 by Thoman O Fredericks. Click install. Bounce2 should now appear inside your Sketch > Include Library list below Contributed Libraries.

List of Bounce2 Library Methods

Always make sure you have included the Bounce2 Library to your project by going to Sketch > Include Library > Bounce2 or by typing in the following at the top of your sketch.

Bounce()

Creates a Bounce object, which you will later assign to an input pin.

void attach(int pin)
void interval(unsigned long interval)

Drop these both into the setup() function to set the Bounce object’s input pin that will be debounced, and the interval delay in milliseconds before the pin is read. 5 milliseconds is a good value to use.

bool update()

Bounce objects don’t use interrupts, so you need to update the object to check for state changes once per loop(). This method returns true (1) if the pin state just changed, and false (0) if no change was detected.

bool read()

Returns the updated/current pin state (1 for HIGH, 0 for LOW).

bool rose()

Returns true if the pin state just changed from LOW to HIGH, such as when a button is pressed.

bool fell()

Returns true if the pin state just changed from HIGH to LOW, such as when a button is released.

 

You can find the Bounce2 Library’s GitHub page here.