Push Button
Interfacing Push Buttons with Arduino UNO
Interfacing Push Buttons with Arduino UNO demonstrates how push buttons can be interfaced with an Arduino UNO board to control an LED and a buzzer.
The practical also demonstrates the concept of pull-up and pull-down resistor configurations used while interfacing push buttons with a microcontroller.
Objectives
- To study the concept of push buttons.
- To study the pull-up resistor and pull-down resistor configurations used for interfacing push buttons with the Arduino UNO.
- To interface push buttons with the Arduino UNO.
- To write a program to control the inbuilt LED and buzzer using push buttons.
Theory
Introduction to Push Buttons
A push button or simply button is a simple switch mechanism used for controlling different aspects of a machine or process.
A push button is used to make or break an electrical circuit.
When the push button is pressed, the circuit is closed and current starts flowing through it. As a result, the devices connected in the circuit can be turned ON.
Examples include:
- Doorbells
- Horns
- Calculators
- Control panels
In this practical, a Momentary Tactile Push Button is used.
A momentary push button temporarily closes the circuit when it is pressed.
When the button is pressed:
Push Button Pressed → Circuit Closed → Device ON
When the button is released:
Push Button Released → Circuit Open → Device OFF
A push button generally has four pins. For interfacing purposes, the appropriate two terminals of the push button are used to provide the required connection to the Arduino UNO.
Push Button as a Digital Sensor
A push button is a digital sensor because it provides two possible logic states:
- LOW (0)
- HIGH (1)
One connection from the push button is connected to a digital input pin of the Arduino UNO.
The corresponding Arduino pin is configured as an INPUT.
When the push button is not pressed, the input pin must have a defined logic state.
If the input pin is not connected to a defined HIGH or LOW level, it can enter a floating or undetermined state.
Undefined Input → Floating State
To avoid this condition, pull-up or pull-down resistor configurations are used.
Pull-Up Resistor
In a pull-up resistor configuration, a resistor is connected between the input pin and the VCC supply.
The resistor keeps the input pin at a HIGH state when the push button is not pressed.
When the button is pressed, the input can be connected to ground and becomes LOW.
Pull-Up → Default HIGH
Pull-Down Resistor
In a pull-down resistor configuration, a resistor is connected between the input pin and Ground.
In this practical, the pull-down resistor configuration is used.
The value of the pull-down resistor is approximately 1 kΩ.
When the push button is not pressed, the input pin is connected to ground through the resistor.
Therefore, the input pin reads LOW.
Push Button Not Pressed → Input LOW
When the push button is pressed, the input pin gets connected directly to VCC.
Therefore, the input pin reads HIGH.
Push Button Pressed → Input HIGH
The operation can be summarized as:
| Push Button State | Input Pin | Logic |
|---|---|---|
| Not Pressed | Connected to GND through pull-down resistor | LOW |
| Pressed | Connected to VCC | HIGH |
Connections
| Device | Pin | Arduino UNO | Function |
|---|---|---|---|
| 1st Push Button | Data | D9 | Digital input |
| Push Buttons | VCC | 5V | Power supply |
| Push Buttons | GND | GND | Ground |
| Buzzer | Data | D5 | Buzzer control |
| Buzzer | GND | GND | Ground |
| LED | Inbuilt LED | D13 | LED control |
Steps for Assembling the Circuit
- Connect the data pin of the 1st Push Button to digital pin 9 of the Arduino UNO.
- Connect the data pin of the 2nd Push Button to digital pin 12 of the Arduino UNO.
- Connect the data pin of the buzzer to digital pin 5 of the Arduino UNO.
- Connect the VCC pin of the push buttons to the 5 V pin of the Arduino UNO.
- Connect the ground pin of the push buttons to the GND pin of the Arduino UNO.
- Connect the ground pin of the buzzer to the GND pin of the Arduino UNO.
- The inbuilt LED of the Arduino UNO is controlled through digital pin 13.
Safety Precautions
- First, make all the connections according to the circuit diagram.
- Check the connections of both push buttons before applying power.
- Ensure that the pull-down resistors are connected correctly.
- Check the polarity and connections of the buzzer.
- Ensure that all connections are correct before applying power.
- Then connect the Arduino UNO board to the PC/Laptop.
Schematic Diagram
The push buttons are connected to digital input pins of the Arduino UNO using the pull-down resistor configuration.
The first push button controls the LED and the second push button controls the buzzer.
Push Button 1 → Arduino D9 → LED
Push Button 2 → Arduino D12 → Buzzer
Interfacing Diagram Using Breadboard
The push buttons, LED and buzzer can be connected to the Arduino UNO using a breadboard.
The first push button is connected to D9 and the second push button is connected to D12.
The buzzer is connected to D5.
The required ground connections are connected to the Arduino UNO ground.
Interfacing Diagram Using the IoT Kit
The push buttons, LED and buzzer can be connected to the Arduino UNO through the corresponding connections provided on the IoT kit.
The push button inputs are connected to the specified digital pins and the output devices are controlled according to the program.
Procedure
- Write the program according to the algorithm given below.
- Save and compile the program.
- Connect the Arduino UNO board to the PC/Laptop using a USB cable.
- Upload the program to the Arduino UNO.
- Open the Serial Monitor if required.
- Press the first push button and observe the LED.
- Press the second push button and observe the buzzer.
- Release the push buttons and observe the corresponding outputs.
Algorithm
The following algorithm is used to interface the push buttons with the Arduino UNO:
- Start the Arduino IDE.
- Set the baud rate to 9600.
- Declare four integer variables for storing the pin numbers of the push buttons, buzzer and LED.
- Declare two integer variables for storing the input values of the push buttons.
- Set the push button pins as INPUT.
- Set the buzzer and LED pins as OUTPUT.
- Read the input from both push buttons using the digitalRead() function.
- Store the input values in their corresponding variables.
- Compare the first input value with HIGH.
- If the first input is HIGH, turn the LED ON; otherwise, turn the LED OFF.
- Compare the second input value with HIGH.
- If the second input is HIGH, turn the buzzer ON; otherwise, turn the buzzer OFF.
- Repeat the process continuously.
The overall operation is:
Push Button 1 → D9 → LED ON/OFF
Push Button 2 → D12 → Buzzer ON/OFF
Working
The Arduino UNO continuously reads the state of both push buttons.
The first push button is connected to digital pin D9.
When the first push button is pressed, D9 receives a HIGH signal. The Arduino then turns the LED ON.
D9 = HIGH → LED ON
When the first push button is released, D9 becomes LOW and the LED is turned OFF.
D9 = LOW → LED OFF
The second push button is connected to digital pin D12.
When the second push button is pressed, D12 receives a HIGH signal. The Arduino then turns the buzzer ON.
D12 = HIGH → Buzzer ON
When the second push button is released, D12 becomes LOW and the buzzer is turned OFF.
D12 = LOW → Buzzer OFF
Input and Output Operation
| Push Button | Arduino Pin | Pressed | Output |
|---|---|---|---|
| 1st Push Button | D9 | HIGH | LED ON |
| 1st Push Button | D9 | LOW | LED OFF |
| 2nd Push Button | D12 | HIGH | Buzzer ON |
| 2nd Push Button | D12 | LOW | Buzzer OFF |
Program
int ledPin = 13;
int buzzPin = 5;
int pushbutton1Pin = 9;
int pushbutton1Value;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(pushbutton1Pin, INPUT);
}
void loop() {
pushbutton1Value = digitalRead(pushbutton1Pin);
if (pushbutton1Value == 1) {
digitalWrite(ledPin, HIGH);
digitalWrite(buzzPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzPin, LOW);
}
}
Observation
Observe the output of the LED and buzzer while pressing the push buttons.
When the 1st push button is pressed, the LED turns ON.
When the 1st push button is released, the LED turns OFF.
When the 2nd push button is pressed, the buzzer turns ON.
When the 2nd push button is released, the buzzer turns OFF.
The push buttons provide digital input to the Arduino UNO in the form of HIGH and LOW signals.
Result
The Arduino UNO successfully interfaces with the push buttons using the pull-down resistor configuration.
The first push button is used to control the LED, while the second push button is used to control the buzzer.
The Arduino UNO correctly detects the HIGH and LOW states of the push buttons and controls the respective output devices accordingly.