ESP32 UNO multiple Button

ESP32 Uno multiple buttons

Need five inputs on one board? Read ESP32 Uno multiple buttons at the same time, each one cleanly debounced, and never block the loop with delay(). The ESP32 Uno form board catches every press and every release of every button, even when two fingers hit two keys at once. Two sketches are included: one with a separate ezButton object per key, and one with an array of buttons that grows to three, four or more keys by editing two lines.

What you will do:

  1. Wire five push buttons to the ESP32 Uno digital header
  2. Install the ezButton library in Arduino IDE
  3. Debounce several buttons without the delay() function
  4. Print a message for each button press and release
  5. Shrink the code with an array of buttons and a for loop

Components You Need

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Button
1×Push Button Module
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
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 .

Button Background

After this part, you know where to learn the single-button details that this project repeats five times.

Topic Where to Read It
Button legs, pull-up idea, single-button wiring and code ESP32 Uno - Button
Why one tap can fire many events, and the fix ESP32 Uno - Button Debounce

Good to know: ezButton turns on the internal pull-up of every button pin, so this build needs zero extra resistors. A pin reads HIGH when its key is up and LOW while you hold it down.

ESP32 Uno Pinout

Your sketch talks to GPIO numbers, while the board shows Uno labels such as D2 or D6. Use this map to find the GPIO behind each header hole before you plug in the jumper wires.

ESP32 Uno pinout diagram

How to Connect

At the end of this part, all five keys share one GND rail and each has its own signal pin.

The wiring diagram between ESP32 Uno multiple button

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 Uno Pin
Button 1 (first leg) D2 (GPIO26)
Button 2 (first leg) D3 (GPIO25)
Button 3 (first leg) D4 (GPIO17)
Button 4 (first leg) D5 (GPIO16)
Button 5 (first leg) D6 (GPIO27)
Buttons 1 to 5 (second leg) GND

Good to know: all five pins (GPIO26, 25, 17, 16, 27) are normal I/O pins with an internal pull-up. None of them is a boot strapping pin, so a pressed key cannot stop an upload.

※ NOTE THAT:

The ESP32 Uno is a 3.3V board, and its inputs are NOT 5V tolerant. In this build every key only shorts its pin to GND, so the board stays safe. Never wire a key between a pin and the 5V header.

ESP32 Uno Code - Five Buttons, One Object Each

By the end of this part, the board reports every press and release of five keys in the Serial Monitor.

Hand-written debounce for five keys means five timers, five "last state" variables and a lot of copy and paste. It gets harder still when you want the exact moment a key goes down or comes back up. The ezButton library keeps those timers and states inside each object. You call loop(), then ask isPressed() or isReleased().

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // how many buttons this sketch reads #define BUTTON_PIN_1 26 // D2 (GPIO26) reads button 1 #define BUTTON_PIN_2 25 // D3 (GPIO25) reads button 2 #define BUTTON_PIN_3 17 // D4 (GPIO17) reads button 3 #define BUTTON_PIN_4 16 // D5 (GPIO16) reads button 4 #define BUTTON_PIN_5 27 // D6 (GPIO27) reads button 5 ezButton button1(BUTTON_PIN_1); // ezButton instance for button 1 ezButton button2(BUTTON_PIN_2); // ezButton instance for button 2 ezButton button3(BUTTON_PIN_3); // ezButton instance for button 3 ezButton button4(BUTTON_PIN_4); // ezButton instance for button 4 ezButton button5(BUTTON_PIN_5); // ezButton instance for button 5 void setup() { Serial.begin(9600); button1.setDebounceTime(100); // ignore bounce shorter than 100 ms button2.setDebounceTime(100); // ignore bounce shorter than 100 ms button3.setDebounceTime(100); // ignore bounce shorter than 100 ms button4.setDebounceTime(100); // ignore bounce shorter than 100 ms button5.setDebounceTime(100); // ignore bounce shorter than 100 ms } void loop() { button1.loop(); // update button 1 before any other check button2.loop(); // update button 2 before any other check button3.loop(); // update button 3 before any other check button4.loop(); // update button 4 before any other check button5.loop(); // update button 5 before any other check // read the filtered (debounced) state of each button int button1_state = button1.getState(); // stable state of button 1 int button2_state = button2.getState(); // stable state of button 2 int button3_state = button3.getState(); // stable state of button 3 int button4_state = button4.getState(); // stable state of button 4 int button5_state = button5.getState(); // stable state of button 5 /* Serial.print("The button 1 state: "); Serial.println(button1_state); Serial.print("The button 2 state: "); Serial.println(button2_state); Serial.print("The button 3 state: "); Serial.println(button3_state); Serial.print("The button 4 state: "); Serial.println(button4_state); Serial.print("The button 5 state: "); Serial.println(button5_state); */ 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"); }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Open the Library Manager with the Libraries icon in the left bar.
  6. Search for ezButton and find the entry by ArduinoGetStarted.com.
  7. Click Install and wait until the IDE says it is done.
  • Search for ezButton created by ArduinoGetStarted.com and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Library Manager
