Saturday, January 30, 2021

Raspberry Pi Pico/MicroPython: simple example of UART

MicroPython UART class implements the standard UART/USART duplex serial communications protocol. At the physical level it consists of 2 lines: RX and TX. 

To check the default setting of UART on Raspberry Pi Pico, enter the code in MicroPython REPL:

>>> import machine
>>> machine.UART(0)
>>> machine.UART(1)

It's found that the default GPIO assigned to UART(1) is tx=4, rx=5.

Mark a jump to short between GP4 (pin 6) and GP5 (pin 7).

Run the exercise code, mpyPico_uart.py
import os
import utime
import machine

#print sys info
print(os.uname())

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(0)     # onboard LED OFF for 0.5 sec
utime.sleep(0.5)
led_onboard.value(1)

#print uart info
uart = machine.UART(1)
print(uart)

uart.write("hello")
utime.sleep(0.1)
while uart.any():
    print(uart.read(1))

print()
print("- bye -")






Tried similar with Adafruit CircuitPython 6.2.0-beta.1 firmware installed, it report NotImplementedError: UART not yet supported.



No comments: