Polling Button Arduino Library

Button is an Arduino library. It can be included in all your programs and will handle software debouncing of the input.

In addition, it can detect rising and falling edges in a functional programming style, or you can attach functions to the rising and falling edges like an interrupt style of code.



Download latest version from GitHub.¬†And unzip the content into a “Button” folder in your Arduino libraries folder. On Windows, you should have a structure similar to this one below once done.


How to use it

From within the Arduino IDE, choose Sketch -> Include Library -> Button. Note that you might have to restart your Arduino IDE before the Button library shows in the list.



Basic example

In order to realize this, just plug a button between pin 12 or your Arduino and a ground pin, like so:

Upload this program and open the Serial Monitor console to see the magic happen!



Interrupt Style functions

This 2nd example does exactly the same thing, but uses function calls once an edge is detected.



Multiple Buttons

Software debouncing works by using millis(). If you have multiple buttons, it is good practice to do a call only once per loop and supply the value to the buttons; as in:



… And that is all!



This code is licensed under CC-BY 4.0.


Leave a comment

Your email address will not be published. Required fields are marked *