This tutorial instructs you how to use Arduino Nano to read a button module, a push button that sits on a tiny PCB with its pull-down resistor already in place. You plug three wires into the Nano and the input is clean right away: no extra resistor, no guessing which of four legs to use. In detail, we will learn:
How to connect Arduino Nano to the button module through its VCC, OUT and GND pins
How to program Arduino Nano to print the raw level of the OUT pin
How to program Arduino Nano to catch the moment the button goes down and comes back up, with a short delay as debounce
How to program Arduino Nano to do the same job with the ezButton library, without ever pausing the loop
We suggest:
Utilizing a bare 4-pin push button if you want to see how pull-up and pull-down resistors work by yourself.
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
image source: diyables.io
A momentary tactile switch with a colored cap is soldered onto a small PCB, together with a pull-down resistor. While nobody touches the cap, the resistor holds the OUT pin at 0V. Push the cap and the switch links OUT straight to VCC. Let go and OUT falls back to 0V. One thing to keep in mind: metal contacts do not close cleanly. For a few milliseconds they bounce, so a single push can look like a quick burst of presses to a fast board like the Nano. The code examples below show two ways to filter that out.
Button Module
Switch type
momentary tactile push button, colored cap
Colors in the pack
red, yellow, green, blue, white (5 pieces)
Operating voltage
3.3V to 5V DC
Resistor on board
pull-down on the OUT line
Output logic
active HIGH (pressed → OUT = VCC, released → OUT = 0V)
Header
3-pin, 2.54mm pitch
The header is labeled VCC, OUT and GND, in this order:
VCC pin: must be connected to the 3V3 or the 5V pin of Arduino Nano. When pressed, the OUT pin gives out this same voltage, and the Nano's inputs read both levels as HIGH
OUT pin: must be connected to a digital input pin of Arduino Nano. It reads HIGH while the cap is held down and LOW the rest of the time
GND pin: must be connected to GND (0V)
※ NOTE THAT:
The arrangement of pins on a module may differ from one manufacturer to another. It is essential to always refer to the labels printed on the module when using it. Take a close look!
Wiring Diagram
Power the module from the 3V3 pin of the Nano (the 5V pin works as well) and run its OUT pin to D2. All three code examples in this tutorial use this one wiring.
This image is created using Fritzing. Click to enlarge image
The first step is to name the Arduino Nano pin that receives the OUT signal:
#define BUTTON_MODULE_PIN 2
Set that pin as a plain input in setup() with pinMode(). Do not use INPUT_PULLUP here: the internal pull-up would work against the pull-down resistor on the module.
pinMode(BUTTON_MODULE_PIN, INPUT);
Get the current level with digitalRead(). HIGH means the cap is pushed down, LOW means it is up:
int level = digitalRead(BUTTON_MODULE_PIN);
Arduino Nano Code for Button Module - Raw State
This sketch reads the OUT pin five times a second and prints what it sees: 1 for pressed, 0 for released. Nothing is filtered, so it is the best way to check the wiring first.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-module */#define BUTTON_MODULE_PIN 2 // The Arduino Nano pin wired to the OUT pin of the button module// The setup function runs once on reset or power-upvoidsetup() {Serial.begin(9600); // start the Serial Monitor at 9600 baudpinMode(BUTTON_MODULE_PIN, INPUT); // plain INPUT: the module already has its own pull-down resistor}// The loop function repeats indefinitelyvoidloop() {int level = digitalRead(BUTTON_MODULE_PIN); // HIGH (1) = pressed, LOW (0) = releasedSerial.print("OUT pin level: ");Serial.println(level);delay(200); // slow the printing down so the Serial Monitor is easy to read}
Connect the Arduino Nano board to your computer using a USB cable.
Open Arduino IDE on your computer.
Pick the Arduino Nano board and the matching COM port.
Paste the code above into a new sketch.
Press the Upload button to send it to the Nano.
Open the Serial Monitor at 9600 baud.
Leave the button alone for a moment, then hold it down for about a second, then release it.
The printed level follows your finger:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
OUT pin level: 0
OUT pin level: 0
OUT pin level: 0
OUT pin level: 1
OUT pin level: 1
OUT pin level: 1
OUT pin level: 1
OUT pin level: 1
OUT pin level: 0
OUT pin level: 0
Ln 11, Col 1
Arduino Nano on COM15
2
If the value never changes, check the OUT wire first, then make sure VCC really goes to 3V3 or 5V and not to an unused pin.
Arduino Nano Code for Button Module - Debounce with Delay
Most projects do not care about the level itself. They care about the moment it changes, for example a start/stop key for a timer or a "next mode" key for a lamp. This sketch keeps the last confirmed level. When a new reading is different, it waits 50 milliseconds for the contacts to settle, reads the pin again, and reports an event only if the change is still there. Note that delay() stops the whole loop during those 50 milliseconds, so nothing else can run while it waits.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-module */#define BUTTON_MODULE_PIN 2 // The Arduino Nano pin wired to the OUT pin of the button module#define DEBOUNCE_DELAY 50 // How long to wait (in milliseconds) for the contacts to settleint lastState = LOW; // the module idles LOW, because of its pull-down resistor// The setup function runs once on reset or power-upvoidsetup() {Serial.begin(9600);pinMode(BUTTON_MODULE_PIN, INPUT); // no INPUT_PULLUP: it would fight the module's pull-down}// The loop function repeats indefinitelyvoidloop() {int reading = digitalRead(BUTTON_MODULE_PIN);if (reading != lastState) { // the level seems to have changeddelay(DEBOUNCE_DELAY); // wait until the bouncing is over (this blocks the loop) reading = digitalRead(BUTTON_MODULE_PIN); // read once more to confirmif (reading != lastState) { // the change is real, not a bounceif (reading == HIGH)Serial.println("The button is pressed");elseSerial.println("The button is released"); lastState = reading; // remember the confirmed level } }}
Detailed Instructions
Do not change the wiring.
Paste this sketch into Arduino IDE, replacing the old one.
Click Upload.
Press and release the button three times, at your own pace.
Each push shows up once, and so does each release:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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 on COM15
2
Arduino Nano Code for Button Module - ezButton Library
The delay-based method is fine for a small test, but a frozen loop is a problem when the Nano also has to blink an LED, read a sensor or count time. The ezButton library handles debouncing in the background by checking the clock instead of waiting. Its loop() function returns at once, every time. Because the module has its own pull-down resistor, create the button object with the EXTERNAL_PULLDOWN mode, so that HIGH is read as pressed (this mode needs ezButton version 1.0.6 or newer).
The sketch below uses three functions of the library:
button.getState(): the debounced level of the button, HIGH or LOW
button.isPressed(): true once, right after the button goes down
button.isReleased(): true once, right after the button comes up
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-module */#include <ezButton.h>#define BUTTON_MODULE_PIN 2 // The Arduino Nano pin wired to the OUT pin of the button module// EXTERNAL_PULLDOWN tells the library that the module has its own pull-down resistor,// so HIGH means pressed and LOW means released (needs ezButton 1.0.6 or newer)ezButtonbutton(BUTTON_MODULE_PIN, EXTERNAL_PULLDOWN);int lastSteadyState = -1; // used to print the steady state only when it changes// The setup function runs once on reset or power-upvoidsetup() {Serial.begin(9600);button.setDebounceTime(50); // ignore any change shorter than 50 milliseconds}// The loop function repeats indefinitelyvoidloop() {button.loop(); // MUST be called first; it never blocks the loopif (button.isPressed())Serial.println("The button is pressed");if (button.isReleased())Serial.println("The button is released");int steadyState = button.getState(); // the debounced level: HIGH = pressed, LOW = releasedif (steadyState != lastSteadyState) {Serial.print("Steady state: ");Serial.println(steadyState == HIGH ? "PRESSED" : "RELEASED"); lastSteadyState = steadyState; }// other code can run here without waiting for the button}
Detailed Instructions
Keep the same wiring.
In Arduino IDE, click the Libraries icon on the left bar.
Type "ezButton" into the search box and find the library by ArduinoGetStarted.com.
Click Install. If an older version is already installed, update it to the newest one.
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
Library Manager
ezButton
Type:
All
Topic:
All
ezButtonby 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
voidsetup() {
Output
Serial Monitor
Ln 1, Col 1
Arduino Nano on COM15
1
Paste the sketch above into Arduino IDE.
Click Upload.
Push the button, hold it for a second, and let it go. Do this twice.
The Serial Monitor shows the starting state, then each event followed by the new steady state:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
Steady state: RELEASED
The button is pressed
Steady state: PRESSED
The button is released
Steady state: RELEASED
The button is pressed
Steady state: PRESSED
The button is released
Steady state: RELEASED
It is evident that the button module is the quicker choice: the logic is not inverted and the input never floats. The bare push button is still a good way to learn what a pull-up or pull-down resistor really does. If you need several keys at once, see Arduino Nano - Multiple Button.
Project Ideas
A doorbell key that plays a tune on a buzzer
Start and stop keys for a stopwatch shown on an LCD
A mode key that steps a lamp through OFF, dim and bright
A small game controller with the five colored modules as action keys
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!