Arduino Nano ESP32 - Button Module

In this guide, we will learn how to use a push button module as a simple input for the Arduino Nano ESP32. Think of a start/stop key for a WiFi data logger, or a "next page" key for a small display: one finger press, one clear signal for your sketch. In detail, we will learn:

Arduino Nano ESP32 and push button module

Hardware Preparation

1×Arduino Nano ESP32
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×Button Module
1×Jumper Wires
1×Breadboard
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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 Module

This module is a small PCB with one momentary tactile switch and a colored cap on top. In short, it is a push button that already carries its own resistor. A pack holds five of them, one each in red, yellow, green, blue and white, so you can give every key in a project its own color.

Button module pinout
image source: diyables.io

On the edge there is a 3-pin header with 2.54mm spacing. The labels read VCC, OUT and GND, in that order:

  • VCC pin: connect to the 3V3 pin of the Nano ESP32. The module accepts 3.3V to 5V, but here it must be 3.3V.
  • OUT pin: connect to a digital input pin of the Arduino Nano ESP32 (D2 in this guide).
  • GND pin: connect to GND (0V).

What the OUT pin reports:

  • Key up (released) → the on-board pull-down resistor holds OUT at LOW.
  • Key down (pressed) → OUT is tied to VCC, so it reads HIGH.

So the logic is the natural one: HIGH means pressed. No extra resistor is needed and the input pin should be set as plain INPUT. INPUT_PULLUP is the wrong choice here, because the internal pull-up would work against the resistor on the module.

Like every mechanical switch, the metal contacts inside do not close cleanly. For a few milliseconds they bounce, so one press can look like several quick presses to a fast board like the Nano ESP32. The second and third sketches below deal with that.

※ NOTE THAT:

Why 3V3 and not VBUS? When the key is down, OUT gives out the same voltage as VCC. If VCC comes from the VBUS (5V) pin, D2 would get 5V, and the GPIO pins of the Nano ESP32 are made for 3.3V only. That can damage the board. Always power this module from 3V3.

Wiring Diagram

Three jumper wires and no breadboard. The module's VCC goes to 3V3, OUT goes to D2, and GND goes to GND. D2 is the same pin used by the other button tutorials for this board, so you can reuse their sketches later with little change.

The wiring diagram between Arduino Nano ESP32 and button module

This image is created using Fritzing. Click to enlarge image

Pin connections between Button Module and Arduino Nano ESP32

Button Module Arduino Nano ESP32
VCC → 3V3 (not VBUS)
OUT → D2
GND → GND

How To Program For Button Module

All three sketches start the same way. Only the last one needs a library.

  • Name the pin that reads the OUT signal:
const int BUTTON_MODULE_PIN = D2;
  • Make it a plain input in setup(). The module's own pull-down does the rest:
pinMode(BUTTON_MODULE_PIN, INPUT);
  • Read the level. HIGH means the key is down, LOW means it is up:
int level = digitalRead(BUTTON_MODULE_PIN);

Arduino Nano ESP32 Code - Read the Raw State

The first sketch does the simplest job: five times per second, it reads D2 and prints the level together with a short label. Use it to check the wiring before anything else.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-module */ const int BUTTON_MODULE_PIN = D2; // Arduino Nano ESP32 pin connected to the OUT pin of the button module void setup() { Serial.begin(9600); // Plain INPUT: the module already has its own pull-down resistor. // Do NOT use INPUT_PULLUP here, it would fight the pull-down on the module. pinMode(BUTTON_MODULE_PIN, INPUT); } void loop() { int level = digitalRead(BUTTON_MODULE_PIN); // HIGH = pressed, LOW = released Serial.print("OUT level: "); Serial.print(level); if (level == HIGH) Serial.println(" -> button is held down"); else Serial.println(" -> button is up"); delay(200); // slow down the printing so the Serial Monitor is easy to read }

Detailed Instructions

Follow these instructions step by step:

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Make the three connections from the table above. Double-check that VCC goes to 3V3.
  • Plug the Arduino Nano ESP32 into your computer with the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Nano ESP32 board (e.g., Arduino Nano ESP32) and COM port.
  • Paste the code above into a new sketch.
  • Click the Upload button.
  • Open the Serial Monitor at 9600 baud.
  • Leave the key alone for a moment, then hold it down for about one second, then let go.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
OUT level: 0 -> button is up OUT level: 0 -> button is up OUT level: 0 -> button is up OUT level: 1 -> button is held down OUT level: 1 -> button is held down OUT level: 1 -> button is held down OUT level: 1 -> button is held down OUT level: 1 -> button is held down OUT level: 0 -> button is up OUT level: 0 -> button is up
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

