Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Last active June 3, 2023 19:04
Show Gist options
  • Select an option

  • Save DavesCodeMusings/c4843a60348692408a0dbdd5436f7ee7 to your computer and use it in GitHub Desktop.

Select an option

Save DavesCodeMusings/c4843a60348692408a0dbdd5436f7ee7 to your computer and use it in GitHub Desktop.
Use the BOOT button on ESP boards as a regular input.
# After booting, the BOOT button attached to GPIO 0 is available to use as a regular input for your program.
# GPIO 9 is used for BOOT on the ESP32-C3. Check the Espressif docs for other ESP variations.
from machine import Pin
p0 = Pin(0, Pin.IN, Pin.PULL_UP) # Normal state is logic high, pressing the button will pull it low.
pressed = False # Flag to track if button was pressed.
def irq_handler(pin):
global pressed
pressed = True
p0.irq(trigger=Pin.IRQ_FALLING, handler=irq_handler) # Trigger on high to low (falling) level transition.
while True:
if (pressed == True):
print('Button press detected.')
# Do something useful here.
pressed = False # Clear the flag to be ready for the next press.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment