This tutorial instructs you how to use Arduino Nano with button. In detail, we will learn:
How to connect the button to Arduino Nano
How to program Arduino Nano to read the state of the button
How to program Arduino Nano to detect the pressing and releasing events from button.
How to prevent the floating input problem when using the button with Arduino Nano.
How to prevent the chattering problem when using the button with Arduino Nano.
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.
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.
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)
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).
? 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.
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: Connect this pin to ground.
VCC: Connect this pin to a 5V or 3.3V power supply.
OUT: Connect this pin to a digital input on your Arduino.
With this configuration, the module outputs LOW when the button is not pressed and outputs HIGH when the button is pressed.
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.
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:
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
This image is created using Fritzing. Click to enlarge image
Wiring Diagram between Arduino Nano and panel-mount button
This image is created using Fritzing. Click to enlarge image
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
constint BUTTON_PIN = 2; // the number of the pushbutton pinint prev_button_state = HIGH; // the previous state from the input pinint button_state; // the current reading from the input pinvoidsetup() {// 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);}voidloop() {// 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 pinvoidsetup() {// 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);}voidloop() {// 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.
Open the Serial Monitor.
Press and release the button multiple times.
Check out the results displayed on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
1
1
1
0
0
0
0
0
0
1
1
1
Ln 11, Col 1
Arduino Nano on COM15
2
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 pinint prev_button_state = LOW; // The previous state from the input pinint button_state; // The current reading from the input pinvoidsetup() {// 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);}voidloop() {// 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");elseif(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.
Open the Serial Monitor.
Press the button and hold it down.
Release the button and observe the result in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' on 'COM15')
New Line
9600 baud
The button is pressed
The button is released
Ln 11, Col 1
Arduino Nano on COM15
2
※ 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.
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.
For the button module, set the pin with pinMode(BUTTON_PIN, INPUT). The module outputs LOW when not pressed and HIGH when pressed.
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.
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!