The output stays at 0 when nobody touches the key, without random jumps. That steady 0 is the job of the pull-down resistor. If you see 1 all the time, the OUT and VCC wires are probably swapped.

Arduino Nano ESP32 Code - Press and Release Events with delay()

Most projects do not care about the level itself. They care about the moment it changes: a doorbell rings once per press, a counter adds one per press. This sketch remembers the last state. When a new reading is different, it waits 50 milliseconds for the bouncing to end, reads D2 a second time, and only then reports the event.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-module */ const int BUTTON_MODULE_PIN = D2; // Arduino Nano ESP32 pin connected to the OUT pin of the button module const int DEBOUNCE_MS = 50; // time to wait for the contacts to stop bouncing int lastState = LOW; // the module outputs LOW while the button is up void setup() { Serial.begin(9600); pinMode(BUTTON_MODULE_PIN, INPUT); // the module has its own pull-down resistor } void loop() { int reading = digitalRead(BUTTON_MODULE_PIN); if (reading != lastState) { // The level changed. It may be a real press/release or just bouncing. delay(DEBOUNCE_MS); // wait (the loop is blocked here) reading = digitalRead(BUTTON_MODULE_PIN); // read again after the wait if (reading != lastState) { // the change is still there, so it is real if (reading == HIGH) Serial.println("The button is pressed"); else Serial.println("The button is released"); lastState = reading; // remember the confirmed state } } }

Detailed Instructions

  • Keep the wiring as it is.
  • Upload the code above to the Arduino Nano ESP32.
  • With the Serial Monitor open, press and release the key three times.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
The button is pressed The button is released The button is pressed The button is released The button is pressed The button is released
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Each press gives exactly one pair of lines. The weak point is delay(): for those 50 milliseconds the Nano ESP32 does nothing else, so a sketch that also handles WiFi or a display will freeze for a short moment on every change.

Arduino Nano ESP32 Code - Button Module with the ezButton Library

The ezButton library does the same debounce, but it checks the time with millis() instead of waiting. The module has a pull-down, so the object is created with EXTERNAL_PULLDOWN, which makes HIGH count as pressed. This mode needs ezButton version 1.0.6 or newer.

The main functions:

  • Button.setDebounceTime(50): set the debounce time in setup().
  • Button.loop(): call it first in every loop(). It returns at once and never blocks.
  • Button.isPressed() and button.isReleased(): true once per press or release.
  • Button.getState(): the steady, debounced level (HIGH = pressed).
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-module */ #include <ezButton.h> const int BUTTON_MODULE_PIN = D2; // Arduino Nano ESP32 pin connected to the OUT pin of the button module // EXTERNAL_PULLDOWN tells ezButton that the module has a pull-down resistor, // so HIGH means pressed (needs ezButton 1.0.6 or newer) ezButton button(BUTTON_MODULE_PIN, EXTERNAL_PULLDOWN); int lastSteadyState = -1; // used to print the steady state only when it changes void setup() { Serial.begin(9600); button.setDebounceTime(50); // 50 milliseconds debounce, done without delay() } void loop() { button.loop(); // MUST be called first, it never blocks if (button.isPressed()) Serial.println("Event: pressed"); if (button.isReleased()) Serial.println("Event: released"); int steadyState = button.getState(); // debounced state of the button if (steadyState != lastSteadyState) { Serial.print("Steady state: "); Serial.println(steadyState == HIGH ? "PRESSED" : "RELEASED"); lastSteadyState = steadyState; } // Other code can run here at full speed, because nothing above waits }

Detailed Instructions

  • In the Arduino IDE, click the Library Manager icon on the left bar.
  • Type "ezButton" in the search box and find the ezButton library by ArduinoGetStarted.com.
  • Click Install. If an older version is installed, update it to 1.0.6 or newer.
  • Search for ezButton created by ArduinoGetStarted.com and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
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
Arduino Nano ESP32 on COM15
1
  • Upload the code above with the same wiring.
  • Open the Serial Monitor at 9600 baud, then give the key a short tap and then a long press.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Steady state: RELEASED Event: pressed Steady state: PRESSED Event: released Steady state: RELEASED Event: pressed Steady state: PRESSED Event: released Steady state: RELEASED
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

Because button.loop() returns right away, you can put other work in the same loop(), such as reading a sensor or sending data over WiFi, and the key still responds at once. To learn more about debouncing, see the Arduino Nano ESP32 - Button - Debounce tutorial. To make the key switch a light, see Arduino Nano ESP32 - Button LED.

Video Tutorial

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