In this guide, we will learn how to use a push button module with the Arduino UNO R4. A doorbell, a start/stop key for a timer, a "next mode" key for a lamp: all of these begin with one button and one input pin. The module makes that first step easy, because its pins are labeled and its resistor is already soldered on. In detail, we will learn:
Which job each of the VCC, OUT and GND pins does, and why the module needs no extra resistor.
How to hook the button module up to the Arduino UNO R4 WiFi or Minima.
How to print the live HIGH/LOW level of the button to the Serial Monitor.
How to catch one clean "pressed" and one clean "released" message per click, first with a short delay, then with the ezButton library.
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
Picture a small tactile switch with a colored cap, sitting on its own little PCB next to a 3-pin header. Under the board there is a pull-down resistor, so the OUT pin always has a clear level: LOW while nobody touches the cap, HIGH while your finger holds it down. In short, it is a push button with its resistor already on board. The DIYables pack holds five of them in red, yellow, green, blue and white, which is handy when each key in a project needs its own color.
image source: diyables.io
The header pins, in the order printed on the board:
VCC pin: connect to the 3.3V or the 5V pin of the Arduino UNO R4.
OUT pin: connect to a digital input pin. It reads HIGH when the button is pressed and LOW when it is let go.
GND pin: connect to GND (0V).
Button Module
Switch type
momentary tactile push button with cap
Header
3 pins, 2.54mm pitch (VCC, OUT, GND)
Operating voltage
3.3V to 5V DC
On-board resistor
pull-down on OUT
Output when released
LOW
Output when pressed
HIGH (the same voltage as VCC)
Cap colors
red, yellow, green, blue, white
When pressed, the OUT pin gives out the VCC voltage. The Arduino UNO R4 pins work at 5V, so feeding the module from either the 3.3V pin or the 5V pin is safe here.
One thing every mechanical button does: its metal contacts bounce for a few milliseconds when they close or open. For that short time the pin can flip HIGH-LOW-HIGH, so a single click may look like several clicks to a fast program. Examples 2 and 3 below show two ways to clean this up.
You don't add a resistor or use INPUT_PULLUP. The pull-down is built in, so plain INPUT is the right mode.
You don't have to guess which two of the four legs are joined inside. The pins have names.
The logic is not flipped: pressed is HIGH, released is LOW.
Wiring Diagram
Three jumper wires do it, and no breadboard is required. Run VCC to the 3.3V header pin (the 5V pin also works), OUT to digital pin 7, and GND to any GND pin of the Arduino UNO R4.
Button Module Pin
Arduino UNO R4 Pin
VCC
3.3V (or 5V)
OUT
7
GND
GND
This image is created using Fritzing. Click to enlarge image
For the first two sketches, the built-in Arduino functions are all we need.
Give a name to the pin wired to OUT:
constint BUTTON_MODULE_PIN = 7;
Set it as an input. Leave out the internal pull-up, because it would work against the module's pull-down:
pinMode(BUTTON_MODULE_PIN, INPUT);
Read the level, where HIGH means the cap is pushed down:
int level = digitalRead(BUTTON_MODULE_PIN);
Arduino UNO R4 Code - Read Button Module State
Let's start by simply watching the OUT pin. Five times a second, the sketch reads the level and prints it as a number, together with a word that says what the number means.
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-module */constint BUTTON_MODULE_PIN = 7; // Arduino UNO R4 pin connected to the OUT pin of the button modulevoidsetup() {Serial.begin(9600);pinMode(BUTTON_MODULE_PIN, INPUT); // plain INPUT: the module already has its own pull-down resistor}voidloop() {int level = digitalRead(BUTTON_MODULE_PIN); // HIGH = pressed, LOW = releasedSerial.print("OUT level: ");Serial.print(level);if (level == HIGH)Serial.println(" -> pressed");elseSerial.println(" -> released");delay(200); // slow down the printing so it is easy to read}
Connect the button module to the Arduino UNO R4 as the wiring diagram shows.
Plug the Arduino UNO R4 into your computer with a USB Type-C cable.
Launch the Arduino IDE on your computer.
Select the appropriate Arduino UNO R4 board (e.g., Arduino UNO R4 WiFi or Arduino UNO R4 Minima) and COM port.
Paste the code above into the Arduino IDE.
Click the Upload button.
Open the Serial Monitor at 9600 baud.
First let the readings run untouched, then keep the cap pressed for one second and lift your finger.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
OUT level: 0 -> released
OUT level: 0 -> released
OUT level: 0 -> released
OUT level: 1 -> pressed
OUT level: 1 -> pressed
OUT level: 1 -> pressed
OUT level: 1 -> pressed
OUT level: 1 -> pressed
OUT level: 0 -> released
OUT level: 0 -> released
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
This sketch shows the state, but it does not tell you when a click starts or ends. A doorbell or a start/stop key needs that moment, not a stream of ones and zeros.
Arduino UNO R4 Code - Pressed and Released Events with delay()
Here the sketch keeps the last confirmed level in a variable. When a new reading is different, it pauses for 50 milliseconds so the bouncing can end, and then reads the pin again. Only if the new level is still there does it print one message. Keep in mind that delay() stops the whole loop during those 50 milliseconds, so nothing else can run in that time.
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-module */constint BUTTON_MODULE_PIN = 7; // Arduino UNO R4 pin connected to the OUT pin of the button moduleconstint DEBOUNCE_DELAY = 50; // milliseconds to wait for the contacts to settleint lastState = LOW; // the module gives LOW while the button is not pressedvoidsetup() {Serial.begin(9600);pinMode(BUTTON_MODULE_PIN, INPUT); // no INPUT_PULLUP: the module has a pull-down on board}voidloop() {int reading = digitalRead(BUTTON_MODULE_PIN);if (reading != lastState) { // the pin changed, maybe a real press, maybe a bouncedelay(DEBOUNCE_DELAY); // wait until the bouncing is over (this blocks the loop) reading = digitalRead(BUTTON_MODULE_PIN); // read again to confirmif (reading != lastState) { // the change is still there, so it is realif (reading == HIGH)Serial.println("The button is pressed");elseSerial.println("The button is released"); lastState = reading; // remember the confirmed state } }}
Detailed Instructions
The wiring stays as it is.
Paste this code into a new sketch and click the Upload button.
Give the button three short clicks while you look at the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' 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 Uno R4 WiFi on COM15
2
Three clicks, three pairs of lines. Without the 50 ms check, a bouncy button could have printed "pressed" two or three times for one click.
Arduino UNO R4 Code - Button Module with ezButton Library
A blocking pause is fine for a tiny test, but a real project, such as a game controller that also reads a joystick or drives a display, should not freeze. The ezButton library debounces by checking the time with every pass of loop(), so it never waits. Because our module has a pull-down, we create the button with EXTERNAL_PULLDOWN; then HIGH counts as pressed. This option needs ezButton version 1.0.6 or newer.
The library functions used in the sketch:
button.setDebounceTime(50); sets the settle time to 50 ms (in setup).
button.loop(); updates the button. Call it first in every pass of loop().
button.isPressed() and button.isReleased() are true once, at the moment of the click or the release.
button.getState() returns the steady level: HIGH while held, LOW while released.
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-module */#include <ezButton.h>constint BUTTON_MODULE_PIN = 7; // Arduino UNO R4 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 version 1.0.6 or newer)ezButtonbutton(BUTTON_MODULE_PIN, EXTERNAL_PULLDOWN);voidsetup() {Serial.begin(9600);button.setDebounceTime(50); // ignore any change shorter than 50 milliseconds}voidloop() {button.loop(); // MUST be called first in loop(); it never blocksif (button.isPressed())Serial.println("The button is pressed");if (button.isReleased())Serial.println("The button is released");// getState() gives the steady (debounced) state at any timestaticint lastShownState = -1;int state = button.getState();if (state != lastShownState) {Serial.print("Steady state: ");Serial.println(state == HIGH ? "HIGH (held down)" : "LOW (not touched)"); lastShownState = state; }}
Detailed Instructions
Go to the Libraries icon on the left side of the Arduino IDE.
Type "ezButton" in the search box and find the button library by ArduinoGetStarted.com.
Click the Install button. If it is already installed, make sure the version is 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 Uno R4 WiFi
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 Uno R4 WiFi on COM15
1
Paste the code above into the Arduino IDE and click Upload.
Press and hold the button for a second, then let go. Do it twice.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Steady state: LOW (not touched)
The button is pressed
Steady state: HIGH (held down)
The button is released
Steady state: LOW (not touched)
The button is pressed
Steady state: HIGH (held down)
The button is released
Steady state: LOW (not touched)
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
The first line comes right after upload, before you touch anything. After that, each click gives one "pressed" and one "released" event, and the steady state follows them.
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!