ESP32 Toggle Button: Control LED States with a Single Press

In this ESP32 toggle button tutorial, we will walk you through the process of using a button to toggle an LED’s state between ON and OFF with each press. By leveraging ESP32 toggle button functionality in MicroPython, we can easily program the ESP32 to detect button presses and change the state of an LED accordingly. This tutorial is perfect for those who are new to MicroPython programming and want to learn how to interact with hardware in a simple yet effective way.

The main goal is to teach you how to use a single button and a single LED to create a toggle effect. Whether you are just starting with the ESP32 or you want to refresh your knowledge of button inputs and LED control, this step-by-step guide will provide clear instructions and code examples. You’ll also learn how to handle input debouncing to ensure the button is responsive without triggering multiple actions for a single press.


Table of Contents


Components Needed

  • ESP32 development board
  • 1 LED connected to pin 21 (or your preferred GPIO pin)
  • 1 Button connected to pin 15 (or your preferred GPIO pin)
  • Breadboard and jumper wires

Wiring the Components

Here’s how to wire up the ESP32, button, and LED for this tutorial:

  • Button: One side of the button is connected to pin 15 and the other side to GND.
  • LED: The anode (long leg) of the LED is connected to pin 21, and the cathode (short leg) is connected to GND.
  • Optional: Use a 10kΩ pull-up or pull-down resistor to ensure proper button input handling.

Program to Toggle LED with Button Press

In this example, we will use a button to toggle the state of an LED connected to pin 21. Each press of the button will toggle the LED on and off. We will track the button’s state with a simple counter that changes the LED’s state.

Here’s the MicroPython code for this:

from machine import Pin
from time import sleep

# PINS
led1 = Pin(21, Pin.OUT)  # LED connected to pin 21
btn1 = Pin(15, Pin.IN, Pin.PULL_UP)  # Button connected to pin 15 with pull-up resistor

# State tracking variable
button_state = 0
led_state = 0

while True:
    # Read the current button state
    state = btn1.value()
    
    # Check if the button is pressed
    if state == 0:  # Button pressed (LOW due to pull-up)
        # Toggle the LED state
        led_state = not led_state
        if led_state:
            led1.on()  # Turn LED on
        else:
            led1.off()  # Turn LED off
        
        # Wait a bit to debounce the button
        sleep(0.2)
    sleep(0.1)

Explanation of the Code

  1. Pin Setup:
    • led1 = Pin(21, Pin.OUT): This sets pin 21 as an output pin for the LED.
    • btn1 = Pin(15, Pin.IN, Pin.PULL_UP): This sets pin 15 as an input for the button, with an internal pull-up resistor. This means the button will read as LOW when pressed.
  2. State Tracking:
    • button_state tracks the button’s state.
    • led_state tracks whether the LED is ON (1) or OFF (0).
    • The state of the LED toggles each time the button is pressed.
  3. Button Press Logic:
    • If the button is pressed (state == 0), the led_state toggles between 1 (LED ON) and 0 (LED OFF).
    • The LED is turned on with led1.on() and turned off with led1.off().
  4. Debouncing:
    • The sleep(0.2) line helps debounce the button by delaying for a short period. This ensures the LED doesn’t toggle multiple times for a single press.
  5. Main Loop:
    • The loop continuously checks the button’s state, toggles the LED accordingly, and waits briefly before checking again.

Adding More LEDs

If you want to control multiple LEDs using the same button, you can modify the code to toggle between several LEDs as shown in the following example:

from machine import Pin
from time import sleep

# PINS
led1 = Pin(21, Pin.OUT)  # LED 1 connected to pin 21 (RED)
led2 = Pin(19, Pin.OUT)  # LED 2 connected to pin 19 (GREEN)
led3 = Pin(18, Pin.OUT)  # LED 3 connected to pin 18 (BLUE)
btn1 = Pin(15, Pin.IN, Pin.PULL_UP)  # Button connected to pin 15

# State tracking variable
counter = 0

while True:
    # Read the button state
    state = btn1.value()

    if state == 0:  # Button pressed
        counter += 1  # Increment the counter
        
        if counter == 1:
            led1.on()  # Turn on LED 1
            led2.off()  # Turn off LED 2
            led3.off()  # Turn off LED 3
        elif counter == 2:
            led1.off()  # Turn off LED 1
            led2.on()   # Turn on LED 2
            led3.off()  # Turn off LED 3
        elif counter == 3:
            led1.off()  # Turn off LED 1
            led2.off()  # Turn off LED 2
            led3.on()   # Turn on LED 3
        else:
            led1.off()  # Turn off LED 1
            led2.off()  # Turn off LED 2
            led3.off()  # Turn off LED 3
            counter = 0  # Reset the counter

        # Debounce the button
        sleep(0.2)

In this example, each button press toggles between three LEDs connected to pins 21, 19, and 18. After cycling through the LEDs, the counter resets, and the cycle starts again.

Conclusion

In this ESP32 toggle button tutorial, we have demonstrated how to use a single button press to toggle the state of an LED. The code uses a simple counter and state tracking to switch the LED between ON and OFF. This provides a foundation for more advanced control systems, such as multi-button interfaces or sequential LED lighting. With the addition of debouncing, you can ensure that the button is responsive while avoiding multiple triggers for a single press.

By understanding how to implement the ESP32 toggle button functionality in MicroPython, you now have the tools to build interactive projects with buttons and LEDs. This technique can be adapted for various other hardware components, offering endless possibilities for expanding your ESP32 projects.

Leave a Comment