Arduino Nano 33 IoT - Button

The button is a simple part used in many Arduino Nano 33 IoT projects. It may look easy, but because of its moving parts and design, it can be tricky. Beginners might have some problems with it. This lesson is made to help beginners. Let's start!

※ NOTE THAT:

Before we talk about the button, we want to mention two common mistakes that beginners usually encounter:

1. Floating Input Issue:

  • Problem: When you connect a button to an Arduino Nano 33 IoT input pin, the pin's reading is random and does not match the actual button press.
  • Reason: The button is not connected with a pull-down resistor or a pull-up resistor.
  • Fix: Use a pull-down or pull-up resistor on the input pin. More details will be provided later.

2. Chattering:

  • Problem: The Arduino Nano 33 IoT reads the button by noticing a change from HIGH to LOW or LOW to HIGH. However, when you press the button only once, the code sometimes thinks it was pressed many times.
  • Reason: Due to the button's design, one press can make the input pin quickly switch between LOW and HIGH several times.
  • Fix: Use a debounce method. More information is available in the Arduino Nano 33 IoT - Button - Debounce tutorial.

Chattering only affects certain projects where you need to count the exact number of button presses; in other cases, it is not a serious problem.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Push Button Module
1×Breadboard
1×Jumper Wires
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

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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

A push button, also known as a pushbutton, tactile button, or momentary switch, is a switch that closes when you press and hold it, and opens when you let go. There are many kinds of push buttons, usually divided into two groups:

  • PCB-mount push buttons (suitable for breadboard mounting)
  • Panel-mount push buttons
Arduino Nano 33 IoT Push button

Button Pinout

A PCB-mount button usually has four pins that are internally connected in pairs.

Button Pinout

We only need two of the four pins, and they should not be directly connected. So, there are four different ways to connect the button (see the image below).

How To Use Button

Due to symmetry, the four options reduce to two. In the rest of this guide, we will use two pins: Pin A and Pin B, which are not connected to each other.

Why does the button have four pins when we only need two?

The button is pressed by users. To keep it secure on the printed circuit board (PCB), it uses four pins to hold it in place when pressed.

A panel-mount button usually has two pins.

two-pin push button Pinout
image source: diyables.io

The push button module includes an built-in pull-down resistor, which ensures that the output remains LOW when the button is not pressed. It has three pins:

  • GND: Attach this pin to the ground.
  • VCC: Connect this pin to a 3.3V power source.
  • OUT: Connect this pin to a digital input on your Arduino Nano 33 IoT.

In this setup, the device gives a LOW signal when the button is not pressed and a HIGH signal when it is pressed.

How Button Works

  • When you press the button, pin A connects to pin B.
  • When you do not press the button, pin A does not connect to pin B.
How Button Works

Arduino Nano 33 IoT - Button

One button lead is connected to a digital input on the Arduino Nano 33 IoT. The other lead is connected to either VCC or GND. In the Arduino code, by checking the digital input, we can tell if the button is pressed or not.

Input State and Pressing State

The connection between the input pin's condition and whether the button is pressed depends on how you hook up the button to the Arduino Nano 33 IoT and how you set up the Arduino's pin. There are two ways to use a button with the Arduino Nano 33 IoT:

  • Method 1:
    • Connect one side of the button to a digital input pin on the Arduino Nano 33 IoT and the other side to VCC (power).
    • You must use a pull-down resistor.
    • When the button is pressed, the pin reads as HIGH (active). When the button is not pressed, the pin reads as LOW (inactive).
  • Method 2:
    • Connect one side of the button to a digital input pin on the Arduino Nano 33 IoT and the other side to GND (ground).
    • You must use a pull-up resistor.
    • When the button is pressed, the pin reads as LOW (active). When the button is not pressed, the pin reads as HIGH (inactive).

    ※ NOTE THAT:

    If you don't use a pull-down or pull-up resistor, the input pin can randomly be HIGH or LOW when the button is not pressed. This is called the floating input problem and may cause the system to work incorrectly.

    This guide is easy for beginners. We suggest using the built-in pull-up resistor on the Arduino Nano 33 IoT pin. You don't need any extra resistor. This saves hardware and makes the wiring diagram simpler.

Wiring Diagram between Button and Arduino Nano 33 IoT

  • Wiring diagram for connecting the Arduino Nano 33 IoT to a PCB-mounted button.
The wiring diagram between Arduino Nano and 33 IoT Button