Type:
All
Topic:
All
ezButton by ArduinoGetStarted.com
Button library supports debounce, pressed/released events and the press counting. It is easy to use with multiple buttons. The library can be used for push-button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is designed for not only beginners but also experienced users. More info
1.0.6
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32 Dev Module on COM15
1
  1. Paste the sketch into a new Arduino IDE window.
  2. Send it to the board by clicking the Upload arrow.
How to upload ESP32 Uno code on Arduino IDE
  1. Open the Serial Monitor at 9600 baud.
  2. Press the keys in a mixed order, for example 3, 1, 5, then hold 2 while you tap 4. Each event prints on its own line:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The button 3 is pressed The button 3 is released The button 1 is pressed The button 1 is released The button 5 is pressed The button 5 is released The button 2 is pressed The button 4 is pressed The button 4 is released The button 2 is released
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: put a small sticker with the number 1 to 5 on each key cap. When a line looks wrong, you see at once if the fault is in the wiring or in your finger.

ESP32 Uno Code - Button Array Version

By the end of this part, the same five keys run on a much shorter sketch.

The first sketch has the same line five times in four places. Adding key number six means editing every one of those blocks. With an array, three short for loops do the work: one sets the debounce time, one updates the keys, one checks for events. To change the key count, edit BUTTON_NUM and the pin list. The Serial Monitor output stays the same.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // how many buttons are in the array #define BUTTON_PIN_1 26 // D2 (GPIO26) reads button 1 #define BUTTON_PIN_2 25 // D3 (GPIO25) reads button 2 #define BUTTON_PIN_3 17 // D4 (GPIO17) reads button 3 #define BUTTON_PIN_4 16 // D5 (GPIO16) reads button 4 #define BUTTON_PIN_5 27 // D6 (GPIO27) reads button 5 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); // 100 ms bounce filter for every button } } void loop() { for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // refresh each button first for (byte i = 0; i < BUTTON_NUM; i++) { // read the filtered state of this button int button_state = buttonArray[i].getState(); // stable (debounced) state /* Serial.print("The button "); Serial.print(i + 1); Serial.print(": "); Serial.println(button_state); */ 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"); } } }

Video Tutorial

See the ESP32 Uno build in action in the video below.

How the Code Works

Code Part What It Does
#define BUTTON_PIN_1 26 ... 27 Links each key to its GPIO (D2 to D6 on the header)
ezButton button1(BUTTON_PIN_1) Makes one object per key and sets the pin to INPUT_PULLUP
setDebounceTime(100) Ignores any change shorter than 100 ms
button1.loop() Samples the pin and updates the debounce timer, so call it first in every loop
isPressed() / isReleased() Returns true one time, right when the key goes down or comes up
ezButton buttonArray[] + for loop Same job for all keys in a few lines (array sketch)

Since nothing in loop() waits, the board checks all five keys many thousands of times per second and never misses a short tap.

ESP32 Uno vs UNO R4 for This Project

This table lists what changes when you move the five-button build from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Button pins in code 2, 3, 4, 5, 6 26, 25, 17, 16, 27
Header holes used D2 to D6 D2 to D6 (same spots)
Library ezButton ezButton (no change)
Arduino IDE board Arduino UNO R4 WiFi or Minima ESP32 Dev Module
WiFi and Bluetooth WiFi only on the WiFi model WiFi plus Bluetooth Classic and BLE

The wiring stays the same hole for hole. Only the five pin numbers in the sketch change, because the ESP32 Uno code uses GPIO numbers.

