How-to: Reading the REPL output of a Raspberry Pi Pico on a PC via USB

With my Raspberry Pi cactus, I migrated my Home Assistant server to a second hand “thin client” PC I got off eBay. But how could I get data from my Piicodev atmospheric/air quality sensors on the PC?

Sure, Core stock a USB to I2C adapter with Qwiic, but I had no desire to reverse engineer the Piicodev libraries to get it to work - I wouldn’t know where to start.

I could use a Pi Pico W, and output the sensor to data over WiFi to an MQTT broker on the PC, but I wanted something simpler and more reliable.

What if, I were to connect a Raspberry Pi Pico via USB to the PC, print sensor data to the REPL, and then simply have a Python script on the PC read the REPL output over the USB connection?

It works! Here’s my code to the read the REPL on the PC. Note the name of the Raspberry Pi Pico is something like Board CDC.

from serial import Serial
from import list_ports
from time import sleep
from ast import literal_eval

def get_serial_device(name):
    ports = list(list_ports.comports())
    for port in ports:
        if name in port.description:
           return Serial(port.device)

pico = get_serial_device('Board CDC')

while True:
    data = literal_eval(pico.readline().decode().rstrip('\n'))
    # print(data)

My pico simply prints my readings as a dictionary to the REPL e.g. {'tempC': 26, 'humRH': 42}, etc.

From scouring the internet, it’s possible to go the other way (send data from the PC to the Pico, but I’m yet to try it - looks more involved but I’ll report back if I can get it to work.

Hope this “hack” is helpful for some of you - especially since fully fledged Pis are impossible to get right now.

@Michael In the future, would Core consider making a Piicodev USB adapter so we can program our favourite Piicodev modules on a PC?

1 Like

Hi Steven,

Really neat guide!

This sounds interesting, what kind of applications could you use it for?
Its a shame that you don’t see dedicated IO on computers anymore (RS232 UART or Pins like the RPi)

No use case any projects in mind, but ny thinking was given the chip shortage, it might open up more opportunities to use the Piicodev range.