This image is created using Fritzing. Click to enlarge image

  • Wiring Diagram for Connecting the Arduino Nano 33 IoT to a Panel Button
The wiring diagram between Arduino Nano and 33 IoT two-pin push button

This image is created using Fritzing. Click to enlarge image

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

  • This code sets up a pin on the Arduino Nano 33 IoT to use its built-in pull-up resistor. For instance, it sets pin D2 like this:
pinMode(2, INPUT_PULLUP); // Set D2 as an input and enable its internal pull-up resistor
  • This function checks if the input pin is on or off using the digitalRead() function.
int button_state = digitalRead(BUTTON_PIN);

※ NOTE THAT:

There are two common ways to use this:

  • First way: If the input is HIGH, do one action; if the input is LOW, do another action.
  • Second way: If the input changes from LOW to HIGH, do one action; if it changes from HIGH to LOW, do another action.

Which way you choose depends on what you need. For example, when using a button to control an LED:

  • If you want the LED to turn ON when the button is pressed and turn OFF when it is not pressed, use the first way.
  • If you want the LED to switch between ON and OFF every time you press the button, use the second way.

This sample code shows how to check when the state changes from LOW to HIGH.

#define BUTTON_PIN D2 // Button connected to D2 int prev_state = HIGH; // Holds the previous button state int button_state; // Stores the current button state void setup() { Serial.begin(9600); // Configure the button pin with the internal pull-up resistor enabled pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // Obtain the current state of the button button_state = digitalRead(BUTTON_PIN); if(prev_state == LOW && button_state == HIGH) Serial.println("The state changed from LOW to HIGH"); // Update the previous state for the next cycle prev_state = button_state; }

Arduino Nano 33 IoT Code

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the following code and paste it into the Arduino IDE.
#define BUTTON_PIN 2 // Arduino Nano 33 IoT pin D2 pin connected to button void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the button pin as an pull-up input (HIGH when the switch is open and LOW when the switch is closed) pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: int button_state = digitalRead(BUTTON_PIN); // print out the button's state Serial.println(button_state); }
  • Click the Upload button in the Arduino IDE to build your program and send it to your Arduino Nano 33 IoT board.
How to upload Arduino Nano 33 IoT code on Arduino IDE
  • Open the Serial Monitor in the Arduino software.
How to open serial monitor on Arduino IDE
  • Press and let go of the button several times.
  • Look at the output on the Serial Monitor. It should look like what is shown below.
COM6
Send
1 1 1 0 0 0 0 0 0 1 1 1
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

1 means high, and 0 means low.

Line-by-line Code Explanation

The Arduino Nano 33 IoT code above shows an explanation for each line. Please look at the comments in the code!

Modifying Arduino Nano 33 IoT Code

Let's change the code so it can see when something is pressed and let go.

Detailed Instructions

  • Change the code as shown below.
/* * 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 */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT pin D2 pin connected to button int prev_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the button pin as an pull-up input (HIGH when the switch is open and LOW when the switch is closed) pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if (prev_state == HIGH && button_state == LOW) Serial.println("The button is pressed"); else if (prev_state == LOW && button_state == HIGH) Serial.println("The button is released"); // save the the last state prev_state = button_state; }
  • Click the Upload button on the Arduino IDE to compile and send the code to the Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
  • Press and then release the button.
  • Look at the result on the Serial Monitor. It should look like the image below.
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The Serial Monitor might show several press and release messages even if you only pressed and released the button once. This is normal and is known as the chattering phenomenon. In some projects, you need a way to remove this extra information. You can find out more in the Arduino Nano 33 IoT - Button Debounce tutorial.

To keep things simple for beginners, especially when using several buttons, we made a library called ezButton. You can learn more about it here: https://arduinogetstarted.com/tutorials/arduino-button-library.

When using the button module, set the pin as an input by using pinMode(BUTTON_PIN, INPUT). The module gives a LOW signal when not pressed and a HIGH signal when pressed.

Video Tutorial

Additional Knowledge

When should I use a pull-down or pull-up resistor, and when should I not?

  • SHOULD: When a sensor has two conditions—closed or open—you need to add a pull-up or pull-down resistor. This resistor helps change the conditions into two clear levels: LOW and HIGH. Examples include push-buttons, switches, and magnetic door sensors.
  • SHOULD NOT: When the sensor already gives two clear voltage levels (LOW and HIGH), you do not need a pull-up or pull-down resistor. Examples include motion sensors and touch sensors.

Language References

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