Arduino Nano - Button

This tutorial instructs you how to use Arduino Nano with button. In detail, we will learn:

The button is referred to as a pushbutton, tactile button or momentary switch. It is a fundamental component and is frequently used in Arduino projects. It is straightforward to use. However, it can be confusing for beginners because of mechanical, physical aspects and the various ways it can be used. This tutorial makes it easier for beginners.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board 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

Beginners typically encounter two common difficulties when using a button:

1. Floating input problem:.

  • Symptom: The input pin's read value does not correspond with the button's pressed state.
  • Cause: The input pin does not have a pull-up or pull-down resistor in use.
  • Solution: Use a pull-up or pull-down resistor, and the steps to do so will be explained in this tutorial.

2. Chattering phenomenon:.

  • Symptom: Although the button is pressed only once, the Arduino code detects multiple presses.
  • Cause: The mechanical and physical problems are causing the button (or switch) state to rapidly toggle between LOW and HIGH multiple times.
  • Solution: To address this issue, debounce techniques will be covered in the Arduino Nano- Button - Debounce tutorial

Only applications that requires precise detection of the number of presses should take this into consideration.

※ NOTE THAT:

Do not confuse the button with the following:

The Button Pinout

The push button, also referred to as a pushbutton, tactile button, or momentary switch, is a type of switch that closes when the button is pressed and held, and opens when released. Various types of push buttons exist, which can be broadly categorized into two groups:

  • PCB-mount push button (suitable for mounting on a breadboard)
  • Panel-mount push button
Push button

A PCB-mount button typically have four pins.

Button pinout

Nevertheless, these pins are linked together in pairs. Consequently, only two of the four pins need to be used, which are not connected internally.

There are four possible ways to connect to the button, two of which are symmetrical (as seen in the image).

How To Use Button

? Why is it that only two pins of a button are utilized, when it has four?

⇒ To ensure that it is securely mounted on the PCB (printed circuit board) and can withstand any pressing force.

A panel-mount button usually have two pins.

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

How It Works

  • When the button is not pressed, pin A and pin B are not connected.
  • However, when the button is pressed, pin A and pin B become connected.
How Button Works

Arduino Nano - Button

One button's pin is connected to either VCC or GND. The other pin of the same button is connected to a pin on an Arduino Nano board. By examining the state of an Arduino Nano pin set as an input, we can determine whether or not a button has been pressed.

Button State and Pressing State

The connection between the button and Arduino Nano, as well as the configuration of the Arduino's pin, will determine the relationship between the button state and the pressing state.

