Logo

ESP32 LED Blink with Button Control

Introduction to LED Blink with Button Control

In this simple project, we will learn how to use a button to control an LED blink on the ESP32. By pressing the button, we can toggle the LED on or off. This project is a great starting point for beginners learning about GPIO pins and input/output operations on the ESP32. It also demonstrates basic digital input (button press) and output (LED control) functionality.

Components Required

To build this project, you will need the following components: ESP32 development board, an LED, a button, a 220Ω resistor (for the LED), a 10kΩ resistor (for the button), and jumper wires for connecting the components.

  • Connect the LED to the ESP32 with the appropriate resistor to limit the current.
  • Connect a pushbutton to one of the digital GPIO pins on the ESP32 and configure it with a pull-up resistor.
  • Write code to read the button state and toggle the LED when the button is pressed.
  • Upload the code to the ESP32 and test the LED toggle functionality using the button.

Formula: LED State = Button Press ? (LED OFF -> LED ON) : (LED ON -> LED OFF)

Components List

  • 1 × ESP32 Development Board
  • 1 × LED (any standard LED)
  • 1 × Pushbutton
  • 1 × 220Ω Resistor (for LED)
  • 1 × 10kΩ Resistor (for Button pull-up)
  • Jumper wires

Pin Configuration

  • GPIO 23 (LED): The LED is connected to GPIO 23 of the ESP32. The positive leg of the LED connects to GPIO 23, and the negative leg connects to ground through a 220Ω resistor.
  • GPIO 22 (Button): The button is connected to GPIO 22. The other leg of the button is connected to the ground with a 10kΩ pull-down resistor to avoid floating state.

Ensure that the button is configured with a pull-up resistor to handle stable high and low voltage levels.

Wiring and Connections

  • -> GPIO 23
  • -> GPIO 22

Code for ESP32 LED Blink with Button Control

1#define LED_PIN 23
2#define BUTTON_PIN 22
3
4int buttonState = 0; // Variable to store button state
5int lastButtonState = 0; // Variable to store last button state
6int ledState = LOW; // LED initial state
7
8void setup() {
9  pinMode(LED_PIN, OUTPUT);  // Set LED pin as output
10  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Set button pin as input with pull-up resistor
11}
12
13void loop() {
14  buttonState = digitalRead(BUTTON_PIN);  // Read the state of the button
15
16  // Check if the button is pressed (LOW state due to INPUT_PULLUP)
17  if (buttonState == LOW && lastButtonState == HIGH) {
18    ledState = !ledState;  // Toggle LED state
19    digitalWrite(LED_PIN, ledState);  // Set LED state
20    delay(200);  // Debounce delay
21  }
22
23  lastButtonState = buttonState;  // Save the current button state for the next loop
24}

Code Explanation

  • #define LED_PIN 23: This line defines the GPIO pin for the LED, which is connected to pin 23 of the ESP32.
  • #define BUTTON_PIN 22: This line defines the GPIO pin for the button, which is connected to pin 22 of the ESP32.
  • pinMode(LED_PIN, OUTPUT);: This configures the LED pin as an output pin to control the LED state.
  • pinMode(BUTTON_PIN, INPUT_PULLUP);: This configures the button pin as an input with an internal pull-up resistor. The button will read HIGH when not pressed and LOW when pressed.
  • if (buttonState == LOW && lastButtonState == HIGH) {: This condition checks if the button has just been pressed. The button reads LOW when pressed, and we use the last state to detect a state change.
  • ledState = !ledState;: This toggles the LED state. If the LED is off (LOW), it turns on (HIGH), and if it's on (HIGH), it turns off (LOW).
  • digitalWrite(LED_PIN, ledState);: This writes the new LED state (either HIGH or LOW) to the LED pin, turning the LED on or off.
  • delay(200);: This introduces a short delay to debounce the button press, ensuring stable reading and preventing multiple toggles from a single press.

Applications

  • Basic LED control using a button.
  • Learning about GPIO pins, input/output, and state transitions.
  • Starting point for more complex projects like controlling multiple LEDs or other devices with buttons.

Conclusion

This simple ESP32 project demonstrates how to control an LED with a button, providing a basic understanding of input and output operations. The project can be extended to more complex systems where buttons control other components or trigger specific actions.