Arduino - Button
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.
Please do not confuse with the following:
※ NOTE THAT:
There are two common troubles that beginners usually get into:
1. Floating input problem:
Symptom: the reading value from the input pin is not matched with the button's pressing state.
Cause: input pin is NOT used pull-up or pull-down resistor.
Solution: Use pull-up or pull-down resistor. It will be described in this tutorial
2. Chattering phenomenon:
It should be considered in only some application that needs to detect exactly number of the pressing.
Symptom: Button is pressed one, but Arduino code detects several times.
Cause: Due to mechanical and physical issues, the state of the button (or switch) is quickly toggled between LOW and HIGH several times
Or you can buy the following sensor kits:
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.
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.
image source: diyables.io
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
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.
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.
This image is created using Fritzing. Click to enlarge image
This image is created using Fritzing. Click to enlarge image
image source: diyables.io
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.
const int BUTTON_PIN = 7;
int lastState = HIGH;
int currentState;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
currentState = digitalRead(BUTTON_PIN);
if(lastState == LOW && currentState == HIGH)
Serial.println("The state changed from LOW to HIGH");
lastState = currentState;
}
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
const int BUTTON_PIN = 7;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
Serial.println(buttonState);
}
1 is HIGH, 0 is LOW.
Read the line-by-line explanation in comment lines of code!
Let's modify the code to detect the press and release events
const int BUTTON_PIN = 7;
int lastState = LOW;
int currentState;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
currentState = digitalRead(BUTTON_PIN);
if(lastState == HIGH && currentState == LOW)
Serial.println("The button is pressed");
else if(lastState == LOW && currentState == HIGH)
Serial.println("The button is released");
lastState = currentState;
}
The button is pressed
The button is released
※ 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.
※ NOTE THAT:
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.
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.
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 ...
Most electronic products have a reset button. Additionally, the button also keeps other functionalities in many products.
※ OUR MESSAGES
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!