Arduino - Button Module

In this tutorial, we are going to learn:

Arduino Button Module

About Button Module

In short, a button module is a push button with its resistor already on board. The tactile switch, a colored cap and a pull-down resistor sit together on a small PCB. While nobody touches the cap, the resistor holds the OUT pin at LOW. Push the cap and the switch links OUT to VCC, so the pin goes HIGH. Release it and OUT falls back to LOW. Arduino just has to read that one pin.

One thing to keep in mind: a switch is a piece of metal hitting metal. For a few milliseconds after each push or release, the contacts bounce, so one press can look like several quick presses to a fast Arduino. The second and third examples below show how to filter this out.

The module comes in a pack of five, one cap of each color: red, yellow, green, blue and white. They are wired the same way inside, so you can pick colors by job, for example green for "start" and red for "stop".

Button Module
Switch Type momentary tactile push button
Cap Colors red, yellow, green, blue, white
Operating Voltage 3.3V to 5V DC
On-board Resistor pull-down on the OUT pin
Output Logic LOW = released, HIGH = pressed
Header 3-pin, 2.54mm
Button Module Pinout
image source: diyables.io

From left to right, the header is labeled VCC, OUT, GND.

  • VCC pin: needs to be connected to the 3.3V or the 5V pin of the Arduino Uno. When the button is pressed, this voltage appears on OUT.
  • OUT pin: needs to be connected to an Arduino digital input pin. It reads LOW at rest and HIGH while the cap is held down.
  • GND pin: needs to be connected to GND (0V)

Why pick the module over the bare 4-leg button from the Arduino - Button tutorial?

  • The bare button has four legs joined in pairs, and it is easy to wire the wrong pair. The module has three printed labels instead.
  • The bare button floats when it is not pressed, so you must add a pull-up or pull-down resistor, or use INPUT_PULLUP. The module brings its own pull-down.
  • With INPUT_PULLUP, a bare button reads LOW when pressed, which feels backward. The module is not inverted: pressed is HIGH.

Wiring Diagram

Run three wires: module VCC to the Arduino 3.3V pin, module OUT to digital pin 7, and module GND to an Arduino GND pin. The 5V pin works for VCC too, so you can use either one.

Arduino Button Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Pin 7 is only a choice. Any digital pin from 2 to 13 can read the module; just change the pin number at the top of the code to match.

How To Program For Button Module

  • Define the Arduino pin that the OUT wire goes to:
#define BUTTON_MODULE_PIN 7
  • Configure that pin as a plain input with pinMode(). Do not use INPUT_PULLUP here. The internal pull-up would fight the module's own pull-down resistor.
pinMode(BUTTON_MODULE_PIN, INPUT);
  • Read the level with digitalRead(). HIGH means the cap is pushed down:
