← index #731Issue #656
Related · high · value 0.906
QUERY · ISSUE

Raspberry Pi Pico and SD Card module

openby catietravers33opened 2023-09-15updated 2025-03-25

Hi there,

I've been trying really hard to sort out this error with wiring and looking at other forums but just cannot work it out.

This is the error message I get when using sdcard.py:
Traceback (most recent call last):
File "<stdin>", line 19, in <module>
File "sdcard.py", line 231, in readblocks
OSError: [Errno 5] EIO

And this is the code I am calling it from:
import machine
import sdcard, uos
import time

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

Intialize SPI peripheral (start with 1 MHz)

spi = machine.SPI(1,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin(14),
mosi=machine.Pin(15),
miso=machine.Pin(12))
sd = sdcard.SDCard(spi, cs)
time.sleep_ms(500)
vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")
#uos.mount(sd, '/sd')
time.sleep_ms(500)
print(uos.listdir('/sd'))

If anyone could help that would be appreciated!

1 comment
BNNorman · 2025-03-25

sdcard resets the SPI baudrate to 1320000 (default) so there's no point doing it in the SPI constructor.

If you want to change it do it here:-

sd = sdcard.SDCard(spi, cs,baudrate=<whatever you want>)

I have set it to 20,000,000 and can read my sandisk ultra sdcard. See my issue #990 posted today.

I never got 25,000,000 to work, but that may just be my sdcard hdw.

CANDIDATE · ISSUE

Raspberry Pi Pico SD Card Errors

openby Draxiss314opened 2023-05-08updated 2023-08-13

I am not able to consistently write to my microSD card using the sdcard.py library.

I have been following this tutorial to get my Raspberry Pi Pico write to my SD card. As the requisite drivers are no longer available in the normal micropython branch, I assume that they've been moved here (and that the tutorial is slightly out-of-date) and copied over the sdcard.py code to my Pico.

I'm using a Raspberry Pi Pico with a Maker Pi breakout board. My IDE is Thonmy with the Micropython (Raspberry Pi Pico) interpreter. The SD card I use is a Transcend 2GB MicroSD Card, which I have formatted to be FAT32.

When I attempt to test my Pico's ability (see below for code) to read and write to the SD card, my results are inconsistent and frequently mangle the SD card's filing system, requiring me to reformat or repair the partition before I can access it from my computer. Here is a sample of my most recent results:
After having deleted all data from the SD card and subsequently repairing the card, my output acted as expected:
Writing . . .
Finished Writing.
Reading . . .
Hello, SD World!
This is a test

Checking the SD card on my computer showed the expected result: a file called test01.txt containing
Hello, SD World!
This is a test
and nothing else.

Returning the SD card to my Pico, I ran the test again with this result:
Traceback (most recent call last):
File "<stdin>", line 27, in <module>
File "sdcard.py", line 252, in readblocks
File "sdcard.py", line 190, in readinto
OSError: timeout waiting for response

Checking the SD card on my computer, I found I could not mount it and had to repair the filesystem. After repairing, the data was garbled so I deleted it. Running the test again yielded this:
Writing . . .
Traceback (most recent call last):
File "<stdin>", line 29, in <module>
File "sdcard.py", line 252, in readblocks
File "sdcard.py", line 190, in readinto
OSError: timeout waiting for response

Again, filesystem must be repaired and data is garbled. After fixing and deleting:
Writing . . .
Traceback (most recent call last):
File "<stdin>", line 34, in <module>
File "sdcard.py", line 279, in writeblocks
OSError: [Errno 5] EIO
I think the data was garbled again (can't remember precisely), but regardless I repaired and deleted everything and tried again with the same error. This time, however, my filesystem wasn't corrupted and I had a blank test01.txt file.

I've been dealing with this for a while now and these events represent the typical pattern of issues I've been having with this code. Any help explaining what's going on would be greatly appreciated.

This is the code that I am using to test being able to read/write to the SD card; it's very similar to the default but I've adjusted it to use the Maker Pi Pico board and added a few lines to tell me whether I'm reading or writing:

import machine
import sdcard
import uos
import utime

# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(15, machine.Pin.OUT) #It's 15 on the board we're using.
# Intialize SPI peripheral (start with 1 MHz)
spi = machine.SPI(1, 
                  baudrate=1000000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(12))

# Initialize SD card
sd = sdcard.SDCard(spi, cs)

# Mount filesystem
vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")

# Create a file and write something to it
with open("/sd/test01.txt", "w") as file:
    print("Writing . . .")
    file.write("Hello, SD World!\r\n")
    #utime.sleep_ms(75)
    file.write("This is a test\r\n")
    #utime.sleep_ms(75)
    file.close()
    print("Finished Writing.")

# Open the file we just created and read from it
with open("/sd/test01.txt", "r") as file:
    print("Reading . . . ")
    data = file.read()
    print(data)
    file.close()
1 comment
AgentSonji · 2023-08-13

I know this is an old thread, but I was reading and wanted to ask if you had tried a different card. Getting EIO errors, sounds like the SD card might have a bad sector on it. I've has similar experiences with a USB stick on a raspberry pi 4, and it ended up being that the stick wasn't reliable when attempting to write any file from any source. Replacing the USB stick solved all problems.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied