Arduino Nano 33 IoT - Button Module

This tutorial instructs you how to use Arduino Nano 33 IoT to read a push button module - a handy input when your connected gadget needs one physical control, such as a "send now" key for a sensor node, a reset-Wi-Fi key, or a call button that pushes a message to your phone. In detail, we will learn:

Arduino Nano 33 IoT Button Module

We suggest:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Button Module
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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 and Its Pinout

Button Module Pinout
image source: diyables.io

In short, the button module is a momentary tactile push button that comes with its resistor already on board. A pull-down resistor on the small PCB ties the OUT pin to GND, so OUT reads LOW while nobody touches the cap. Pressing the cap joins OUT to VCC, and the pin goes HIGH; letting go brings it back to LOW. The modules come in a pack of five, each with a different cap color - red, yellow, green, blue and white - so a panel with several keys stays easy to read.

Button Module
Switch type momentary tactile push button with colored cap
Cap colors in the pack red, yellow, green, blue, white
Operating voltage 3.3V to 5V DC
Onboard part pull-down resistor on OUT
Logic active HIGH (pressed = HIGH, released = LOW)
Connector 3-pin 2.54mm header

The header has three pins, labeled in this order:

  • VCC pin: must be connected to the 3.3V pin of the Arduino Nano 33 IoT
  • OUT pin: must be connected to a digital input pin of the Arduino Nano 33 IoT. It gives LOW when the button is up and HIGH when it is pushed down
  • 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!

Why VCC Must Be 3.3V on This Board

When the button is pressed, the OUT pin carries the same voltage that is on VCC. The Arduino Nano 33 IoT is a 3.3V board, and its input pins are not made to accept 5V. If VCC were wired to 5V, every press would push 5V straight into pin D2 and could damage the board. Powering the module from the 3.3V pin keeps the HIGH level at a safe 3.3V, and the module works perfectly at that voltage.

Like every mechanical switch, the button does not close cleanly: its metal contacts bounce for a few milliseconds, so a single press can show up in the code as several quick presses. The second and third examples below deal with this.

Wiring Diagram

The module needs only three jumper wires. VCC takes power from the 3.3V pin (not 5V), GND joins the board ground, and OUT goes to pin D2. No resistor and no other parts are needed.

The wiring diagram between Arduino Nano and 33 IoT Button Module

This image is created using Fritzing. Click to enlarge image

Button Module Arduino Nano 33 IoT
VCC 3.3V
OUT D2
GND GND

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do NOT use internal or external pull-down resistors for these pins

How To Program For Button Module

The first two examples use only the built-in Arduino functions. The third one adds the ezButton library.

  • The first step is to give a name to the Arduino Nano 33 IoT pin that receives the OUT signal:
#define BUTTON_MODULE_PIN 2
  • Set that pin as a plain input in setup(). Do not use INPUT_PULLUP: an internal pull-up would work against the pull-down resistor on the module.
pinMode(BUTTON_MODULE_PIN, INPUT);
  • Read the pin. HIGH means the button is held down, LOW means it is up:
int level = digitalRead(BUTTON_MODULE_PIN);

Arduino Nano 33 IoT Code for Button Module - Raw State

This first sketch does nothing clever: four times per second it reads pin D2 and prints the level, plus a short word about what it means. It is the quickest way to prove that the wiring is right.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-module */ #define BUTTON_MODULE_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the OUT pin of the button module #define PRINT_INTERVAL 250 // time between two printed readings (milliseconds) void setup() { Serial.begin(9600); // plain INPUT: the module already holds OUT at LOW with its own pull-down resistor, // so INPUT_PULLUP must NOT be used here pinMode(BUTTON_MODULE_PIN, INPUT); } void loop() { int level = digitalRead(BUTTON_MODULE_PIN); // HIGH = pressed, LOW = released Serial.print("OUT pin level: "); Serial.print(level); if (level == HIGH) Serial.println(" -> button held down"); else Serial.println(" -> button up"); delay(PRINT_INTERVAL); }

Detailed Instructions

If this is the first time you use the Arduino Nano 33 IoT, see the Getting Started with Arduino Nano 33 IoT tutorial before going further.

  • Wire the components as shown in the diagram, with VCC on 3.3V.
  • Connect the Arduino Nano 33 IoT board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the Arduino Nano 33 IoT board and its COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button to build the sketch and send it to the board.
  • Open the Serial Monitor at 9600 baud.
  • Watch a few lines with your hands off the key, then push and hold it for a second before releasing it.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
OUT pin level: 0 -> button up OUT pin level: 0 -> button up OUT pin level: 1 -> button held down OUT pin level: 1 -> button held down OUT pin level: 1 -> button held down OUT pin level: 1 -> button held down OUT pin level: 0 -> button up OUT pin level: 0 -> button up
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

The level stays at 0 with no flicker while the button is up, thanks to the pull-down on the module. If you see 1 all the time, check that you did not set the pin to INPUT_PULLUP.

