UART Loopback example script (arduino.py) Sends a numbered message out of UART0 and reads it back, twice a second.
Arduino's Serial1 has no match here, so this uses mcu.uart() from phyduino.py.
- Wiring
- Connect a jumper wire from D1 (TX) to D0 (RX) first; without it, nothing comes back.
- Output
sent 'hello 0', received 'hello 0'
from arduino import *
import phyduino as mcu
serial = None
count = 0
def setup():
global serial
serial = mcu.uart(0, 115200)
def loop():
global count
message = "hello %d" % count
serial.write(message)
delay(50)
received = serial.read()
print("sent %r, received %r" % (message, received.decode() if received else None))
count += 1
delay(500)
def main():
setup()
while True:
loop()
if __name__ == "__main__":
main()