ESP32 S3 UNO multiple Button

ESP32 S3 Uno multiple button

This ESP32 S3 Uno multiple button tutorial shows you how to read several buttons at the same time on the ESP32 S3 Uno form board. Every button gets its own debounce, and the code never calls delay(), so no press is missed. The example uses five buttons, but you can change it to two buttons, three buttons, four buttons or more in a few seconds.

In this tutorial, you will:

  1. Wire five buttons to the ESP32 S3 Uno
  2. Debounce multiple buttons without the delay() function
  3. Detect when each button is pressed and released with the ezButton library
  4. Handle an array of buttons with a simple for loop

Hardware Preparation

1×ESP32 S3 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 .

Overview of Button

This project builds on the single button basics. Each button here is wired and read the same way, so it helps to know how one button works first.

The button tutorial explains the button pins, how a press changes the pin state, and how to wire and code it on the ESP32 S3 Uno. The debounce tutorial explains why one press can look like many presses, and how to fix it. You can read them here:

※ NOTE THAT:

You need one button for each input. For this example, that means five buttons. No external resistors are needed, because the code turns on the internal pull-up resistor of each pin.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D2 to D6) and their GPIO numbers when you wire the buttons.

ESP32 S3 Uno pinout diagram

Wiring Diagram

One leg of each button goes to its own digital pin, and the other leg goes to GND. When you press a button, its pin reads LOW.

The wiring diagram between ESP32 S3 Uno multiple button

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Button 1 (one leg) D2 (GPIO18)
Button 2 (one leg) D3 (GPIO17)
Button 3 (one leg) D4 (GPIO19)
Button 4 (one leg) D5 (GPIO20)
Button 5 (one leg) D6 (GPIO3)
Button 1 to 5 (other leg) GND

WARNING

Button 5 uses D6 (GPIO3). This is a boot strapping pin on the ESP32 S3 Uno. The chip reads it at startup. A button to GND is normally safe, but do not hold button 5 down while you power on, reset or upload. If the board does not boot or upload, unplug the wire from D6, upload the code, then plug it back in.

※ NOTE THAT:

The ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. In this wiring the buttons only connect the pins to GND, so no 5V ever reaches the board.

ESP32 S3 Uno Code - Multiple Buttons with debounce

Reading many buttons by hand gets messy fast. You must debounce each one, and you often need to know the exact moment a button is pressed or released. Doing this with your own timers and state variables for five buttons means a lot of code.

The ezButton library does the debounce and the press/release detection for you. You only create one ezButton object per button, call its loop() function, and ask it what happened. The sketch below does this for five buttons and prints every press and release to the Serial Monitor.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // the number of buttons #define BUTTON_PIN_1 18 // The ESP32 S3 Uno pin D2 (GPIO18) connected to the button 1 #define BUTTON_PIN_2 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to the button 2 #define BUTTON_PIN_3 19 // The ESP32 S3 Uno pin D4 (GPIO19) connected to the button 3 #define BUTTON_PIN_4 20 // The ESP32 S3 Uno pin D5 (GPIO20) connected to the button 4 #define BUTTON_PIN_5 3 // The ESP32 S3 Uno pin D6 (GPIO3) connected to the button 5 ezButton button1(BUTTON_PIN_1); // create ezButton object for button 1 ezButton button2(BUTTON_PIN_2); // create ezButton object for button 2 ezButton button3(BUTTON_PIN_3); // create ezButton object for button 3 ezButton button4(BUTTON_PIN_4); // create ezButton object for button 4 ezButton button5(BUTTON_PIN_5); // create ezButton object for button 5 void setup() { Serial.begin(9600); button1.setDebounceTime(100); // set debounce time to 100 milliseconds button2.setDebounceTime(100); // set debounce time to 100 milliseconds button3.setDebounceTime(100); // set debounce time to 100 milliseconds button4.setDebounceTime(100); // set debounce time to 100 milliseconds button5.setDebounceTime(100); // set debounce time to 100 milliseconds } void loop() { button1.loop(); // MUST call the loop() function first button2.loop(); // MUST call the loop() function first button3.loop(); // MUST call the loop() function first button4.loop(); // MUST call the loop() function first button5.loop(); // MUST call the loop() function first // get button state after debounce int button1_state = button1.getState(); // the state after debounce int button2_state = button2.getState(); // the state after debounce int button3_state = button3.getState(); // the state after debounce int button4_state = button4.getState(); // the state after debounce int button5_state = button5.getState(); // the state after debounce /* 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"); }

Detailed Instructions

