banner
News center
We focus on delivering exceptional products, prompt deliveries, and attentive customer care.

Raspberry Pi Pico File System & SD Card Reader

Jun 07, 2023

The Raspberry Pi Pico doesn't have any removable storage, but you can use its internal flash memory to read and write files and its easy to add an SD card reader. We show how to do it in MicroPython.

This is new material that extends our recent book:

Buy from Amazon.

Also of interest:

Raspberry Pico File System

<ASIN:1871962803>

<ASIN:B0BR8LWYMZ>

<ASIN:187196279X>

<ASIN:B0BL1HS3QD>

The Pico has 2MB of flash memory which is used to store the MicroPython interpreter. The rest of the memory is converted into a file system that you can make use of from within your own MicroPython programs.

The flash memory is divided into a number of partitions that hold the system and data. Unlike some other implementations of MicroPython, only the data partition,1.6MB, is available and it is automatically mounted as the root when the system boots up. You can gain access to this partition using the rp2.Flash() function which returns a Partition object representing the data partition.

Once you have an instance of Partition you can use any of the following methods:

These methods implement both the simple and extended the block protocols defined by os.AbstractBlockDev. Only a subset of ioctl commands are implemented for the Pico:

4 – Get a count of the number of blocks, should return an integer (arg is unused)

5 – Get the number of bytes in a block, should return an integer, or None in which case the default value of 512 is used (arg is unused)

6 – Erase a block, arg is the block number to erase

Generally these commands are too low level to work with a partition and instead you want to install a file system so that you can work in terms of files. But if you really want to reinvent the wheel then you can work directly in terms of raw blocks.

The Flash object is set up so that it isolates you from the other partitions and its block numbers start at zero. So to write some data to block zero and read it back we can use:

First we unmount the file system so that it can't be used. If the partition was left mounted the file system could use the block we are about to use and overwrite it. Next we get the number of blocks and the block size. Before we can write new data to a block we have to erase it using ioctl command 6. After this we can write any number of bytes up to the block size. Reading the data back is just a matter of setting the length of the buffer to specify the number of bytes to read in. If you try this out you will find that we have stored "Hello World":

The bytes beyond "Hello World" are whatever was already stored in the block.