MicroPython UART loopback on a Pico: GP8 to GP1
MicroPython on a Raspberry Pi Pico: text typed in the Serial Monitor goes out of UART1 on GP8 and is read back by UART0 on GP1, over two jumpers. The whole build, a Raspberry Pi Pico, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4. On Mokxi they're free.
What you'll learn
- What a UART is, and TX and RX
- Crossing TX to RX between two ports
- Baud rate and how long bytes take
- Testing a serial link with a loopback
- Difficulty: Intermediate
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 1
How it works
A UART is a serial port. The Pico has two. Here two jumper wires connect them to each other: UART1's TX (GP8) goes to UART0's RX (GP1), and UART0's TX (GP0) goes to UART1's RX (GP9). Everything you send out of one comes into the other.
The program asks you to type something in the Serial Monitor. It clears UART0, sends your text out of UART1 with a newline, waits a moment for it to arrive at 9600 baud, then reads it from UART0. It prints what came in, keeping only printable characters.
Parts list
1 part, plus the jumper wires. They retail for about $4; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
Kits that can build it
These 4 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Raspberry Pi Pico pin GP9 | Raspberry Pi Pico pin GP0 |
| Raspberry Pi Pico pin GP8 | Raspberry Pi Pico pin GP1 |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Serial read and write between the Pico's two UARTs. Two jumpers join
# UART1's TX (GP8) to UART0's RX (GP1), and UART0's TX (GP0) to UART1's RX
# (GP9). What you type in the Serial Monitor goes out of UART1 and comes
# back in on UART0.
from machine import Pin, UART
import time
uart0 = UART(0, 9600, tx=Pin(0), rx=Pin(1))
uart1 = UART(1, 9600, tx=Pin(8), rx=Pin(9))
while True:
text = input("Type something and press Enter: ")
uart0.read() # empty UART0 of anything left over
uart1.write(text + "\n")
time.sleep_ms(20 + len(text)) # 9600 baud is about a byte a millisecond
got = uart0.read() or b""
# keep the printable characters: a line can pick up a stray byte as a UART starts
print("UART0 received:", "".join(chr(c) for c in got if 32 <= c < 127))
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- UART0 received prints nothing This project
Check the jumpers: GP8 to GP1, and GP0 to GP9. TX always goes to the other side's RX. - The text comes back garbled This project
Both UARTs use 9600 baud in the code. Keep them the same. - The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.