Skip to content

Instantly share code, notes, and snippets.

@extrasleepy
Created February 4, 2026 14:43
Show Gist options
  • Select an option

  • Save extrasleepy/f049d7fb2e2d4dc80c7e09e155d9701b to your computer and use it in GitHub Desktop.

Select an option

Save extrasleepy/f049d7fb2e2d4dc80c7e09e155d9701b to your computer and use it in GitHub Desktop.
from gpiozero import Button, LED
from signal import pause
import time
# Define button on GPIO pin 19 and LED on GPIO pin 17
button = Button(19)
led = LED(17) # Change this pin number to match your LED connection
# Counter to track which state we're in (0, 1, or 2)
state = 0
print('start')
# Function to call when button is pressed
def on_button_press():
global state
if state == 0:
# First press: Turn LED on solid
print('State 1: LED ON')
led.on()
state = 1
elif state == 1:
# Second press: Blink LED
print('State 2: LED BLINKING')
led.blink(on_time=0.5, off_time=0.5)
state = 2
elif state == 2:
# Third press: Turn LED off
print('State 3: LED OFF')
led.off()
state = 0
time.sleep(0.2) # debounce delay
# Attach the function to the button press event
button.when_pressed = on_button_press
# Keep the program running to listen for button presses
pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment