Arduino - Button Library

When using buttons, beginners usually run into the following troubles:

The ezButton (easy button )library is designed to solve all of the above problems and make it easy to use for not only beginners but also experienced users. It is created by ArduioGetStarted.com.

The library can be used for push button, momentary switches, toggle switch, magnetic contact switch (door sensor)..​.

Library Features

  • Uses the internal pull-up resistor to avoid the floating value
  • Supports debounce to eliminate the chattering phenomenon
  • Supports the pressed and released events
  • Easy to use with multiple buttons

How To Install Library

  • On Arduino IDE, Go to Sketch Include Library Manage Libraries
  • Arduino add library
  • Search “ezButton”, then find the button library by ArduinoGetStarted
  • Click Install button to install ezButton library.
  • Arduino button library

Or you can download it on Github

Examples

  • 01.SingleButton
  • 02.SingleButtonDebounce
  • 03.SingleButtonEvents
  • 04.SingleButtonAll
  • 05.MultipleButtonAll
  • 06.ButtonCount

Functions

  • Button()
  • setDebounceTime()
  • getState()
  • getStateRaw()
  • isPressed()
  • isReleased()
  • setCountMode()
  • getCount()
  • resetCount()
  • loop()

References

Button()

Description

This function creates a new instance of the Button class that represents a particular button attached to your Arduino board.

Syntax

Button(pin)

Parameters

pin: int - Arduino's pin that connects to the button.

Return

A new instance of the Button class.

Example

Button button(7);

setDebounceTime()

Description

This function is used to set the debounce time. The debounce time takes effect on getState(), isPressed(), isReleased() and getCount().

Syntax

button.setDebounceTime(time);

Parameters

time: unsigned long - debounce time in milliseconds

Return

None

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { button.setDebounceTime(100); // set debounce time to 100 milliseconds } void loop() {}

getState()

Description

This function returns the state of the button. If the debounce time is set, the returned state is the state after debouncing. We MUST call button.loop() function before using this function.

Syntax

button.getState();

Parameters

None

Return

The state of button (LOW or HIGH). If the debounce time is set, the returned state is the state after debouncing.

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { } void loop() { button.loop(); // MUST call the loop() function first int btnState = button.getState(); }

getStateRaw()

This function returns the current state of the button without debouncing. It is equivalent to digitalRead().

Description

Syntax

button.getStateRaw()

Parameters

None

Return

The current state of button without debouncing.

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { } void loop() { int btnState = button.getStateRaw(); }

isPressed()

Description

This function check whether a button is pressed or not. We MUST call button.loop() function before using this function.

Syntax

button.isPressed()

Parameters

None

Return

true if the button is pressed, false otherwise

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) Serial.println("The button is pressed"); }

isReleased()

Description

This function check whether a button is released or not. We MUST call button.loop() function before using this function.

Syntax

button.isReleased()

Parameters

None

Return

true if the button is released, false otherwise

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); } void loop() { button.loop(); // MUST call the loop() function first if(button.isReleased()) Serial.println("The button is released"); }

setCountMode()

Description

This function sets the count mode.

Syntax

button.setCountMode(mode)

Parameters

mode: int - count mode. The available count modes include: COUNT_FALLING, COUNT_RISING and COUNT_BOTH.

Return

None

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); Serial.println(count); }

getCount()

Description

This function returns the count value. We MUST call button.loop() function before using this function.

Syntax

button.getCount()

Parameters

None.

Return

The count value. If the debounce time is set, the returned value is the value after debouncing.

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); Serial.println(count); }

resetCount()

Description

This function set the counter value to 0.

Syntax

button.resetCount()

Parameters

None.

Return

None.

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); if(count >= 100) button.resetCount(); }

loop()

loop()

Description

This function does debounce and updates the state of the button. It MUST be called before using getState(), isPressed(), isReleased() and getCount() functions.

Syntax

button.loop();

Parameters

None

Return

None

Example

#include <Button.h> Button button(7); // create Button object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds } void loop() { button.loop(); // MUST call the loop() function first int btnState = button.getState(); Serial.println(btnState); if(button.isPressed()) Serial.println("The button is pressed"); if(button.isReleased()) Serial.println("The button is released"); }

Comments

If you have any questions or suggestions, please comment on our YouTube channel