int state = digitalRead(BUTTON_MODULE_PIN); if (state == HIGH) { // the button is being held }

Arduino Code - Read Button Module State

The first sketch does nothing clever. It reads pin 7 five times per second and prints the number it gets: 1 while the button is held, 0 when it is free. It is a quick way to prove that the wiring is right before you build anything on top of it.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-module */ #define BUTTON_MODULE_PIN 7 // Arduino pin connected to the OUT pin of the button module void setup() { Serial.begin(9600); // open the Serial Monitor connection pinMode(BUTTON_MODULE_PIN, INPUT); // plain INPUT: the module already has a pull-down resistor } void loop() { int state = digitalRead(BUTTON_MODULE_PIN); // HIGH (1) = pressed, LOW (0) = released Serial.print("Button state: "); Serial.println(state); delay(200); // slow down the printing so it is easy to read }

Quick Steps

  • Connect Arduino to PC via USB cable.
  • Plug the three wires between the button module and Arduino, following the wiring diagram.
  • Open Arduino IDE, select the right board and port.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor and set the baud rate to 9600.
  • Hold the button down for about one second, then let it go.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Button state: 0 Button state: 0 Button state: 1 Button state: 1 Button state: 1 Button state: 1 Button state: 1 Button state: 0 Button state: 0
Ln 11, Col 1
Arduino Uno on COM15
2

If the value stays at 0 no matter what you do, check that OUT really goes to pin 7 and that VCC is on the 3.3V or 5V pin, not on a free header hole.

Arduino Code - Button Module Press and Release with delay()

Most projects do not care about the level itself. A doorbell, a "next song" button or a start/stop key only needs to know the moment the button goes down or comes up. This sketch remembers the last state. When a new reading is different, it waits 50 ms for the bounce to settle, reads the pin again, and only then prints a message.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-module */ #define BUTTON_MODULE_PIN 7 // Arduino pin connected to the OUT pin of the button module #define DEBOUNCE_DELAY 50 // time (ms) to wait until the contacts stop bouncing int lastState = LOW; // the module outputs LOW while the button is released void setup() { Serial.begin(9600); pinMode(BUTTON_MODULE_PIN, INPUT); // no INPUT_PULLUP: it would fight the on-board pull-down } void loop() { int reading = digitalRead(BUTTON_MODULE_PIN); if (reading != lastState) { // the pin changed - maybe a real press, maybe a bounce delay(DEBOUNCE_DELAY); // wait (this blocks the loop for 50 ms) reading = digitalRead(BUTTON_MODULE_PIN); // read again 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 new state } } }

Quick Steps

  • Keep the wiring from the first example.
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor (baud rate 9600).
  • Press and release the button three times.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' 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 on COM15
2

Each press now gives exactly one "pressed" line and one "released" line. The cost is that delay() freezes the whole loop for 50 ms every time the pin changes, so nothing else can run during that wait.

For a deeper look at bouncing and a millis() version of this idea, see Arduino - Button - Debounce.

Arduino Code - Button Module with ezButton Library

The ezButton library does the same debounce job without ever stopping the loop. Each time button.loop() runs, it takes a quick look at the pin and checks the clock, then returns at once. This leaves Arduino free to blink LEDs, read sensors or drive a motor at the same time, which matters in something like a game controller where you cannot afford to pause.

Because the module pulls OUT down, the button object is created with EXTERNAL_PULLDOWN. This tells the library that HIGH means pressed. This option needs ezButton version 1.0.6 or newer.

The functions used in the sketch:

  • button.setDebounceTime(50): ignore any change shorter than 50 ms.
  • button.loop(): must be called first in every loop().
  • button.isPressed() / button.isReleased(): true only once, right when the event happens.
  • button.getState(): the steady, debounced level (HIGH = pressed).
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-module */ #include <ezButton.h> #define BUTTON_MODULE_PIN 7 // Arduino pin connected to the OUT pin of the button module // EXTERNAL_PULLDOWN tells the library that the module has its own pull-down, // so HIGH means pressed and LOW means released ezButton button(BUTTON_MODULE_PIN, EXTERNAL_PULLDOWN); int lastSteadyState = -1; // used to print the state only when it changes void setup() { Serial.begin(9600); button.setDebounceTime(50); // debounce time: 50 milliseconds } void loop() { button.loop(); // MUST be called first. It never blocks the loop. if (button.isPressed()) Serial.println("The button is pressed"); if (button.isReleased()) Serial.println("The button is released"); int state = button.getState(); // steady (debounced) state: HIGH = pressed if (state != lastSteadyState) { Serial.print("Steady state: "); Serial.println(state == HIGH ? "PRESSED" : "RELEASED"); lastSteadyState = state; } // other code can run here without waiting for the button }

Quick Steps

  • Keep the same wiring.
  • In Arduino IDE, click the Libraries icon on the left bar.
  • Search "ezButton" and find the library by ArduinoGetStarted.com.
  • Click the Install button. 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 Uno
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 Uno on COM15
1
  • Copy the above code and paste it to Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor (baud rate 9600).
  • Press the button, hold it a moment, then release it. Do it twice.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' 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
Ln 11, Col 1
Arduino Uno on COM15
2

※ NOTE THAT:

If every line comes out backward (pressed when you let go), the library is treating the pin as pull-up. Check that the object is created with EXTERNAL_PULLDOWN and that your ezButton version is 1.0.6 or newer.

Next, you can use the module to switch something real, as in Arduino - Button - LED.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES