Jump to content

Push Button: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
CogWikiEditor (talk | contribs)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
===Interfacing Push Buttons with Arduino UNO===
===Interfacing Push Buttons with ESP32===


'''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.
'''Interfacing Push Buttons with ESP32''' demonstrates how multiple push buttons can be interfaced with an ESP32 board to control an LED.


The practical also demonstrates the concept of '''pull-up''' and '''pull-down''' resistor configurations used while interfacing push buttons with a microcontroller.
The practical also demonstrates the concept of '''pull-up''' and '''pull-down''' resistor configurations used while interfacing push buttons with a microcontroller.
Line 8: Line 8:


* To study the concept of push buttons.
* 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 study the pull-up resistor and pull-down resistor configurations used for interfacing push buttons with the ESP32.
* To interface push buttons with the Arduino UNO.
* To interface multiple push buttons with the ESP32.
* To write a program to control the inbuilt LED and buzzer using push buttons.
* To write a program to control an LED using push buttons.


== Theory ==
== Theory ==
Line 45: Line 45:
</center>
</center>


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.
A push button generally has four pins. For interfacing purposes, the appropriate terminals of the push button are used to provide the required connection to the ESP32.


== Push Button as a Digital Sensor ==
== Push Button as a Digital Sensor ==
Line 54: Line 54:
* '''HIGH (1)'''
* '''HIGH (1)'''


One connection from the push button is connected to a digital input pin of the Arduino UNO.
The push buttons are connected to digital GPIO pins of the ESP32.


The corresponding Arduino pin is configured as an '''INPUT'''.
The corresponding ESP32 pins are configured as '''INPUT'''.


When the push button is not pressed, the input pin must have a defined logic state.
When the push button is not pressed, the input pin must have a defined logic state.
Line 79: Line 79:
'''Pull-Up → Default HIGH'''
'''Pull-Up → Default HIGH'''
</center>
</center>
[[File:Pull-up-pull-down-resistor.jpg|thumb|400px| On our kit pull down resistors have been provided]]


== Pull-Down Resistor ==
== Pull-Down Resistor ==


In a '''pull-down resistor configuration''', a resistor is connected between the input pin and '''Ground'''.
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.
When the push button is not pressed, the input pin is connected to ground through the resistor.
Line 96: Line 94:
</center>
</center>


When the push button is pressed, the input pin gets connected directly to '''VCC'''.
When the push button is pressed, the input pin gets connected to '''VCC'''.


Therefore, the input pin reads '''HIGH'''.
Therefore, the input pin reads '''HIGH'''.
Line 119: Line 117:
| HIGH
| HIGH
|}
|}
[[File:Pull-up-pull-down-resistor.jpg|thumb|400px| On our kit pull down resistors have been provided]]


== Connections ==
== Connections ==
The four push buttons and LED are connected to the ESP32 using the GPIO pins specified in the program.


{| class="wikitable"
{| class="wikitable"
! Device
! Device
! Pin
! Pin
! Arduino UNO
! ESP32 GPIO
! Function
! Function
|-
|-
| 1st Push Button
| 1st Push Button
| Data
| Data
| D9
| GPIO 2
| Digital input
| Digital input
|-
|-
| Push Buttons
| 2nd Push Button
| VCC
| Data
| 5V
| GPIO 4
| Power supply
| Digital input
|-
|-
| Push Buttons
| 3rd Push Button
| GND
| Data
| GND
| GPIO 12
| Ground
| Digital input
|-
|-
| Buzzer
| 4th Push Button
| Data
| Data
| D5
| GPIO 13
| Buzzer control
| Digital input
|-
| Buzzer
| GND
| GND
| Ground
|-
|-
| LED
| LED
| Inbuilt LED
| Data
| D13
| GPIO 14
| LED control
| LED control
|}
|}
The required VCC and GND connections are made according to the push button circuit.
== Steps for Assembling the Circuit ==
# Connect the data pin of the '''1st Push Button''' to '''GPIO 2''' of the ESP32.
# Connect the data pin of the '''2nd Push Button''' to '''GPIO 4''' of the ESP32.
# Connect the data pin of the '''3rd Push Button''' to '''GPIO 12''' of the ESP32.
# Connect the data pin of the '''4th Push Button''' to '''GPIO 13''' of the ESP32.
# Connect the LED data pin to '''GPIO 14''' of the ESP32.
# Connect the required VCC connections of the push buttons according to the circuit diagram.
# Connect the ground connections of the push buttons and LED to '''GND''' of the ESP32.
== Safety Precautions ==
* First, make all the connections according to the circuit diagram.
* Check the connections of all four push buttons before applying power.
* Ensure that the pull-down resistors are connected correctly.
* Check the LED connections.
* Ensure that all connections are correct before applying power.
* Then connect the ESP32 board to the PC/Laptop.
== Interfacing Diagram Using Breadboard ==
The four push buttons are connected to the ESP32 digital GPIO pins.
The connections are:
{| class="wikitable"
! Push Button
! ESP32 GPIO
|-
| Push Button 1
| GPIO 2
|-
| Push Button 2
| GPIO 4
|-
| Push Button 3
| GPIO 12
|-
| Push Button 4
| GPIO 13
|}
The LED is connected to '''GPIO 14'''.
The push button inputs are read by the ESP32 and their values are displayed on the Serial Monitor.
== Interfacing Diagram Using the IoT Kit ==
The four push buttons and LED can be connected to the ESP32 through the corresponding connections provided on the IoT kit.
The push button inputs are connected to GPIO 2, GPIO 4, GPIO 12 and GPIO 13.
The LED is controlled through GPIO 14.
== Procedure ==
# Write the program according to the algorithm given below.
# Save and compile the program.
# Connect the ESP32 board to the PC/Laptop using a USB cable.
# Upload the program to the ESP32.
# Open the Serial Monitor.
# Set the Serial Monitor baud rate to '''9600'''.
# Press each push button and observe its corresponding value on the Serial Monitor.
# Observe the LED output when any of the push buttons is pressed.
== Algorithm ==
The following algorithm is used to interface four push buttons with the ESP32:
# Start the Arduino IDE.
# Set the baud rate to '''9600'''.
# Declare integer variables for storing the pin numbers of the four push buttons and LED.
# Declare integer variables for storing the input values of the four push buttons.
# Set the four push button pins as '''INPUT'''.
# Set the LED pin as '''OUTPUT'''.
# Read the input from the first push button using '''digitalRead()'''.
# Display the value of the first push button on the Serial Monitor.
# Read and display the value of the second push button.
# Read and display the value of the third push button.
# Read and display the value of the fourth push button.
# Give a delay of '''1000 ms'''.
# Check whether any of the four push button values is HIGH.
# If any push button value is HIGH, turn the LED '''ON'''.
# After a delay of '''500 ms''', turn the LED '''OFF'''.
# Give another delay of '''500 ms'''.
# Repeat the process continuously.
The overall operation is:
<center>
'''Push Buttons → ESP32 GPIO Inputs → Serial Monitor'''
</center>
<center>
'''Any Button HIGH → LED ON → 500 ms → LED OFF → 500 ms'''
</center>
== Working ==
The ESP32 continuously reads the state of all four push buttons.
The first push button is connected to '''GPIO 2'''.
The second push button is connected to '''GPIO 4'''.
The third push button is connected to '''GPIO 12'''.
The fourth push button is connected to '''GPIO 13'''.
The values of all four push buttons are displayed on the Serial Monitor at a baud rate of '''9600'''.
The ESP32 checks whether any of the four push button input values is HIGH.
If at least one push button has a HIGH value, the LED connected to '''GPIO 14''' is turned ON.
<center>
'''Any Push Button HIGH → LED ON'''
</center>
The LED remains ON for '''500 ms''' and is then turned OFF for '''500 ms'''.
<center>
'''LED ON → 500 ms → LED OFF → 500 ms'''
</center>
The process repeats continuously.
== Input and Output Operation ==
{| class="wikitable"
! Push Button
! ESP32 GPIO
! Input
! Result
|-
| Push Button 1
| GPIO 2
| HIGH
| LED operation triggered
|-
| Push Button 2
| GPIO 4
| HIGH
| LED operation triggered
|-
| Push Button 3
| GPIO 12
| HIGH
| LED operation triggered
|-
| Push Button 4
| GPIO 13
| HIGH
| LED operation triggered
|}
If any one of the four push buttons is HIGH, the LED connected to GPIO 14 is operated.


== Program ==
== Program ==


<pre>
<pre>
int ledPin = 13;
int push1Pin = 2;
int buzzPin = 5;
 
int pushbutton1Pin = 9;
int push2Pin = 4;
 
int push3Pin = 12;
 
int push4Pin = 13;
 
int led1Pin = 14;
 
int push1Value;
 
int push2Value;
 
int push3Value;
 
int push4Value;


int pushbutton1Value;


void setup() {
void setup() {
   Serial.begin(9600);
   Serial.begin(9600);


   pinMode(ledPin, OUTPUT);
   pinMode(push1Pin, INPUT);
   pinMode(buzzPin, OUTPUT);
 
   pinMode(pushbutton1Pin, INPUT);
  pinMode(push2Pin, INPUT);
 
   pinMode(push3Pin, INPUT);
 
   pinMode(push4Pin, INPUT);
 
  pinMode(led1Pin, OUTPUT);
 
}
}


void loop() {
void loop() {
  pushbutton1Value = digitalRead(pushbutton1Pin);


   if (pushbutton1Value == 1) {
  push1Value = digitalRead(push1Pin);
     digitalWrite(ledPin, HIGH);
 
     digitalWrite(buzzPin, HIGH);
  Serial.print("Button 1 value : ");
  }
 
  else {
  Serial.println(push1Value);
     digitalWrite(ledPin, LOW);
 
     digitalWrite(buzzPin, LOW);
  push2Value = digitalRead(push2Pin);
 
  Serial.print("Button 2 value : ");
 
  Serial.println(push2Value);
 
  push3Value = digitalRead(push3Pin);
 
  Serial.print("Button 3 value : ");
 
  Serial.println(push3Value);
 
  push4Value = digitalRead(push4Pin);
 
  Serial.print("Button 4 value : ");
 
  Serial.println(push4Value);
 
  delay(1000);
 
   if (push1Value == 1 || push2Value == 1 || push3Value == 1 || push4Value == 1)
 
  {
 
     digitalWrite(led1Pin, HIGH);
 
     delay(500);
 
     digitalWrite(led1Pin, LOW);
 
     delay(500);
 
   }
   }
}
}
</pre>
</pre>


== Observation ==
== Observation ==


Observe the output of the LED and buzzer while pressing the push buttons.
Observe the output on the Serial Monitor.


When the '''1st push button''' is pressed, the LED turns '''ON'''.
The Serial Monitor displays the values of all four push buttons:


When the '''1st push button''' is released, the LED turns '''OFF'''.
<center>
'''Button 1 value : 0/1'''
</center>


When the '''2nd push button''' is pressed, the buzzer turns '''ON'''.
<center>
'''Button 2 value : 0/1'''
</center>
 
<center>
'''Button 3 value : 0/1'''
</center>
 
<center>
'''Button 4 value : 0/1'''
</center>


When the '''2nd push button''' is released, the buzzer turns '''OFF'''.
When any one of the push buttons is pressed, its corresponding input value becomes '''1'''.


The push buttons provide digital input to the Arduino UNO in the form of '''HIGH''' and '''LOW''' signals.
When any push button has a HIGH input, the LED connected to '''GPIO 14''' turns ON for '''500 ms''' and then turns OFF for '''500 ms'''.


== Result ==
== Result ==


The Arduino UNO successfully interfaces with the '''push buttons''' using the pull-down resistor configuration.
The ESP32 successfully interfaces with '''four push buttons''' using digital GPIO inputs.


The first push button is used to control the '''LED''', while the second push button is used to control the '''buzzer'''.
The values of all four push buttons are displayed on the Serial Monitor.


The Arduino UNO correctly detects the HIGH and LOW states of the push buttons and controls the respective output devices accordingly.
When any one of the push buttons is HIGH, the LED connected to '''GPIO 14''' is turned ON and OFF with a delay of '''500 ms'''.

Latest revision as of 03:30, 4 September 2026

Interfacing Push Buttons with ESP32

Interfacing Push Buttons with ESP32 demonstrates how multiple push buttons can be interfaced with an ESP32 board to control an LED.

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 ESP32.
  • To interface multiple push buttons with the ESP32.
  • To write a program to control an LED 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 terminals of the push button are used to provide the required connection to the ESP32.

Push Button as a Digital Sensor

A push button is a digital sensor because it provides two possible logic states:

  • LOW (0)
  • HIGH (1)

The push buttons are connected to digital GPIO pins of the ESP32.

The corresponding ESP32 pins are configured as 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

On our kit pull down resistors have been provided

Pull-Down Resistor

In a pull-down resistor configuration, a resistor is connected between the input pin and Ground.

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 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
On our kit pull down resistors have been provided

Connections

The four push buttons and LED are connected to the ESP32 using the GPIO pins specified in the program.

Device Pin ESP32 GPIO Function
1st Push Button Data GPIO 2 Digital input
2nd Push Button Data GPIO 4 Digital input
3rd Push Button Data GPIO 12 Digital input
4th Push Button Data GPIO 13 Digital input
LED Data GPIO 14 LED control

The required VCC and GND connections are made according to the push button circuit.

Steps for Assembling the Circuit

  1. Connect the data pin of the 1st Push Button to GPIO 2 of the ESP32.
  2. Connect the data pin of the 2nd Push Button to GPIO 4 of the ESP32.
  3. Connect the data pin of the 3rd Push Button to GPIO 12 of the ESP32.
  4. Connect the data pin of the 4th Push Button to GPIO 13 of the ESP32.
  5. Connect the LED data pin to GPIO 14 of the ESP32.
  6. Connect the required VCC connections of the push buttons according to the circuit diagram.
  7. Connect the ground connections of the push buttons and LED to GND of the ESP32.

Safety Precautions

  • First, make all the connections according to the circuit diagram.
  • Check the connections of all four push buttons before applying power.
  • Ensure that the pull-down resistors are connected correctly.
  • Check the LED connections.
  • Ensure that all connections are correct before applying power.
  • Then connect the ESP32 board to the PC/Laptop.

Interfacing Diagram Using Breadboard

The four push buttons are connected to the ESP32 digital GPIO pins.

The connections are:

Push Button ESP32 GPIO
Push Button 1 GPIO 2
Push Button 2 GPIO 4
Push Button 3 GPIO 12
Push Button 4 GPIO 13

The LED is connected to GPIO 14.

The push button inputs are read by the ESP32 and their values are displayed on the Serial Monitor.

Interfacing Diagram Using the IoT Kit

The four push buttons and LED can be connected to the ESP32 through the corresponding connections provided on the IoT kit.

The push button inputs are connected to GPIO 2, GPIO 4, GPIO 12 and GPIO 13.

The LED is controlled through GPIO 14.

Procedure

  1. Write the program according to the algorithm given below.
  2. Save and compile the program.
  3. Connect the ESP32 board to the PC/Laptop using a USB cable.
  4. Upload the program to the ESP32.
  5. Open the Serial Monitor.
  6. Set the Serial Monitor baud rate to 9600.
  7. Press each push button and observe its corresponding value on the Serial Monitor.
  8. Observe the LED output when any of the push buttons is pressed.

Algorithm

The following algorithm is used to interface four push buttons with the ESP32:

  1. Start the Arduino IDE.
  2. Set the baud rate to 9600.
  3. Declare integer variables for storing the pin numbers of the four push buttons and LED.
  4. Declare integer variables for storing the input values of the four push buttons.
  5. Set the four push button pins as INPUT.
  6. Set the LED pin as OUTPUT.
  7. Read the input from the first push button using digitalRead().
  8. Display the value of the first push button on the Serial Monitor.
  9. Read and display the value of the second push button.
  10. Read and display the value of the third push button.
  11. Read and display the value of the fourth push button.
  12. Give a delay of 1000 ms.
  13. Check whether any of the four push button values is HIGH.
  14. If any push button value is HIGH, turn the LED ON.
  15. After a delay of 500 ms, turn the LED OFF.
  16. Give another delay of 500 ms.
  17. Repeat the process continuously.

The overall operation is:

Push Buttons → ESP32 GPIO Inputs → Serial Monitor

Any Button HIGH → LED ON → 500 ms → LED OFF → 500 ms

Working

The ESP32 continuously reads the state of all four push buttons.

The first push button is connected to GPIO 2.

The second push button is connected to GPIO 4.

The third push button is connected to GPIO 12.

The fourth push button is connected to GPIO 13.

The values of all four push buttons are displayed on the Serial Monitor at a baud rate of 9600.

The ESP32 checks whether any of the four push button input values is HIGH.

If at least one push button has a HIGH value, the LED connected to GPIO 14 is turned ON.

Any Push Button HIGH → LED ON

The LED remains ON for 500 ms and is then turned OFF for 500 ms.

LED ON → 500 ms → LED OFF → 500 ms

The process repeats continuously.

Input and Output Operation

Push Button ESP32 GPIO Input Result
Push Button 1 GPIO 2 HIGH LED operation triggered
Push Button 2 GPIO 4 HIGH LED operation triggered
Push Button 3 GPIO 12 HIGH LED operation triggered
Push Button 4 GPIO 13 HIGH LED operation triggered

If any one of the four push buttons is HIGH, the LED connected to GPIO 14 is operated.

Program

int push1Pin = 2;

int push2Pin = 4;

int push3Pin = 12;

int push4Pin = 13;

int led1Pin = 14;

int push1Value;

int push2Value;

int push3Value;

int push4Value;


void setup() {

  Serial.begin(9600);

  pinMode(push1Pin, INPUT);

  pinMode(push2Pin, INPUT);

  pinMode(push3Pin, INPUT);

  pinMode(push4Pin, INPUT);

  pinMode(led1Pin, OUTPUT);

}

void loop() {

  push1Value = digitalRead(push1Pin);

  Serial.print("Button 1 value : ");

  Serial.println(push1Value);

   push2Value = digitalRead(push2Pin);

  Serial.print("Button 2 value : ");

  Serial.println(push2Value);

   push3Value = digitalRead(push3Pin);

  Serial.print("Button 3 value : ");

  Serial.println(push3Value);

   push4Value = digitalRead(push4Pin);

  Serial.print("Button 4 value : ");

  Serial.println(push4Value);

  delay(1000);

  if (push1Value == 1 || push2Value == 1 || push3Value == 1 || push4Value == 1)

  {

    digitalWrite(led1Pin, HIGH);

    delay(500);

    digitalWrite(led1Pin, LOW);

    delay(500);

  }

}

Observation

Observe the output on the Serial Monitor.

The Serial Monitor displays the values of all four push buttons:

Button 1 value : 0/1

Button 2 value : 0/1

Button 3 value : 0/1

Button 4 value : 0/1

When any one of the push buttons is pressed, its corresponding input value becomes 1.

When any push button has a HIGH input, the LED connected to GPIO 14 turns ON for 500 ms and then turns OFF for 500 ms.

Result

The ESP32 successfully interfaces with four push buttons using digital GPIO inputs.

The values of all four push buttons are displayed on the Serial Monitor.

When any one of the push buttons is HIGH, the LED connected to GPIO 14 is turned ON and OFF with a delay of 500 ms.