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 Second Edition

By Harry Fairhead & Mike James

picopython2e360

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 ***NEW!
  • 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 Asyncio And Servers
  • Chapter 19 Direct To The Hardware
             Extract: Direct To The Hardware

Also of interest:

Raspberry Pico File System

<ASIN:1871962803>

<ASIN:B0BR8LWYMZ>

<ASIN:187196279X>

<ASIN:B0BL1HS3QD>

 

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.


Amazon Updates Q Family And Previews App Studio
15/07/2024

Amazon made multiple announcements at its AWS Summit in New York, including a preview of AWS App Studio, the addition of Q Developer to SageMaker Studio, and an Amazon Q Apps API.



Couchbase C++ SDK Goes GA
16/07/2024

Accessing and working with Couchbase from C++ is now possible, thanks to the release of a C++ SDK that provides integration with Couchbase Server.


More News

kotlin book

 

Comments




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



Last Updated ( Thursday, 08 June 2023 )