Button example script (arduino.py) Lights the on-board LED while a pushbutton on D2 is pressed.
- Wiring
- A pushbutton between D2 and 3.3 V. The pin's pull-down resistor holds D2 at 0 while the button is open.
from arduino import *
BUTTON_PIN = 2
def setup():
pinMode(LED_BUILTIN, OUTPUT)
pinMode(BUTTON_PIN, INPUT_PULLDOWN)
def loop():
if digitalRead(BUTTON_PIN) == HIGH:
digitalWrite(LED_BUILTIN, HIGH)
else:
digitalWrite(LED_BUILTIN, LOW)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()