You need three file: On your pi, boot.py (Note you have reboot the pi to run this) This turns on the data serial port

import storage
import usb_cdc

usb_cdc.disable()   # Disable both serial devices.

usb_cdc.enable(console=True, data=True)

On your pi. This runs the code and has it send text to the serial port

# usb_io for  Raspberry Pi Pico with rp2040

import supervisor
import usb_cdc
import time

while not supervisor.runtime.usb_connected:
    pass

ser = usb_cdc.data

def usb_writeline (usb_data_port, x):
    print (str(x))
    usb_data_port.write(bytes(str(x) + "\n", "utf-8"))
    usb_data_port.flush()

while True:
    usb_writeline(ser, 'Grahammers')

On your laptop

This code reads the serial port

# usb_io.py

import serial
import adafruit_board_toolkit.circuitpython_serial
import select

ser      = serial.Serial( None, 115200, 8, "N", 1, timeout=120)
ser.port = "COM5"

ser.open()

s = ser.readline()
print(s)
Posted in

Leave a Reply


Cosmic Pathways, Lab for Kids, and many of the other research activities discussed on this website is supported by the National Science Foundation and the Physics Teacher Education Coalition (PhysTEC) under grant no. 2325980. Any opinions, findings, and conclusions, or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.