The button is also called pushbutton, tactile button or momentary switch. It is a basic component and widely used in many Arduino projects. It is simple to use. However, it may make the beginners confuse, due to mechanical, physical issues and ways to use it as well. This tutorial makes it easy for the beginners.
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .
About Button
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. There are various types of push buttons, broadly categorized into two groups:
However, these pins are internally connected in pairs. Therefore, we only need to use two of the four pins, which are NOT internally connected.
There are four ways (actually two ways because of symmetry) to connect to button (see image)
We can use only two pins of a button, why does it have four pins?
⇒ To make it stand firmly in PCB (board) to resist the pressing force.
The panel-mount buttons 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 is NOT connected to pin B
When the button is pressed, pin A is connected to pin B
Arduino - Button
One button's pin is connected to VCC or GND. The other pin is connected to an Arduino pin.
By reading the state of Arduino's pin (configured as input pin), we can detect the button is pressed or NOT.
Button State and Pressing State
The relation between the button state and the pressing state depends on how we connect the button with Arduino and the setting of the Arduino's pin.
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
If the button is pressed, Arduino's pin state is HIGH. If otherwise, Arduino's pin state is LOW
We MUST use an external resistor.
One button's pin is connected to GND, the other is connected to an Arduino's pin with a pull-up resistor
If the button is pressed, Arduino's pin state is LOW. If otherwise, Arduino's pin state is HIGH
We can use either an internal or external resistor. The internal resistor is built inside Arduino, we just need to set via Arduino code.
※ NOTE THAT:
If we do NOT use neither pull-down nor pull-up resistor, the state of the input pin is “floating” when the button is NOT pressed. It means the state can be HIGH or LOW (unstable, unfixed), resulting in the wrong detection.
The worst practice: initializes the Arduino pin as an input (by using pinMode(BUTTON_PIN, INPUT)) and does NOT use any external pull-down/pull-up resistor.
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.
To make it easy for beginners, this tutorial uses the simplest method: initializes the Arduino pin as an internal pull-up input without using the external resistor. The beginners do NOT need to care about how to wire the pull-up/pull-down resistor. The beginners just need to use the Arduino code.
Wiring Diagram
Wiring Diagram between Arduino and PCB-mount button
This image is created using Fritzing. Click to enlarge image
Wiring Diagram between Arduino and panel-mount button
This image is created using Fritzing. Click to enlarge image
How To Program For Button
Initializes the Arduino pin as an internal pull-up input by using pinMode() function. For example, pin 7:
pinMode(7, INPUT_PULLUP);
Reads the state of the Arduino pin by using digitalRead() function.
int buttonState = digitalRead(BUTTON_PIN);
※ NOTE THAT:
There are two wide-used use cases:
The first: If the input state is HIGH, do something. If the input state is LOW, do another thing in reverse.
The second: If the input state is changed from LOW to HIGH (or HIGH to LOW), do something.
Depending on the application, we choose one of them. For example, in case of 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 toggle 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
// constants won't change. They're used here to set pin numbers:constint BUTTON_PIN = 7; // the number of the pushbutton pin// Variables will change:int lastState = HIGH; // the previous state from the input pinint currentState; // the current reading from the input pinvoidsetup() {// initialize serial communication at 9600 bits per second: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: currentState = digitalRead(BUTTON_PIN);if(lastState == LOW && currentState == HIGH)Serial.println("The state changed from LOW to HIGH");// save the last state lastState = currentState;}
Arduino Code
Quick Steps
Connect Arduino to PC via USB cable
Open Arduino IDE, select the right board and port
Copy the below code and open with Arduino IDE
// constants won't change. They're used here to set pin numbers:constint BUTTON_PIN = 7; // the number of the pushbutton pinvoidsetup() {// initialize serial communication at 9600 bits per second: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:int buttonState = digitalRead(BUTTON_PIN);// print out the button's state Serial.println(buttonState);}
Click Upload button on Arduino IDE to upload code to Arduino
Open Serial Monitor
Press and release the button several time
See the result on Serial Monitor:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
1
1
1
0
0
0
0
0
0
1
1
1
Ln 11, Col 1
Arduino Uno on COM15
2
1 is HIGH, 0 is LOW.
Code Explanation
Read the line-by-line explanation in comment lines of code!
Modifying Arduino Code
Let's modify the code to detect the press and release events
Quick Steps
Modify the code as below
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button */// constants won't change. They're used here to set pin numbers:constint BUTTON_PIN = 7; // the number of the pushbutton pin// Variables will change:int lastState = LOW; // the previous state from the input pinint currentState; // the current reading from the input pinvoidsetup() {// initialize serial communication at 9600 bits per second: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: currentState = digitalRead(BUTTON_PIN);if(lastState == HIGH && currentState == LOW)Serial.println("The button is pressed");elseif(lastState == LOW && currentState == HIGH)Serial.println("The button is released");// save the the last state lastState = currentState;}
Click Upload button on Arduino IDE to upload code to Arduino
Open Serial Monitor
Press the button and then release
See the result on Serial Monitor
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.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
Ln 11, Col 1
Arduino Uno on COM15
2
※ NOTE THAT:
Even you pressed and released the button only once, the output in Serial Monitor may show several pressed and release events. This is the normal behavior of the button. This behavior is called the “chattering phenomenon”. You can learn more in Arduino - Button Debounce tutorial.
To make it much easier for beginners, especially when using multiple buttons, we created a library, called ezButton. You can learn about ezButton library here.
Use pinMode(BUTTON_PIN, INPUT) for the button module. It outputs LOW when not pressed and HIGH when pressed.
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.
Challenge Yourself
Turn on LED when button is pressed and turn off LED when button is NOT pressed.
Toggle LED between ON and OFF each time the button is pressed.
Additional Knowledge
When should and should NOT we use a pull-down/pull-up resistor for an input pin?
If the sensor has either closed (connected to VCC or GND) or open (NOT connected to anything) states, you need a pull-up or pull-down resistor to make these states become two states: LOW and HIGH. For example, push-button, switch, magnetic contact switch (door sensor)...
If the sensor has two defined voltage levels (LOW and HIGH), you do NOT need a pull-up or pull-down resistor. For example, motion sensor, touch sensor ...
Button on Commercial Products
Most electronic products have a reset button. Additionally, the button also keeps other functionalities in many products.
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!