Jump to content

Push Button

From cognifront
Revision as of 03:01, 4 September 2026 by CogWikiEditor (talk | contribs)

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

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

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.