PyRasp

A Pi talks to a Pico over UART

A Raspberry Pi is good at thinking (files, networks, a full Python); a Pico is good at doing (exact timing, analog inputs, starting instantly). A common pattern on real benches is to let the Pi decide and the Pico act, talking over a UART: two wires and a ground. Here the Pi asks the Pico to blink an LED a number of times and waits for the Pico to say it is done.

This board has two controllers. See Several boards for how programs and consoles work with more than one.

Parts: a Pi, a Pico, a 330 Ω resistor and an LED.

The circuit

UART lines cross: what one board sends (TX) goes to what the other receives (RX). The Pi's TXD is GPIO14 and its RXD GPIO15; the Pico's UART0 uses GP0 (TX) and GP1 (RX). The ground wire matters: a level only means something relative to a shared ground.

# circuit.py
from wiring import *

pi = Pi()
pico = Pico(label="pico")

pi.TXD >> pico.GP1      # Pi transmits, Pico receives
pico.GP0 >> pi.RXD      # Pico transmits, Pi receives
pi.GND >> pico.GND      # a common ground

pico.GP15 >> Resistor(330) >> LED("green") >> pico.GND

The Pi's program: main.py

The Pi uses pyserial, as on a real Pi (/dev/serial0). Each command is one line of text; the answer is one line back. A command sent before the Pico has opened its UART is simply lost, so the Pi sends it again until it gets an answer.

# main.py — runs on the Pi, in Python
import serial, time

port = serial.Serial("/dev/serial0", 115200, timeout=2)

def ask(command):
    """Sends one command line and returns the Pico's one-line answer."""
    for attempt in range(5):
        port.write(f"{command}\n".encode())
        reply = port.readline().decode(errors="ignore").strip()
        if reply:
            return reply
    return "(no answer)"

for blinks in (1, 2, 3):
    print(f"asked for {blinks} blinks, Pico says: {ask(f'BLINK {blinks}')}")
    time.sleep(0.5)

The Pico's program: pico/main.py

The Pico waits for a line, carries it out, then answers. It compares bytes rather than decoding text, so a garbled line (sent before both sides agreed on the baud rate) is ignored rather than crashing it.

# pico/main.py — runs on the Pico, in MicroPython
from machine import UART, Pin
import utime

uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
led = Pin(15, Pin.OUT)

while True:
    line = uart.readline()
    if not line:
        utime.sleep_ms(5)
        continue
    words = line.split()
    if len(words) == 2 and words[0] == b"BLINK" and words[1].isdigit():
        count = int(words[1])
        for _ in range(count):
            led.on()
            utime.sleep_ms(150)
            led.off()
            utime.sleep_ms(150)
        uart.write(b"DONE %d\n" % count)
        print("blinked", count)

Press Run. The pi console tab shows asked for 3 blinks, Pico says: DONE 3, the pico tab shows each blinked line, and All interleaves both.

Going deeper

  • Both sides must agree on the baud rate. Change the Pico's to 9600 and the Pi receives garbage, as it would on the bench.
  • Why a protocol at all? UART only moves bytes. Ending each message with a newline, and answering every command, is what lets each side know where one message ends and that it was heard.
  • 5 V boards: a Pi and a Pico are both 3.3 V, so they connect directly. An Arduino Uno is 5 V and would need a level shifter on its TX line.