Save recorded data to .txt file

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Posts: 5
Joined: Wed Jun 09, 2021 1:14 pm

Save recorded data to .txt file

Post by james_saunders2 » Mon Jun 21, 2021 9:58 am

I'm looking for a way to save data that I have stored into an array on the Raspberry Pi Pico using Thonny into a .txt file on my computer so that I can access it. This is the code I am using:

Code: Select all

import machine
from utime import sleep, ticks_ms, ticks_diff

# This code reads the serial data and assigns it to an array which then sends it to a .txt file after

analog_value = machine.ADC(26) # Assign pin 26 to the input
reading = [None] * 20000 # Setup array of 20,000 elements for 20,000 recordings
timer_start = ticks_ms() # Just used to check how long the recording takes
for i in range(20000):
    reading[i] = analog_value.read_u16() # Takes down ADC output value 
time_difference = ticks_diff(ticks_ms(), timer_start) # Stops and prints timer value
print("Recording time: " + str(time_difference) + " miliseconds.")
file = open("microphone_readings.txt", "w")
timer_start = ticks_ms() # Just to check how long printing to the .txt file takes
for i in range(20000):
    file.write(str(reading[i]) + "\n") # Writes readings to .txt fie saved on Pico
time_difference = ticks_diff(ticks_ms(), timer_start)
print("Print time: " + str(time_difference) + " miliseconds.")
Basically, the issue is that it saves the data to the Pi Pico, but I need to be able to access and plot the data which I can't think how to do from the Pico, and I can't see a way of saving that data to the computer instead.

Any ideas?

Posts: 91
Joined: Sat Aug 12, 2017 2:27 pm

Re: Save recorded data to .txt file

Post by scruss » Wed Jun 23, 2021 12:11 am

You could:
  • write the file to an SD card, and look at that in your computer later
  • print the data to the serial connection, and capture that to a local file using something like grabserial

User avatar
Posts: 3668
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada

Re: Save recorded data to .txt file

Post by dhylands » Wed Jun 23, 2021 2:24 am

I wrote json-ipc which you could use to transfer the data the your host and then plot it from there.

Christian Walther
Posts: 152
Joined: Fri Aug 19, 2016 11:55 am

Re: Save recorded data to .txt file

Post by Christian Walther » Wed Jun 23, 2021 7:12 pm

If a permanent serial connection to your computer is acceptable, you can use mpremote to mount a directory from your computer on the Pico. If not, just use it (or any other file transfer tool) to copy the file over.

Post Reply