There are two ways to use a button with Arduino:

  1. One button's pin is connected to VCC, the other is connected to an Arduino's pin with a pull-down resistor
    • When the button is pressed, the Arduino's pin state will be HIGH. If not, the Arduino's pin state will be LOW
    • An external resistor is required in this case.
  • One button's pin is connected to GND, the other is connected to an Arduino's pin with a pull-up resistor
    • When the button is pressed, the Arduino's pin state will be LOW. If not, the Arduino's pin state will be HIGH
    • We can use either an internal or external resistor. The internal resistor is already built into Arduino Nano, and can be set via Arduino Nano code.

    ※ NOTE THAT:

    • If we do NOT use any external pull-down/pull-up resistor, the state of the input pin is “floating” when the button is NOT pressed. This means the state can be HIGH or LOW (unstable, unfixed), resulting in incorrect detection.
    • The worst practice: Initializes the Arduino pin as an input (pinMode(BUTTON_PIN, INPUT)) without utilizing any external pull-down or pull-up resistors, .
    • The best practice: initializes the Arduino pin as an internal pull-up input (by using pinMode(BUTTON_PIN, INPUT_PULLUP)). It does NOT need to use any external pull-down/pull-up resistor.

    For the sake of simplicity for those just starting out, this tutorial uses the most straightforward approach: initializing the Arduino Nano pin as an internal pull-up input without requiring an external resistor. There is no need for the beginners to worry about how to connect the pull-up/pull-down resistor. All they have to do is use the Arduino Nano code.

    Wiring Diagram

    • Wiring Diagram between Arduino Nano and PCB-mount button
    The wiring diagram between Arduino Nano and Button

    This image is created using Fritzing. Click to enlarge image

    • Wiring Diagram between Arduino Nano and panel-mount button
    The wiring diagram between Arduino Nano and two-pin push button

    This image is created using Fritzing. Click to enlarge image

    How To Program For Button

    • Use the pinMode() function to set the Arduino Nano pin as an internal pull-up input.
    • For instance, pin 2:
    pinMode(2, INPUT_PULLUP);
    • Utilizes the digitalRead() function to ascertain the state of the Arduino Nano pin.
    int button_state = digitalRead(BUTTON_PIN);

    ※ NOTE THAT:

    Two common use cases exist:

    • The first: If the input state is HIGH, perform one action. If the input state is LOW, take the opposite action.
    • The second: If the input state changes from LOW to HIGH (or HIGH to LOW), do something.

    We select one of these based on the application. For example, when using a button to control an LED:

    • If we want the LED to be ON when the button is pressed and OFF when the button is NOT pressed, we SHOULD use the first use case.
    • If we want the LED to be toggled between ON and OFF each time we press the button, we SHOULD use the second use case.

    How to detect the state change from LOW to HIGH

    const int BUTTON_PIN = 2; // the number of the pushbutton pin int prev_button_state = HIGH; // 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 pushbutton pin as an pull-up input // the pull-up input pin will be 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_button_state == LOW && button_state == HIGH) Serial.println("The state changed from LOW to HIGH"); // save the last state prev_button_state = button_state; }

    Arduino Nano Code

    Detailed Instructions

    • Connect your Arduino Nano to a computer using a USB cable.
    • Launch the Arduino IDE, select the correct board and port.
    • Copy the code below and open it in the Arduino IDE.
    /* * 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 */ #define BUTTON_PIN 2 // The number of the pushbutton pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // Configure the Arduino Nano pin as a pull-up input // The pull-up input pin is HIGH when the button is open and LOW when pressed. 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 on Arduino IDE to compile and upload the code to Arduino Nano.
    How to upload code to Arduino Nano
    • Open the Serial Monitor.
    • Press and release the button multiple times.
    • Check out the results displayed on the Serial Monitor.
    COM6
    Send
    1 1 1 0 0 0 0 0 0 1 1 1
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

    1 is HIGH, 0 is LOW.

    Code Explanation

    Check out the line-by-line explanation contained in the comments of the source code!

    Modifying Arduino Nano Code

    Let's modify the code so that it can recognize press and release events.

    Detailed Instructions

    • Modify the code as follows:
    /* * 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 */ #define BUTTON_PIN 2 // The number of the pushbutton pin int prev_button_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); // Configure the Arduino Nano pin as a pull-up input // The pull-up input pin is HIGH when the button is open and LOW when pressed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if(prev_button_state == HIGH && button_state == LOW) Serial.println("The button is pressed"); else if(prev_button_state == LOW && button_state == HIGH) Serial.println("The button is released"); // save the the last state prev_button_state = button_state; }
    • Click the Upload button on Arduino IDE to compile and upload the code to the Arduino Nano board.
    Arduino IDE Upload Code
    • Open the Serial Monitor.
    • Press the button and hold it down.
    • Release the button and observe the result in the Serial Monitor.
    COM6
    Send
    The button is pressed The button is released
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

    ※ NOTE THAT:

    Even if you only press and release the button once, the output in the Serial Monitor may display multiple pressed and released events. This is the typical behavior of the button and is known as the "chattering phenomenon". To solve this problem, please refer to the Arduino Nano - Button Debounce tutorial.

    ※ NOTE THAT:

    We have developed a library, ezButton, to simplify the process for those just starting out, particularly when dealing with multiple buttons. You can find out more about the ezButton library here.

    Video Tutorial

    Challenge Yourself

    • When the button is pressed, the LED will be turned on.
    • When the button is not pressed, the LED will be turned off.
    • Each time the button is pressed, the LED will switch between ON and OFF.

    Additional Knowledge

    What are the occasions when it is appropriate to use a pull-down/pull-up resistor for an input pin? What are the times when it is not suitable to utilize a pull-down/pull-up resistor for an input pin?

    • If the sensor has either closed or open states, a pull-up or pull-down resistor is needed to make them become LOW and HIGH. Examples of such sensors include push-button, switch, and magnetic contact switch (door sensor).
    • On the other hand, if the sensor has two defined voltage levels (LOW and HIGH), no pull-up or pull-down resistor is required. Examples of such sensors are motion sensor and touch sensor.

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