- LED Blink with Button
- Motion Detection Alarm System
- Simple RGB LED Controller
- Blinking LED with WebSocket Control
- Control LED Brightness using PWM
- Web Page with HTML and CSS on ESP32
- Control Multiple LEDs
- ESP32 with Bluetooth Serial Communication
- EEPROM with ESP32
- ESP32 Push Button Input: Reading Digital States
- Interfacing DHT11 Sensor
- Interfacing Ultrasonic Sensor
- Interfacing Flame Sensor
- Interfacing Sound Sensor
- Interfacing Potentiometer
- Interfacing IR Sensor
- Interfacing Servo Motor
- Interfacing Cam Wireless
- Interfacing DC Motor
- Interfacing Shock Sensor
- Interfacing Color Recognition Sensor
- Interfacing RFID Module
- TTGO LoRa32 SX1276 OLED
- Interfacing Keypad
- Interfacing Solenoid Lock
- Interfacing 16x2 LCD
- Interfacing Soil Moisture
- Interfacing MQ-7 Gas Sensor
- Interfacing Light Sleep Mode
- Interfacing Smart Light Control
- Interfacing (IoT) Weather Station
- Interfacing Web Server for Temperature Data Display
- Interfacing Home Automation System with Relay Control
- Interfacing IoT Smart Garden
- Face Recognition-Based Door Unlock System
- Interfacing Wi-Fi Jammer Detector
- Interfacing Health Band with Pulse
- Interfacing Sound Level Logger for Classrooms
- Night Vision Surveillance Camera
- Solar Panel Monitoring System
- Smart Farming Robot for Crop Surveillance
- Smart Water Quality Monitoring System
- Industrial IoT Gateway for Real-Time Monitoring
- Agriculture System with Automated Drone Control
Read Digital Input from Push Button using ESP32
What is a Push Button?
A push button is a simple mechanical switch that can either be pressed or released. It is used to send a signal to the microcontroller based on its state (pressed or not pressed). In this tutorial, we will use a push button as a digital input for the ESP32.
Working Principle of Push Button
When the push button is pressed, it either connects a pin to ground (active low) or to a supply voltage (active high), depending on how it is wired. The ESP32 reads this voltage state to determine if the button is pressed or not.
- Wire the push button to a GPIO pin.
- Configure the GPIO pin as an input with an internal pull-up or pull-down resistor.
- Use `digitalRead()` to check the button's state.
- Use an `if` statement to perform actions based on the button press.
Formula: N/A
Components Required
- ESP32 Development Board
- Push Button
- 10kΩ Resistor (for pull-down or pull-up configuration)
- Breadboard
- Jumper Wires
Pin Configuration of Push Button
- Button Pin: GPIO Pin used to read the button input.
- GND: Ground pin connected to one side of the push button.
It is good practice to use internal pull-up or pull-down resistors to prevent floating pin behavior, which can cause undefined states.
Wiring Push Button to ESP32
- Button Pin -> GPIO 13 (or any other available GPIO)
- GND -> GND
Arduino Code to Read Push Button
1#define BUTTON_PIN 13
2
3void setup() {
4 Serial.begin(115200);
5 pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
6}
7
8void loop() {
9 int buttonState = digitalRead(BUTTON_PIN);
10
11 if (buttonState == LOW) {
12 Serial.println("Button Pressed");
13 } else {
14 Serial.println("Button Released");
15 }
16
17 delay(200); // Debounce delay
18}
Code Explanation (Line-by-Line)
- #define BUTTON_PIN 13: Defines GPIO 13 as the button input pin.
- pinMode(BUTTON_PIN, INPUT_PULLUP);: Sets the button pin as an input with an internal pull-up resistor.
- int buttonState = digitalRead(BUTTON_PIN);: Reads the digital state of the button (HIGH or LOW).
- if (buttonState == LOW) { ... }: If the button is pressed, the state will be LOW (assuming active-low configuration).
- Serial.println("Button Pressed");: Displays a message in the Serial Monitor when the button is pressed.
- delay(200);: Debounce delay to prevent multiple reads from button bounce.
Applications
- Simple User Input (on/off actions)
- Toggling LED or Relay
- Controlling Devices via Buttons
- Creating a Button-Activated Alarm or Alert System
- Building User Interfaces for Embedded Systems
Conclusion
Reading a digital input from a push button with the ESP32 is straightforward and enables various interactive applications. Whether used for simple on/off control or more complex user input, push buttons are an essential component in embedded systems, and with the ESP32's internal pull-up/pull-down resistors, they are easy to interface.