Arduino UNO R4 multiple Button
This guide shows you how to use an Arduino UNO R4 with multiple buttons at the same time without the delay() function to debounce. The guide offers code in two different methods:
We will use five buttons as examples. You can change this easily for two buttons, four buttons, or even more.
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
We offer a detailed guide on buttons, covering hardware connections, how they work, wiring with Arduino UNO R4, and how to write the code. Learn more here:
This image is created using Fritzing. Click to enlarge image
When using several buttons, situations can become complex.
The ezButton library simplifies working with buttons by handling debounce and button events internally. Users do not need to worry about managing timestamps and variables when using this library. Also, using multiple buttons can make the code clearer and shorter.
#include <ezButton.h>
#define BUTTON_NUM 5
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define BUTTON_PIN_3 4
#define BUTTON_PIN_4 5
#define BUTTON_PIN_5 6
ezButton button1(BUTTON_PIN_1);
ezButton button2(BUTTON_PIN_2);
ezButton button3(BUTTON_PIN_3);
ezButton button4(BUTTON_PIN_4);
ezButton button5(BUTTON_PIN_5);
void setup() {
Serial.begin(9600);
button1.setDebounceTime(100);
button2.setDebounceTime(100);
button3.setDebounceTime(100);
button4.setDebounceTime(100);
button5.setDebounceTime(100);
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();
int button1_state = button1.getState();
int button2_state = button2.getState();
int button3_state = button3.getState();
int button4_state = button4.getState();
int button5_state = button5.getState();
if (button1.isPressed())
Serial.println("The button 1 is pressed");
if (button1.isReleased())
Serial.println("The button 1 is released");
if (button2.isPressed())
Serial.println("The button 2 is pressed");
if (button2.isReleased())
Serial.println("The button 2 is released");
if (button3.isPressed())
Serial.println("The button 3 is pressed");
if (button3.isReleased())
Serial.println("The button 3 is released");
if (button4.isPressed())
Serial.println("The button 4 is pressed");
if (button4.isReleased())
Serial.println("The button 4 is released");
if (button5.isPressed())
Serial.println("The button 5 is pressed");
if (button5.isReleased())
Serial.println("The button 5 is released");
}
Follow these instructions step by step:
Connect the Arduino Uno R4 to the buttons according to the provided diagram.
Connect the Arduino Uno R4 board to your computer using a USB cable.
Launch the Arduino IDE on your computer.
Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
Click on the Libraries icon on the left side of the Arduino IDE.
Search for ezButton and locate the button library created by ArduinoGetStarted.com.
Click on the Install button to install the ezButton library.
The button 1 is pressed
The button 1 is released
The button 2 is pressed
The button 2 is released
The button 3 is pressed
The button 3 is released
The button 4 is pressed
The button 4 is released
The button 5 is pressed
The button 5 is released
We can make the above code better by using an array of buttons.
#include <ezButton.h>
#define BUTTON_NUM 5
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define BUTTON_PIN_3 4
#define BUTTON_PIN_4 5
#define BUTTON_PIN_5 6
ezButton buttonArray[] = {
ezButton(BUTTON_PIN_1),
ezButton(BUTTON_PIN_2),
ezButton(BUTTON_PIN_3),
ezButton(BUTTON_PIN_4),
ezButton(BUTTON_PIN_5)
};
void setup() {
Serial.begin(9600);
for (byte i = 0; i < BUTTON_NUM; i++) {
buttonArray[i].setDebounceTime(100);
}
}
void loop() {
for (byte i = 0; i < BUTTON_NUM; i++)
buttonArray[i].loop();
for (byte i = 0; i < BUTTON_NUM; i++) {
int button_state = buttonArray[i].getState();
if (buttonArray[i].isPressed()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is pressed");
}
if (buttonArray[i].isReleased()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is released");
}
}
}