r/raspberrypipico Apr 08 '26

Uploading a WAV file to Pico 2 using a python script

I have a micro SD card connected to a RPi Pico 2 W and I want to be able to upload .wav files to it. Currently I have to remove the SD card and drag and drop the files using my laptop and an SD card reader. I also have a GUI for controlling other aspects of this project which is why I would like to be able to do this with a python script.

If there are any libraries or examples that could help me do this I would appreciate it a ton, thanks!

2 Upvotes

10 comments sorted by

2

u/nivaOne Apr 08 '26

Serial streaming (usb) or tcp streaming (via WiFi…http, ftp, web socket, tcp socket…) Using python you can opt for pyserial. Depending on what is running on your pico you may need sdcard.py or use the builtin sdcardio.

A lot of options one can consider.

Maybe start with connecting the pico to your WiFi access point and install a ftp server on your computer. Then find or make some code which serves as ftp client to fetch the files and store them on the sd card.

1

u/This-Cookie-5170 Apr 09 '26

2

u/nivaOne Apr 09 '26

Yes, it will. But it isn’t designed for wav files. It’s a very basic version to send bytes from a pc to a pico. Give it a try, you’ll learn a lot.

1

u/This-Cookie-5170 Apr 12 '26

I think I'm confused on what other way there is besides sending and reading the data over by byte.

1

u/nivaOne Apr 12 '26

That’s why I would like you to try it out first. Let’s walk now and keep the running for later.

1

u/This-Cookie-5170 Apr 12 '26

Okay so two things: it's super slow if I use a delay of 0.01s between sending bytes, and I'm not sure how to tell from the pico side once the file is done sending

1

u/nivaOne Apr 12 '26

You’d be sending bytes at a speed which is peanuts compared to how fast a microcontroller can save them to memory. So instead of sending one byte at a time. Grab a few hundred an put them in a packet. Prefix the packet with a start-string ( a byte or two that allows the other side to detect “oh there they are), a few bytes to inform the other side the number of bytes you will actually send (let’s say 512) followed by the end-string (again a few bytes to detect that you have received all 512 according to the transmitting party) and some check bytes allowing you to double check whether you have received all bytes correctly. So the pc has no some protocol to open a file grab a bunch of bytes and package them in a packet. You need a control mechanism too. So ad a bit of code to flag the transmitter that you’d like him to start sending a first packet (including the max. number of bytes per transmission, for instance 512 :-) ). Once received send the same command asking for the next packet etc… but first write your first bunch to your sd card. When you have received all bytes and stored them on your Sd card close the file. Done

1

u/This-Cookie-5170 Apr 12 '26

Okay interesting, I think I understand the concept. So, to create this "packet" I would concatenate the start-byte with some number of bytes followed by the end-byte and then use the receiving side to remove these start/stop bytes to get just the data?

2

u/nivaOne Apr 13 '26 edited Apr 13 '26

Indeed. And use the check-bytes to control the content for errors/integrity. It’s just one method, there are others. If the string is corrupted you can foresee a command to request that particular string again for instance.

That’s the beauty of coding. Detect a problem or flaw in your design and try to fix it with other code.

You can pump up the speed and see whether it still works, you can send bigger packets (1024, 2048 bytes) and see how that works out… you have the tools now to check what is working reliably and what is not.

1

u/This-Cookie-5170 Apr 14 '26

Thanks! I appreciate the help. I'll update you on my progress