5101+ reviews
Order by 16:00 for same day shipping
14 days return
EN
Individual
Business
In this project you will learn two ways to use a pushbutton:
GPIO | Pin # | Function | Connection |
GPIO 17 | Pin 11 | Digital input | Push button |
GPIO 27 | Pin 13 | Digital output | LED |
GND | Pin 6 | Earth (Ground) | Push button & LED |
from gpiozero import LED, Button
from signal import pause
# LED en drukknop koppelen aan GPIO-pinnen
led = LED(27) # LED op GPIO 27
button = Button(17) # Drukknop op GPIO 17
# Actie koppelen: LED aan bij knop indrukken
button.when_pressed = led.on
button.when_released = led.off
print("Druk op de knop om de LED te bedienen!")
pause() # Houd het programma actief
2. Save the file: Click on File > Save As and name the file button_led_digital.py.
3. Run the script: Click on the green Run button (▶) at the top of the Thonny interface.
Pin | Function | Connection |
Pin 1 | 3.3V Power | One side of the push button |
Pin 6 | Earth (Ground) | LED cathode |
from gpiozero import LED, Button
from time import sleep
led = LED(27)
button = Button(17)
while True:
if button.is_pressed:
led.on()
sleep(0.5)
led.off()
sleep(0.5)
else:
led.off()
2. Combining analog and digital: