KevsRobots Learning Platform
By Kevin McAleer, 2 Minutes
In this lesson, you’ll learn how to use UART (Universal Asynchronous Receiver/Transmitter) to send data between your Pico and other devices. UART is used by GPS modules, Bluetooth modules like the HC-05, and to link two microcontrollers together.
UART sends data over two wires: TX (transmit) and RX (receive). It is asynchronous, which means there’s no shared clock wire. Instead, both devices agree on a speed, called the baud rate (for example 9600 or 115200 bits per second).
Wire them crossed over:
TX goes to the other device’s RXRX goes to the other device’s TXGND goes to GNDThe Pico has two UARTs, UART0 and UART1, and each can use several pairs of pins (see the pinout). UART0 on GP0 (TX) and GP1 (RX) is the usual choice:
from machine import UART, Pin
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
uart.write("Hello, world!\n")
if uart.any(): # is there anything waiting?
data = uart.read() # read all of it, as bytes
print(data)
uart.any() returns how many bytes are waiting, so you only read when there’s something there.
Connect GP0 to GP1 with a single jumper wire (a “loopback”). Write a message and read it straight back.
Quick quiz 1 of 2
How should TX and RX be wired between two UART devices?
Quick quiz 2 of 2
What must both devices agree on for UART to work?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments