Build it step by step
0 of 7 doneLoad MicroPython on the Pico
With Thonny.
Copy main.py to the Pico
The first file.
Enable the Pi serial port
raspi-config.
Wire TX to RX, RX to TX and ground
Cross the data wires.
Run bridge.py on the Pi
In a virtual environment.
Open the page
http://pi-address:8000.
Switch the LED
With curl -X POST .../led/on.
How do you build a pi plus Pico co-processor?
A Pico handles real-time sensing and a Pi 5 handles the web page and database, linked by UART. You need Raspberry Pi 5, Raspberry Pi Pico H, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi 27 W USB-C power supply and Jumper wires male to female (40 pcs). It takes 7 steps and about 180 minutes.
- Level
- Intermediate
- Time
- About 180 min
- Parts
- 5
Wiring
| From | To | Why |
|---|---|---|
| Pico GP0 (UART0 TX) | Pi 5 GPIO15 (RXD, pin 10) | Readings to the Pi |
| Pico GP1 (UART0 RX) | Pi 5 GPIO14 (TXD, pin 8) | Commands from the Pi |
| Pico GND | Pi GND (pin 6) | Common ground (both are 3.3 V logic) |
| Pico USB | Power only, from a separate source or the Pi USB port | Keep one power source for the Pico at a time |
| Pi 5 | Power supply and SD card | Raspberry Pi OS |
Code
A Linux board such as the Pi is good at networks, databases and web pages but cannot promise exact timing, and a microcontroller such as the Pico does the opposite, so each does what it is best at. The two talk over a UART with simple text lines of JSON. The Pico code is MicroPython; copy it to the Pico as main.py with Thonny. The Pi side needs the serial port turned on: run sudo raspi-config, then Interface Options, Serial Port, answer No to a login shell and Yes to the serial hardware. Both boards are 3.3 V, so wire them directly, and join the grounds. The Pico here reads its built-in temperature sensor so no extra sensor is needed; swap in your own sensor later. The example web page has no login, so use it only on a home network and never open it to the internet. Raspberry Pi OS (Bookworm or newer) with Python 3.
# ===== File 1: main.py on the Pico (MicroPython) =====
# Sends the Pico's internal temperature as a JSON line every 2 seconds and switches the onboard LED on commands from the Pi.
from machine import UART, Pin, ADC
import time, json
uart = UART(0, baudrate=115200, tx=Pin(0), rx=Pin(1))
led = Pin("LED", Pin.OUT) # Pin("LED") works on the Pico and Pico W
sensor = ADC(4) # internal temperature sensor
def read_temp():
v = sensor.read_u16() * 3.3 / 65535
return 27 - (v - 0.706) / 0.001721
last = time.ticks_ms()
buf = b""
while True:
if uart.any():
buf += uart.read()
while b"\n" in buf:
line, buf = buf.split(b"\n", 1)
cmd = line.strip()
if cmd == b"LED ON": led.value(1)
elif cmd == b"LED OFF": led.value(0)
if time.ticks_diff(time.ticks_ms(), last) > 2000:
last = time.ticks_ms()
uart.write(json.dumps({"temp_c": round(read_temp(), 1), "t": time.time()}) + "\n")
# ===== File 2: bridge.py on the Pi 5 (needs: pip install pyserial flask in a virtual environment) =====
import serial, json, sqlite3, threading, time
from flask import Flask, jsonify
port = serial.Serial("/dev/serial0", 115200, timeout=1)
db = sqlite3.connect("readings.db", check_same_thread=False)
db.execute("CREATE TABLE IF NOT EXISTS r (ts REAL, temp REAL)")
def reader():
while True:
line = port.readline().decode(errors="ignore").strip()
if not line:
continue
try:
d = json.loads(line)
db.execute("INSERT INTO r VALUES (?, ?)", (time.time(), d["temp_c"]))
db.commit()
except (ValueError, KeyError):
pass # ignore half lines
threading.Thread(target=reader, daemon=True).start()
app = Flask(__name__)
@app.route("/")
def latest():
rows = db.execute("SELECT ts, temp FROM r ORDER BY ts DESC LIMIT 20").fetchall()
return jsonify(rows)
@app.route("/led/<state>", methods=["POST"])
def led(state):
port.write(b"LED ON\n" if state == "on" else b"LED OFF\n")
return "ok"
app.run(host="0.0.0.0", port=8000)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Pico running | Pi receives a line every 2 seconds |
| Database | Grows by one row per reading |
| Web page | Lists the latest 20 readings |
| LED command | Pico LED switches |
If it does not work
Nothing arrives
Swap TX and RX and check the shared ground.
Garbled text
Check both use 115200 baud.
Permission denied on serial
Add your user to the dialout group.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pi plus Pico co-processor?
Raspberry Pi 5, Raspberry Pi Pico H, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi 27 W USB-C power supply and Jumper wires male to female (40 pcs).
How long does the pi plus Pico co-processor take to build?
About 180 minutes for a intermediate maker, in 7 steps.
How do I know the pi plus Pico co-processor works?
Pico running: Pi receives a line every 2 seconds. Database: Grows by one row per reading. Web page: Lists the latest 20 readings.
Nothing arrives: what should I check?
Swap TX and RX and check the shared ground.
Garbled text: what should I check?
Check both use 115200 baud.
Permission denied on serial: what should I check?
Add your user to the dialout group.