Follow these steps one by one:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Open the library manager by clicking the Libraries icon on the left side of the Arduino IDE.
  6. Find the library by typing ezButton in the search box. Pick the button library made by ArduinoGetStarted.com.
  7. Install it by clicking the Install button.
  • Search for ezButton created by ArduinoGetStarted.com and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 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
ESP32S3 Dev Module on COM15
1
  1. Copy the code above and paste it into the Arduino IDE.
  2. Upload the code by clicking the Upload button.
How to upload ESP32 S3 Uno code on Arduino IDE
  1. Open the Serial Monitor and set the baud rate to 9600.
  2. Test the buttons by pressing and releasing them one at a time. You will see:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: if one button prints nothing, swap its wire with a button that works. If the problem moves with the wire, the button or wire is bad. If it stays, check the pin number in the code.

ESP32 S3 Uno Code - Multiple Buttons using array

The first sketch repeats almost the same lines five times. That works, but it is long, and adding a sixth button means copying every block again.

A better way is to put all ezButton objects in an array. Then one for loop sets the debounce time, one for loop updates every button, and one for loop checks for presses and releases. To add or remove buttons, you only change BUTTON_NUM and the pin list. The output in the Serial Monitor is the same as before.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 5 // the number of buttons #define BUTTON_PIN_1 18 // The ESP32 S3 Uno pin D2 (GPIO18) connected to the button 1 #define BUTTON_PIN_2 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to the button 2 #define BUTTON_PIN_3 19 // The ESP32 S3 Uno pin D4 (GPIO19) connected to the button 3 #define BUTTON_PIN_4 20 // The ESP32 S3 Uno pin D5 (GPIO20) connected to the button 4 #define BUTTON_PIN_5 3 // The ESP32 S3 Uno pin D6 (GPIO3) connected to the 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); // set debounce time to 100 milliseconds } } void loop() { for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // MUST call the loop() function first for (byte i = 0; i < BUTTON_NUM; i++) { // get button state after debounce int button_state = buttonArray[i].getState(); // the state after debounce /* 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

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

How many buttons can I connect to the ESP32 S3 Uno?

Each button needs one input pin, so the limit is the number of free pins. On the Uno headers you can use D2 to D13 and A0 to A5 as digital inputs. Avoid D0 and D1, because they carry the USB serial data. For many more buttons, use a button matrix or an I/O expander.

Do I need pull-up resistors for each button?

No. The ezButton library sets each pin to INPUT_PULLUP, so the internal pull-up resistor of the ESP32 S3 Uno keeps the pin HIGH. A press connects the pin to GND and the pin reads LOW. Just wire one leg to the pin and the other leg to GND.

Can I wire the buttons to 5V instead of GND?

No. The ESP32 S3 Uno pins work at 3.3V and are NOT 5V tolerant. If you wire a button between a pin and 5V, a press puts 5V on the pin and can damage it. Wire the buttons to GND as shown in this tutorial.

Why do my buttons print "pressed" many times for one press?

That is contact bounce. The metal inside the button jumps a few times before it settles. The code sets a 100 ms debounce time with setDebounceTime(), which hides the bounce. If you still see double presses, raise the value to 150 or 200.

Is button 5 on D6 safe to use?

Yes, in most cases. D6 is GPIO3, a boot strapping pin, but a button to GND does not change the boot as long as you do not hold it during power on or upload. If you have trouble, move button 5 to a free pin and change BUTTON_PIN_5 in the code.

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

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them, and write the GPIO number in the code, for example #define BUTTON_PIN_6 35. For buttons, GPIO15, GPIO16 and GPIO35 to GPIO42 are the best choice. Do not use GPIO47/GPIO48, because they are output only. Also be careful with two warnings. First, GPIO47/GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port in the Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, try another USB cable, or hold BOOT while you press RESET
Upload fails or board does not start Button 5 on D6 (GPIO3) held down during reset or upload Release all buttons while you upload, or unplug the D6 wire and plug it back after upload
One button never prints anything Loose wire, wrong pin, or button legs on the same side of the breadboard gap Check the wiring, check the pin number in the code, and place the button across the breadboard gap
A button prints pressed all the time Button wired to 3.3V instead of GND, or legs that are always connected Wire the other leg to GND and rotate the button 90 degrees on the breadboard
One press prints many times Debounce time too short Increase the value in setDebounceTime(), for example to 150 or 200
Compile error about ezButton.h ezButton library not installed Install ezButton by ArduinoGetStarted.com from the Library Manager
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud to match Serial.begin(9600)

※ 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!