The goal is to turn the LED on when a button is pressed. While the button is being held down, the LED is on. When the button is released, the LED is off.
1 Metro M0 Express
1 breadboard
7 wires
2 resistors (330 ohms)
1 LED light
1 push button
Follow the circuit diagram provided to wire the Metro M0 Express.
4 pin push button
2 pin push button
import board
from digitalio import DigitalInOut, Direction, Pull
import time
led = DigitalInOut(__________ )
button = DigitalInOut(__________ )
led.direction = Direction.__________
button.direction = Direction.__________
button.pull = Pull.UP # Pull: defines the pull of a digital input pin
buttonState = False
while True:
buttonState = button.value
if buttonState:
# button is pressed and led is currently on
led.value = __________
else:
# button is pressed and led is currently off
led.value = __________
time.sleep(0.01)
import board
from digitalio import DigitalInOut, Direction, Pull
import time
led = DigitalInOut(board.D13)
button = DigitalInOut(board.D2)
led.direction = Direction.OUTPUT
button.direction = Direction.INPUT
button.pull = Pull.UP # Pull: defines the pull of a digital input pin
buttonState = False
while True:
buttonState = button.value
if buttonState:
# button is pressed and led is currently on
led.value = True
else:
# button is pressed and led is currently off
led.value = False
time.sleep(0.01)