How to Transfer Data/Files FROM Local Computer to Raspberry Pi Pico (Programmatically) - Part 2

In this blog post, we'll explore a practical example of how to transfer data from your local computer to a Raspberry Pi Pico microcontroller. Data transfer between devices is a fundamental aspect of many IoT and embedded systems projects, and it can be accomplished using various communication methods. In this tutorial, we will use Python scripts to facilitate the data transfer. The local computer will send data to the Raspberry Pi Pico, which will then save it to a CSV file. We'll provide step-by-step instructions and code samples for both sides of the communication. Let's get started!

Before reading the remainder, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

Section 1: Setting Up the Local Computer Python Code

Before we start the data transfer process, we need to prepare the local computer to send data to the Raspberry Pi Pico. For this, we'll use Python and the PySerial library to establish a serial connection to the Pico.

Code Sample - Local Computer

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()

In this script:

- We import the `serial` and `time` libraries.

- Configure the serial connection by specifying the port (adjust this to match your setup) and baud rate.

- Send data from 1 to 1000 in CSV format to the Raspberry Pi Pico.

- Pause for 10 seconds to ensure the transfer is complete.

- Close the serial connection.

Section 2: Preparing the Raspberry Pi Pico

On the Raspberry Pi Pico side, we will create a Python script that listens for incoming data, separates it by commas, and saves it to a CSV file. This script must be saved in the main.py file on the Pico, the reason is, that this script will run automatically once the device is plugged into a power source. We cannot run this script in Thonny because Thonny needs access to the comm port, which the local Python script also needs access to. Unfortunately, they cannot connect to the comm port at the same time, so we simply use Thonny to write and save the code then we unplug and plug the Pico to ensure the script is running without needing to connect in Thonny anymore, sort of a hacky trick. Here is the script and a quick breakdown.

Code Sample - Raspberry Pi Pico

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)

In this script:

- We import the necessary libraries for input and file operations.

- Create a function `save_to_csv` to append data to a CSV file.

- Continuously monitor the serial input for incoming data, split it by commas, and save it to a CSV file named "data.csv."

- The “0” is used in the uselect to mean that the process of waiting for new data is non-blocking. More details of this are discussed in the YouTube video.

Once you have this saved, go ahead and unplug and plug the device, followed by running the local Python script on your local computer. You should see the serial monitor start to print the values and processing should begin. Once it is done, you can connect to Thonny to see the new data.csv file saved to the Raspberry Pi. Go ahead and transfer the data over and check if all values are there, it should be!

Conclusion

You've just learned how to transfer data from your local computer to a Raspberry Pi Pico using Python scripts and a serial connection. This example demonstrates a simple data transfer scenario that can be the basis for more complex IoT and embedded systems projects. Feel free to adapt and expand upon this tutorial for your specific use cases.

Don't forget to subscribe to our YouTube channel for more exciting tutorials and projects! Stay tuned for more informative content on IoT, programming, and technology.

Happy hacking!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

How to Code with Lua on ESP32 with XEdge32

How to Code with Lua on ESP32 with XEdge32

Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

Stream Audio From Raspberry Pi to Local Computer

Stream Audio From Raspberry Pi to Local Computer

Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

SSH Raspberry Pi via Cell Phone

SSH Raspberry Pi via Cell Phone

This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

Remotely Control Raspberry Pi via SSH from External Network

Remotely Control Raspberry Pi via SSH from External Network

Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

Stream Video from Raspberry Pi Camera to YouTube Live

Stream Video from Raspberry Pi Camera to YouTube Live

Learn how to stream to YouTube from a Raspberry Pi Camera.

How to Connect BH1750 with Arduino: Measure Ambient Light

How to Connect BH1750 with Arduino: Measure Ambient Light

Learn how to measure ambient light for smart lighting control using Arduino and the BH1750 Light Intensity Module.

How to Connect MPU9250 and Raspberry Pi (Part 2 - Calibration)

How to Connect MPU9250 and Raspberry Pi (Part 2 - Calibration)

Learn how to calibrate the MPU9250 in Python with the Raspberry Pi to get more accurate acceleration and gyroscopic...

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.