Accessing Micro SD Card with Pi Pico

Firstly pressed on jalanjalantok.wordpress.com

Maker Pi Pico has micro sd card module planted in the board. It is hard-wired to SPI 1 pin. So it will be a great deal if we are able to store/load large information into the external drive such as ML model, or images to be processed by/saved by Pico. The onboard flash storage will definitely be insufficient.

However, it has been quite search and try-out to get the micro sd recognized and read by Pi Pico. The micropython documentation for Pico is not there yet to specifically help us with RP2040 chip. Pi Pico firmware (1.14 stable) os’ library does not support FAT, instead it supports LittleFS. Therefore although we could detect inserted sd card easily, but mounting the file system was not that easy.

Micropython community is alive and listen to users need. A request was made to add FAT support in the OS library. And now it is there. You could compile the latest built which contains the support for FAT systems, or if you want quick solution, do the following:

First, download micropython unstable built (link) and flash it on to your Pi Pico. It is unstable built of version 1.14 on 21-Mar-2021 which contains experimental FAT support. When you read this article, the stable built may have already include the FAT system.

Second, micropython for RP2040 firmware is not built with sdcard library (at least until the time I wrote this).Get sdcard.py from micropython github repository (link) and save it in Pico flash storage.

Once you finish, then we can access the SD card with ease.

from machine import Pin, SPI
import sdcard
import os

spi=SPI(1,sck=Pin(10),mosi=Pin(11),miso=Pin(12))
sd=sdcard.SDCard(spi,Pin(15))
vfs=os.VfsFat(sd)

os.mount(sd,'/sd')
print(os.listdir('/sd'))

Here is the happy output:

At the time when this article is written, sd card access with Pico can only be done when you use CircuitPython. Using CircuitPython may need “trade-off” with some cool features of Micropython such as Interrupt and Threading. These two will make your program runs faster and efficient and do justice to RP2040’s dual core.

Having the sd card now accessible by Pico running Micropython, there is no trade-off.

Thanks for reading and until the next post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s