Digital Read Serial example script (arduino.py) Prints the state of a pushbutton on D2, 1 when pressed and 0 when not, ten times a second.
- 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(BUTTON_PIN, INPUT_PULLDOWN)
def loop():
print(digitalRead(BUTTON_PIN))
delay(100)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()