In this tutorial we will control the lighting of an LED via a push button:
1- When the button is pressed, the LED lights up
2- When the button is released, the LED goes out.
We connect pin D23 of the ESP32 board to the anode (+ terminal) of the LED and the GND of the ESP32 board to the cathode (- terminal) of the LED.
We connect the first pin of the push button to the GND pin of the ESP32 board and the second pin of the push button to the D21 pin of the ESP32 board.
Here is the program that controls the ignition of an LED connected to pin D23 of the ESP32 board.
from machine import Pin
button = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_UP)
if not button.value(): # We press the button
led.value(1) # LA LED s’allume
else: # We release the button
led.value(0) #LED goes out