Controlling LEDs with ESP32 Button Combination Using MicroPython

Learn how to control 1-3 LEDs on the ESP32 with an ESP32 button combination. This guide walks you through the steps of programming button combinations to trigger LED patterns using MicroPython.


In this video, we’re demonstrating how to control LEDs using an ESP32 button combination. Watch as we walk you through the circuit setup on a breadboard, where three buttons are connected to GPIO pins 15, 4, and 5, and three LEDs are connected to GPIO pins 21, 19, and 18. By pressing different combinations of buttons, we’ll trigger various LED patterns, showcasing the power of MicroPython for real-time control. This project is perfect for beginners looking to experiment with GPIO pins and explore the potential of the ESP32 for interactive applications. Follow along to see how button combinations can create dynamic lighting effects!

Controlling LEDs with an ESP32 button combination is a creative way to interact with the microcontroller. By pressing specific button combinations, users can control one or more LEDs to create custom patterns. Using MicroPython, this project becomes highly accessible, allowing beginners and experienced developers to experiment with GPIO control easily. In this guide, we’ll explore how to trigger various LED patterns using the ESP32 by pressing different combinations of buttons.

This project involves configuring three buttons and three LEDs connected to the ESP32. Each button will toggle an LED’s state, and pressing multiple buttons simultaneously will create distinct lighting effects. This approach opens the door to a wide range of possibilities, offering flexibility for users to develop advanced interactive projects.


Table of Contents


Setting Up the ESP32 with MicroPython

Before diving into the code, ensure that MicroPython is installed on your ESP32 board. You can use an IDE like Thonny to upload and run scripts directly. Once your ESP32 is ready, wire three buttons to GPIO pins 15, 4, and 5 for button inputs, and connect three LEDs to GPIO pins 21, 19, and 18 for output. We’ll use the internal pull-up resistors for the buttons, which simplifies the circuit and reduces wiring complexity.

Breadboard wiring for an ESP32 project, showing buttons connected to GPIO pins 15, 4, and 5, and LEDs connected to GPIO pins 21, 19, and 18, demonstrating a simple button-LED control setup.
ESP32 button and LED setup on a breadboard, featuring three buttons connected to GPIO pins 15, 4, and 5, and three LEDs connected to GPIO pins 21, 19, and 18.

Writing the MicroPython Script

The core of the project lies in the MicroPython script that reads the button states and controls the LEDs based on the ESP32 button combination. Below is an example script to get started:

from machine import Pin, Timer
from time import sleep

# Buttons
btn1 = Pin(15, Pin.IN, Pin.PULL_UP)
btn2 = Pin(4, Pin.IN, Pin.PULL_UP)
btn3 = Pin(5, Pin.IN, Pin.PULL_UP)

# LEDs
red = Pin(21, Pin.OUT)
green = Pin(19, Pin.OUT)
yellow = Pin(18, Pin.OUT)

def read_button(t):
    red.value(not btn1.value())
    green.value(not btn2.value())
    yellow.value(not btn3.value())
    print(btn1.value(), btn2.value(), btn3.value())

timer1 = Timer(1)
timer1.init(period=200, mode=Timer.PERIODIC, callback=read_button)


This script uses the Timer module to periodically check the state of the buttons and toggle the LEDs. When a button is pressed, its corresponding LED turns on, and pressing different combinations of buttons will light up multiple LEDs. This behavior can be modified to suit more complex patterns as desired.


How ESP32 Button Combinations Work

The idea behind ESP32 button combination control is that pressing multiple buttons simultaneously will trigger specific LED patterns. When a button is pressed, it toggles its respective LED between on and off. The combination of button presses determines which LEDs light up and when. For example, pressing btn1 and btn2 together could make the red and green LEDs light up, while pressing btn2 and btn3 could make the green and yellow LEDs turn on.

As you get comfortable with the setup, you can explore more complex combinations to create even more intricate lighting effects. The ESP32’s versatility allows for scaling the project with additional buttons and LEDs if needed.


Conclusion

Using an ESP32 button combination to control LEDs offers an engaging and hands-on way to experiment with GPIO pin control and programming in MicroPython. By pressing different combinations of buttons, you can trigger various LED patterns and develop interactive projects. This approach is ideal for beginners wanting to dive into embedded systems or for experienced developers looking for a simple yet effective way to create real-time control systems.

As you expand your skills with the ESP32, the possibilities for button combination control grow. Whether you’re creating simple on/off patterns or more complex visual effects, the ESP32 button combination provides an excellent foundation for building exciting, interactive electronics projects.

Leave a Reply

Your email address will not be published. Required fields are marked *