Raspberry Pi Pico File System & SD Card Reader
Written by Mike James & Harry Fairhead   
Monday, 29 May 2023
Article Index
Raspberry Pi Pico File System & SD Card Reader
File Systems
sdcard Driver

The SD Card driver isn't currently a default module in MicroPython. The simplest thing to do is to go to:

https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py

and copy and paste the Python code into a file called sdcard.py. Upload this to the Pico and you should be able to include it in your program. To make use of it you first have to define the SPI interface:

spi = machine.SPI(1,
                  baudrate=100000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(8))

and a GPIO line to use as CS:

cs = machine.Pin(9, machine.Pin.OUT)

You then use these to create an instance of sdCard:

sd = sdcard.SDCard(spi, cs)

The instance is a block device to which you can install a file system and then mount. For example, assuming that the SD card isn't formatted, you can format it to a FAT file system using:

os.VfsFat.mkfs(sd)

Notice that this will delete any data on the SD card and it takes few minutes to complete. Once you have a formatted card you can mount it:

os.mount(sd,"/sd")

The folder used as the mount point will be created if it doesn't exist. Now that the SD card is mounted we can read and write it using the standard file operations.

A complete test program that erases the SD card, writes some data and reads it back is:

import machine  
import os
import sdcard
cs = machine.Pin(9, machine.Pin.OUT)
spi = machine.SPI(1,
                  baudrate=100000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(8))
sd = sdcard.SDCard(spi, cs)
os.VfsFat.mkfs(sd)
os.mount(sd,"/sd")
f=open("/sd/Hello.txt", "w")
f.write("Hello World!\r\n")
f.write("Some more data\r\n")
f.close()
f=open("/sd/Hello.txt", "r")
data = f.read()
print(data)

If you don't want to format the card first comment out the line os.VfsFat.mkfs(sd).

If you find you have an SD card that doesn't work with this program try a lower clock speed. You can try increasing the clock speed to see if the SD card reader will cope. Some SD cards will fail to work at any speed as they don't implement the SPI bus in the correct way. As the SD card is FAT formatted it can be read in any machine that has an SD card slot.

Programming the Raspberry Pi Pico/W In MicroPython Third Edition

By Harry Fairhead & Mike James

PicoPython3E360

Buy from Amazon.

Contents

  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Getting Started With The GPIO
  • Chapter 4 Simple Output
  • Chapter 5 Some Electronics
  • Chapter 6 Simple Input
             Extract: Simple Input ****
  • Chapter 7 Advanced Input – Events and Interrupts
  • Chapter 8 Pulse Width Modulation
             Extract: PWM  ****
  • Chapter 9 Controlling Motors And Servos
             Extract: DC Motors ****
  • Chapter 10 Getting Started With The SPI Bus
  • Chapter 11 A-To-D and The SPI Bus  ****
  • Chapter 12 Using The I2C Bus
  • Chapter 13 Using The PIO   
  • Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
             Extract: A PIO Driver For The DHT22   ****
  • Chapter 15 The 1‑Wire Bus And The DS1820
  • Chapter 16 The Serial Port
  • Chapter 17 Using The Pico W - WiFi
             Extract: HTTP Client  ****     
  • Chapter 18 Sockets
             Extract: Sockets ****
  • Chapter 19 Asyncio And Servers
             Extract: Asyncio Client  ***NEW!!!
  • Chapter 20 Advanced Hardware
  • Chapter 21 Direct To The Hardware
             Extract: Direct To The Hardware ****

Also of interest:

Raspberry Pico File System

**** articles from the second edition not yet updated.

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


AI Finds Vulnerabilities - Not Everyone Is Happy
13/08/2025

An obvious use for AI, the right sort of AI, is to get it to scan a code base and point out security vulnerabilities. What could possibly go wrong?



Cult.Repo's Python Documentary Coming Soon
27/07/2025

A must-watch full-length documentary for all Pythonistas is to be premiered on August 28th. This 3-minute trailer gives a taster of what to expect.


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Thursday, 08 June 2023 )