Video Tutorial (Optional)
Watch first if you want to see the Raspberry Pi Pico serial file transfer workflow end to end.
Project Overview
Raspberry Pi Pico serial data transfer: You will send data from a local computer to a Raspberry Pi Pico over USB serial and save the incoming values to a CSV file on the Pico using Python scripts on both sides.
This is a practical pattern for IoT and embedded workflows where you need to stream or batch-transfer values from a PC to a microcontroller for storage and later analysis.
Subscribe: Youtube
Support: https://www.buymeacoffee.com/mmshilleh
- Time: 20 to 40 minutes
- Skill level: Beginner to Intermediate
- What you will build: A PC-to-Pico serial sender and a Pico receiver that writes received values into data.csv
Parts List
From ShillehTek
- None required for this software-focused tutorial
External
- Raspberry Pi Pico
- USB cable for the Pico (data-capable)
- Local computer with Python installed
- PySerial library (Python package)
- Thonny IDE (used to write/save code to the Pico)
Note: The PC Python script and Thonny cannot use the same serial (COM) port at the same time. Save the Pico script to main.py so it auto-runs after a power cycle, then close Thonny before running the PC sender script.
Step-by-Step Guide
Step 1 - Create the local computer sender script (PySerial)
Goal: Send a stream of values from your computer to the Raspberry Pi Pico over USB serial.
What to do: Create a Python script on your computer that opens the Pico serial port at 115200 baud, then sends values 1 through 1000 as comma-separated text.
Code:
import serial
import time
# Configure the serial connection
port = "/dev/cu.usbmodem11201" # Adjust the port to match your setup
baudrate = 115200
serial_connection = serial.Serial(port, baudrate)
# Read and write data until the transfer is complete
for i in range(1, 1001):
print(i)
serial_connection.write((str(i) + ',').encode())
time.sleep(0.01)
time.sleep(10)
serial_connection.close()
Expected result: When you run this script later (after the Pico is ready), your terminal prints numbers 1 to 1000 while sending the same values to the Pico as CSV-style data.
Step 2 - Save the Pico receiver as main.py
Goal: Make the Pico listen for incoming serial data, split it by commas, and append each value to a CSV file.
What to do: In Thonny, save the following script to the Pico as main.py. This is important because the Pico will auto-run main.py on boot. Thonny needs access to the same serial port the PC script will use, so you will write the file first, then power-cycle the Pico so it runs without Thonny connected.
Code:
import time
from sys import stdin
import uselect
csv_filename = "data.csv"
def save_to_csv(data):
with open(csv_filename, "a") as f:
f.write(data + "\n")
while True:
select_result = uselect.select([stdin], [], [], 0)
buffer = ''
while select_result[0]:
input_character = stdin.read(1)
if input_character != ',':
buffer += input_character
else:
save_to_csv(buffer)
buffer = ''
select_result = uselect.select([stdin], [], [], 0)
Expected result: The Pico is prepared to accept incoming serial characters, and each comma-delimited value will be appended as a new line in data.csv. The 0 timeout in uselect.select() makes the loop non-blocking.
Step 3 - Power-cycle the Pico, run the sender, then verify data.csv
Goal: Complete the transfer and confirm the file was written on the Pico.
What to do: Unplug and re-plug the Pico so main.py runs on boot. Then run the local computer Python script from Step 1 to start the transfer. After the sender finishes, connect to the Pico in Thonny and look for data.csv, then open it to confirm the values were captured.
Expected result: The computer prints values as it sends them, and the Pico stores the received comma-separated values as lines in data.csv.
Conclusion
You set up a simple serial pipeline where a local computer sends data to a Raspberry Pi Pico, and the Pico saves the incoming stream into a CSV file for later retrieval. This pattern is a solid foundation for more advanced embedded and IoT data-loading workflows.
Want parts for your next Pico build? Grab project essentials from ShillehTek.com. If you want help customizing this workflow for your device, product demo, or data pipeline, check out our IoT consulting services.