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

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

How to Connect and Use the HCSR501 PIR Sensor with a Raspberry Pi Pico/Pico W

Learn how to set up the HCSR501 PIR sensor with a Raspberry Pi Pico to detect motion and trigger...

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Powering the Raspberry Pi Pico W with the MB102 Power Supply

Learn how to power your Raspberry Pi Pico W projects easily and flexibly with the MB102 Power Supply Module...

How to Use L298N Motor Driver with Pico W

How to Use L298N Motor Driver with Pico W

Learn how to use the L298N motor driver to control DC motors with the Raspberry Pi Pico W in MicroPython.

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Back to blog

Leave a comment

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