Arduino Nano 33 IoT Code for Button Module - Press and Release with delay()

Printing the level again and again is not very useful in a real project; usually you want to act once when the button goes down, or once when it comes up. This sketch remembers the previous state. When a new reading is different, it waits 50 milliseconds, reads again, and only reports the change if it is still there - so the bounce is ignored. The cost is that delay() stops the whole loop during those 50 milliseconds.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-module */ #define BUTTON_MODULE_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the OUT pin of the button module #define SETTLE_TIME 50 // how long to wait for the contacts to stop bouncing (milliseconds) int last_state = LOW; // the module rests at LOW, so start from "released" void setup() { Serial.begin(9600); pinMode(BUTTON_MODULE_PIN, INPUT); // the onboard pull-down keeps the pin LOW when released } void loop() { int reading = digitalRead(BUTTON_MODULE_PIN); if (reading != last_state) { // something changed - it may be a real press or just contact bounce delay(SETTLE_TIME); // the sketch stops here while the contacts settle reading = digitalRead(BUTTON_MODULE_PIN); // look again after the wait if (reading != last_state) { // the change is still there, so it is real last_state = reading; if (reading == HIGH) Serial.println("The button is pressed"); else Serial.println("The button is released"); } } }

Detailed Instructions

  • Keep the same wiring.
  • Paste the code above into the Arduino IDE and click Upload.
  • Press and release the button three times, at different speeds.
  • Each press and each release should appear exactly once in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' 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 33 IoT on COM15
2

If a press is still counted twice, raise SETTLE_TIME a little, for example to 80.

Arduino Nano 33 IoT Code for Button Module - ezButton Library

On a Wi-Fi board the loop often has other jobs, such as keeping a cloud connection alive, so a pause of 50 milliseconds on every press is not ideal. The ezButton library from ArduinoGetStarted.com debounces with a timer instead. Its loop() function only checks the time and returns at once, so it never blocks. Because the module already pulls OUT down, the button object is created with the EXTERNAL_PULLDOWN mode (ezButton 1.0.6 or newer), which tells the library that HIGH means pressed. The sketch prints the press and release events from isPressed() and isReleased(), and the steady level from getState().

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-module */ #include <ezButton.h> #define BUTTON_MODULE_PIN 2 // The Arduino Nano 33 IoT pin D2 connected to the OUT pin of the button module #define DEBOUNCE_TIME 50 // debounce time in milliseconds // EXTERNAL_PULLDOWN tells ezButton that the module has its own pull-down resistor, // so HIGH means "pressed" (needs ezButton 1.0.6 or newer) ezButton button(BUTTON_MODULE_PIN, EXTERNAL_PULLDOWN); int shown_state = -1; // last steady state printed, -1 = nothing printed yet void setup() { Serial.begin(9600); button.setDebounceTime(DEBOUNCE_TIME); } void loop() { button.loop(); // MUST be called first in every loop; it returns at once and never waits if (button.isPressed()) Serial.println("Event: the button is pressed"); if (button.isReleased()) Serial.println("Event: the button is released"); // getState() gives the debounced steady level; print it only when it changes int state = button.getState(); if (state != shown_state) { shown_state = state; Serial.print("Steady state: "); Serial.println(state == HIGH ? "PRESSED" : "RELEASED"); } // other work can run here without being slowed down by the button }

Detailed Instructions

  • Keep the same wiring.
  • Click the Library Manager icon in the left bar of the Arduino IDE.
  • Search for ezButton and find the button library by ArduinoGetStarted.
  • Press Install. If an older version is already there, 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 33 IoT
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 33 IoT on COM15
1
  • Paste the code above into the Arduino IDE and click Upload.
  • Press the button, hold it for a second, then release it. Do this twice.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' on 'COM15')
New Line
9600 baud
Steady state: RELEASED Event: the button is pressed Steady state: PRESSED Event: the button is released Steady state: RELEASED Event: the button is pressed Steady state: PRESSED Event: the button is released Steady state: RELEASED
Ln 11, Col 1
Arduino Nano 33 IoT on COM15
2

The first line comes from the start-up, before any press. Any other code you put at the end of loop() keeps running at full speed, even while the button is bouncing.

Additional Knowledge

How is the button module different from a bare 4-pin push button on the Arduino Nano 33 IoT?

Bare 4-pin Button Button Module
Pins four legs joined in pairs, easy to wire the wrong pair three labeled pins - VCC, OUT, GND
Resistor you add a pull-up or pull-down, or use INPUT_PULLUP pull-down already on the module
pinMode() usually INPUT_PULLUP INPUT
Pressed level LOW with INPUT_PULLUP (inverted logic) HIGH (not inverted)
Power wire not needed VCC must go to 3.3V on this board

It is evident that the module is quicker to wire and its logic is easier to read in code, while the bare button is still worth knowing because it shows what the pull-down resistor on the module is doing for you.

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!