Reading Button Inputs with ESP32 Button Input and MicroPython

In this tutorial, we’ll demonstrate how to read ESP32 button input with MicroPython. Specifically, we’ll walk through a simple example where a button, connected to pin 15, is used to control an LED connected to pin 21. Pressing the button will toggle the LED on and off. This guide is a great starting point for building interactive projects with the ESP32.

The ESP32 is a powerful microcontroller, and MicroPython makes it easy to interface with various hardware components. By the end of this tutorial, you’ll know how to set up the button input, read its state, and use it to control an LED.


Table of Contents


Wiring the Button and LED

To get started, wire the components as follows:

  • Button: Connect one side of the button to pin 15 and the other side to GND.
  • LED: Connect the anode (positive leg) of the LED to pin 21 and the cathode (negative leg) to GND.

You can use a 10kΩ pull-down resistor between pin 15 and GND to ensure that the input is in a known state when the button is not pressed.

ESP32 breadboard setup showing a button on pin 15 with a pull-up resistor, controlling LED 21, along with additional LEDs connected to pins 19 and 18.
Breadboard setup with ESP32, button (pin 15), and LEDs (pins 21, 19, 18). Button (pin 15) is configured with a pull-up resistor to control LED 21.

What Are Pull-Up and Pull-Down Resistors?

When working with buttons and switches, it’s essential to understand the concept of pull-up and pull-down resistors. These resistors are used to define the state of an input pin when it is not actively being driven by a signal, ensuring that the pin doesn’t float and give unpredictable results.

Pull-Down Resistor:

A pull-down resistor connects an input pin to ground (GND) when the button is not pressed. This ensures that the input pin reads as 0 (LOW) in the absence of an active signal. When the button is pressed, the pin is connected to the supply voltage (e.g., 3.3V), and the input reads 1 (HIGH).

Use Case: A pull-down resistor is used when you want the default state of the pin to be 0 (LOW).

ESP32 breadboard setup showing a button on pin 15 with a pull-down resistor, controlling LED 21, along with additional LEDs connected to pins 19 and 18.
Breadboard setup with ESP32, button (pin 15), and LEDs (pins 21, 19, 18). Button (pin 15) is configured with a pull-down resistor to control LED 21.

Wiring: You connect one end of the button to the input pin and the other to GND. A pull-down resistor can be placed between the input pin and GND (although the ESP32 has internal pull-down resistors that can be activated in software, eliminating the need for an external resistor).


Pull-Up Resistor:

A pull-up resistor connects an input pin to the supply voltage (typically 3.3V or 5V). When the button is not pressed, the pin will read 1 (HIGH). When the button is pressed, the pin is connected to GND, and the input reads 0 (LOW).

Use Case: A pull-up resistor is used when you want the default state of the pin to be 1 (HIGH).

Wiring: You connect one end of the button to the input pin and the other to GND. A pull-up resistor can be placed between the input pin and the supply voltage, or you can enable the internal pull-up resistors in the ESP32, avoiding the need for an external resistor.

ESP32 breadboard setup showing a button on pin 15 with a pull-up resistor, controlling LED 21, along with additional LEDs connected to pins 19 and 18.
Breadboard setup with ESP32, button (pin 15), and LEDs (pins 21, 19, 18). Button (pin 15) is configured with a pull-up resistor to control LED 21.

In our example, we’ll use the internal pull-down resistor on pin 15 of the ESP32, meaning that when the button is not pressed, the input pin will be pulled to GND (LOW), and when the button is pressed, it will read HIGH.


Basic Button Input to Control LED

Now that the hardware is set up, let’s write a simple MicroPython script to read the button input and control the LED. The button will toggle the state of the LED each time it’s pressed.

ESP32 breadboard setup showing a button on pin 15 with a pull-up resistor, controlling LED 21, along with additional LEDs connected to pins 19 and 18.
Breadboard setup with ESP32, button (pin 15), and LEDs (pins 21, 19, 18). Button (pin 15) is configured with a pull-up resistor to control LED 21.
from machine import Pin
from time import sleep

# PINS
button = Pin(15, Pin.IN, Pin.PULL_UP)  # Button connected to pin 15
led = Pin(21, Pin.OUT)  # LED connected to pin 21

# Initial LED state (off)
led.off()

while True:
    if button.value() == 0:  # Check if button is pressed
        led.value(not led.value())  # Toggle the LED state
        sleep(0.2)  # Debounce delay to avoid multiple toggles

Explanation of the Code

  1. Button Pin Setup:
    • The button is connected to pin 15 and is configured as an input with an internal pull-down resistor (Pin.PULL_DOWN). This ensures the button input reads as low (0) when not pressed.
  2. LED Pin Setup:
    • The LED is connected to pin 21 and is set as an output pin (Pin.OUT).
  3. Button Press Detection:
    • The button.value() function reads the state of the button. When the button is pressed, it returns 1. We check for this in the if statement to detect the button press.
  4. LED Toggling:
    • The LED state is toggled using the led.value(not led.value()) statement. This flips the LED state between 0 (off) and 1 (on) each time the button is pressed.
  5. Debouncing:
    • A short sleep(0.2) delay is added to debounce the button, ensuring that the LED only toggles once per press, even if the button’s mechanical bounce causes multiple inputs.

For more advanced functionality, you can modify the script to blink the LED a set number of times when the button is pressed. Here’s an expanded version:

from machine import Pin
from time import sleep

# PINS
button = Pin(15, Pin.IN, Pin.PULL_DOWN)  # Button connected to pin 15
led = Pin(21, Pin.OUT)  # LED connected to pin 21

# Blink LED when button is pressed
def blink_led(times):
    for _ in range(times):
        led.on()
        sleep(0.5)
        led.off()
        sleep(0.5)

while True:
    if button.value() == 1:  # Button is pressed
        blink_led(3)  # Blink LED 3 times
        sleep(1)  # Wait before detecting another press

In this version, pressing the button will make the LED blink three times. This could be adapted for more complex actions like controlling multiple LEDs or triggering other devices.

Conclusion

In this guide, we’ve learned how to read ESP32 button input using MicroPython. By connecting a button to pin 15 and an LED to pin 21, we can use a button press to toggle the LED on and off. We also expanded the example to blink the LED multiple times. This simple functionality forms the foundation for interactive projects, such as home automation systems or user interfaces for embedded devices.

Leave a Reply

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