For more details you can see this article: https://www.robotique.site/tutorial/l...
To control an LED with a push button using an ESP32 and MicroPython, you can follow these steps:
1- Connect the LED to one of the GPIO pins of the ESP32. For example, you can connect the positive (anode) leg of the LED to GPIO 4 and the negative (cathode) leg of the LED to a ground pin on the ESP32.
2- Connect the push button to another GPIO pin of the ESP32. For example, you can connect one leg of the push button to GPIO 5 and the other leg to a ground pin on the ESP32.
3- Write a MicroPython code to control the LED based on the state of the push button. Here's an example code:
from machine import Pin
led = Pin(421 Pin.OUT)
button = Pin(23, Pin.IN)
while True:
if button.value() == 1:
led.value(1)
else:
led.value(0)
In this code, we first create Pin objects for the LED and the push button using their respective GPIO pins. We set the LED pin as an output pin and the button pin as an input pin.
We then enter an infinite loop where we check the state of the push button using its value method. If the button is pressed (value is 1), we turn on the LED by setting its value to 1. If the button is not pressed (value is 0), we turn off the LED by setting its value to 0.
Upload the code to the ESP32 board using a MicroPython IDE or the REPL (Read-Evaluate-Print Loop) interface.
Test the circuit by pressing and releasing the push button. The LED should turn on and off accordingly.