Go Wireless: Button Status on Your Phone

At the end of this part, you have an idea for showing the key events on a phone.

The ESP32 Uno has WiFi and Bluetooth built in. Instead of the Serial Monitor, send the "button N is pressed" text to a web page or a phone app. This turns the five keys into a small wireless remote panel, and you can watch them from across the room. Each key could also switch a virtual output on the phone.

Try These Next

  • Count how many times each key was pressed and print the totals every 10 seconds.
  • Use key 1 and key 2 as "up" and "down" to change a number from 0 to 100.
  • Make a five-key code lock: the right order 4-2-5-1-3 prints "Unlocked".
  • Add a sixth key by raising BUTTON_NUM and adding one pin to the array sketch.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE CH340 driver missing or charge-only USB cable Install the CH340 driver and use a USB cable that carries data
Upload stops at "Failed to connect" or "Waiting for download" Board did not enter download mode Hold the BOOT button while the upload starts, then let go
A key prints nothing at all Jumper in the wrong header hole or key legs on the same breadboard side Check the D2 to D6 holes and turn the key so it spans the middle gap
A key reports pressed and never released Second leg goes to 3.3V or the two legs are always joined Move the second leg to GND and turn the key by 90 degrees
Two keys fire together Both signal wires share one breadboard row Give every key its own row and its own signal wire
One tap gives several lines Debounce time too short for a worn key Raise setDebounceTime() to 150 or 200
"ezButton.h No such file or directory" Library not installed Install ezButton by ArduinoGetStarted.com in the Library Manager
Random symbols in the Serial Monitor Baud rate does not match the sketch Pick 9600 baud in the Serial Monitor

FAQ

Does each push button need its own pin on the ESP32 Uno?

Yes, in this design each key uses one input pin and all keys share GND. Five keys take five pins. For a large keypad, a button matrix or an I/O expander saves pins.

Do I have to add a 10k resistor to every button?

No. ezButton sets each pin to INPUT_PULLUP, and the pins used here (GPIO26, 25, 17, 16, 27) have an internal pull-up. One wire from the key to its pin and one wire to the GND rail is all it takes.

Can I use the A0 to A5 holes for more buttons?

A0 (GPIO2) and A1 (GPIO4) work as button inputs, but A0 is a boot strapping pin, so do not hold that key during reset. A2 to A5 (GPIO35, 34, 36, 39) are input only and have no internal pull-up. If you use them, add a 10k resistor from the pin to 3.3V, or the input floats and prints random events.

Which ESP32 Uno pins should I avoid for buttons?

Skip D0 and D1 (GPIO3 and GPIO1), because they carry the USB serial data. Be careful with D8 (GPIO12) and D10 (GPIO5), since they are boot strapping pins. A key held down at power-on on those pins can stop the board from starting.

How do I go from five buttons to two or three?

In the array sketch, set BUTTON_NUM to the new count and keep only that many entries in buttonArray[]. In the first sketch, delete the lines for the keys you do not need.

Can the ESP32 Uno tell when two buttons are held at once?

Yes. Every key has its own ezButton object, so the events do not block each other. Call getState() on both keys; LOW on both means both are held.

Will my old Uno button sketch run on the ESP32 Uno?

Almost. Compared with the UNO R4 version, the library and logic are the same, but the pin numbers must change from 2 to 6 into 26, 25, 17, 16 and 27. Also pick ESP32 Dev Module as the board in Arduino IDE.

Can I fit an Uno button shield on the ESP32 Uno?

Often yes, since the header shape matches. Check that the shield does not pull the keys up to 5V. A shield that feeds 5V into the pins can damage the 3.3V ESP32 Uno inputs.

What if my project needs more pins than the Uno headers give?

The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they work with WiFi on) and GPIO15 (I/O, PWM, analog on ADC2). Solder pin headers to them and write the GPIO number in code, for example #define BUTTON_PIN_6 32. For extra keys, use GPIO32 and GPIO33 first. Two warnings: GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11), because they are wired to the flash chip and using them crashes the board.

Can I use these buttons to control things over WiFi?

Yes. The ESP32 Uno has WiFi and Bluetooth, so a key press can update a web page, send a message to a phone app, or switch a relay on another board. See the Go Wireless section above for app tutorials.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!