DEV Community

Cover image for How to Transfer Data from Computer to Raspberry Pi Pico
Shilleh
Shilleh

Posted on

How to Transfer Data from Computer to Raspberry Pi Pico

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!

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()
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 in 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!

Top comments (0)