LED Toggle with Button Press Using MicroPython for ESP32 and ESP8266

This project demonstrates how to LED Toggle with Button Press Using MicroPython for ESP32 and ESP82666. The project focuses on digital read and digital write, where a button is used to control the state of an LED. Each press of the button toggles the LED, either turning it on or off. This is a simple yet essential project for understanding input and output control in MicroPython.

Data Types and Variable Table for LED Toggle with Button Press:

Data Type Variable Name Description Example Value
Boolean led_state Tracks the on/off state of the LED True or False
Pin (Output) led GPIO pin controlling the LED GPIO5
Pin (Input) button GPIO pin reading the button state GPIO14

The boolean data type is used to store the LED’s current state, while pins are used to control the LED and read the button’s state.

Syntax Table for LED Toggle with Button Press in MicroPython:

Operation Syntax Example
Set pin mode to output led = Pin(pin_number, Pin.OUT) led = Pin(5, Pin.OUT)
Set pin mode to input button = Pin(pin_number, Pin.IN) button = Pin(14, Pin.IN)
Read pin state (button press) button.value() if button.value() == 1:
Write pin state (LED control) led.value(state) led.value(1) (Turn LED on)

Required Components:

  • ESP32 or ESP8266
  • 1 LED
  • 1 Resistor (220Ω)
  • 1 Push Button
  • Breadboard
  • Jumper Wires

Circuit Diagram:

   Button        LED       ESP32/ESP8266

    ——-      —–      ————–

    One Pin     Anode      GPIO5 (Pin 5)

    Other Pin   Cathode    GND

    One Pin     GPIO14

    Other Pin   GND

 

Circuit Connection Table:

Component Pin ESP32/ESP8266 Pin Explanation
LED Anode Connect to GPIO5 GPIO5 Controls the LED on/off state
LED Cathode Connect to GND GND Ground connection for the LED
Button Pin 1 Connect to GPIO14 GPIO14 Reads the state of the button (pressed or not)
Button Pin 2 Connect to GND GND Ground connection for the button

Warnings:

  • Always use a 220Ω resistor with the LED to avoid burning it out due to excessive current.
  • Ensure proper wiring for the button and the LED to avoid incorrect readings or unexpected behavior.

Circuit Analysis:

The circuit involves an LED connected to GPIO5 and a button connected to GPIO14 on the ESP32/ESP8266. When the button is pressed, the state of the GPIO14 pin changes, and the LED toggles between on and off using GPIO5.

Installing MicroPython and Libraries:

Install MicroPython on your ESP32 or ESP8266 using Thonny or esptool:
esptool.py –chip esp32 erase_flash

esptool.py –chip esp32 write_flash -z 0x1000 esp32-20210902-v1.17.bin

  1. No Additional Libraries Required: The built-in machine library in MicroPython is sufficient for handling the button and LED.

Writing the MicroPython Code for LED Toggle with Button Press:

import machine

import time

 

# Initialize LED and Button

led = machine.Pin(5, machine.Pin.OUT)

button = machine.Pin(14, machine.Pin.IN)

 

# Variable to track LED state

led_state = False

 

while True:

    # Read the button state

    if button.value() == 1:

        # Toggle the LED state

        led_state = not led_state

        led.value(led_state)

        print(“LED is ON” if led_state else “LED is OFF”)

        

        # Wait for the button to be released (debouncing)

        time.sleep(0.3)

 

Running the Code and Checking the Output:

  1. Upload the code to your ESP32 or ESP8266 using Thonny or another MicroPython IDE.
  2. Press the button to toggle the LED’s state.
  3. Observe the console output: The message “LED is ON” or “LED is OFF” will be printed based on the LED’s state.

Explanation of the Code:

  • Pin Initialization: The LED is connected to GPIO5, and the button is connected to GPIO14.
  • State Tracking: The led_state variable tracks the current state of the LED (on or off).
  • Reading the Button: The button.value() function reads the button state. If pressed, the LED state is toggled using led.value().
  • Debouncing: The time.sleep(0.3) function ensures that only one toggle happens per button press.

Expanding the Project:

  • Multiple LEDs and Buttons: Add more buttons and LEDs to toggle multiple outputs.
  • Button Hold Functionality: Add functionality to detect a long press to control the LED’s behavior differently.
  • Integrate Sensors: Add sensors such as light or temperature sensors to control the LED in combination with button presses.

Common Problems and Solutions:

  1. Problem: The LED doesn’t toggle when the button is pressed.
    • Solution: Check the button connections and ensure the button is correctly debounced with the time.sleep() function.
  2. Problem: The LED flickers or toggles unexpectedly.
    • Solution: Adjust the debounce time in the code to ensure the button press is handled correctly.
  3. Problem: The code throws an error when reading the button state.
    • Solution: Ensure that the Pin.IN mode is set correctly for the button pin.

FAQ:

Q: Can I use different GPIO pins for the button and LED?
A: Yes, you can use any available GPIO pins on your ESP32 or ESP8266, but remember to update the pin numbers in your code.

Q: How can I modify the code to automatically turn off the LED after a certain period?
A: You can add a delay using time.sleep() and then set led.value(0) to turn off the LED after the delay.

Q: Can I use a different type of button for this project?
A: Yes, as long as the button is properly connected to the GPIO pin and ground.

Conclusion:

In this project, you learned how to toggle an LED with a button press using MicroPython for ESP32 and ESP8266. You now understand how to use digital read to detect button presses and digital write to control an LED. This project is a foundational step for more advanced projects involving buttons and LEDs, and can easily be expanded with additional functionality such as sensor inputs or multiple outputs.