Pi Pico SD Card Read/Write Issues

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
thecomeback_king
Posts: 6
Joined: Tue Aug 03, 2021 12:31 pm

Pi Pico SD Card Read/Write Issues

Post by thecomeback_king » Thu Aug 05, 2021 2:16 pm

I am having errors when trying to read/write to a SD module through SPI1 on the pi pico. Below is my code and error message. Does anyone know why this is happening? Any help is appreciated. The micro SD card is 8gb in FAT32 format

Code: Select all

# SPI 1 Pins
miso_sd = Pin(12)  # blue - RX
mosi_sd = Pin(11)  # yellow - TX
sck_sd = Pin(10)  # green
cs_sd = Pin(9, Pin.OUT)  # white - SD

spi_sd = SPI(
    1,
    baudrate=1000000,
    polarity=0,
    phase=0,
    bits=8,
    firstbit=machine.SPI.MSB,
    sck=sck_sd,
    mosi=mosi_sd,
    miso=miso_sd
)
sd = sdcard.SDCard(spi_sd, cs_sd)

# Mount file system
vfs = uos.VfsFat(sd)
uos.mount(vfs)

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

# Open the file we just created and read from it
with open("/test01.txt", "r") as file:
    data = file.read()
    print(data)

uos.unmount()

Error Message:
Traceback (most recent call last):
File "<stdin>", line 73, in <module>
File "/lib/sdcard.py", line 54, in __init__
File "/lib/sdcard.py", line 87, in init_card
File "/lib/sdcard.py", line 135, in init_card_v2
OSError: timeout waiting for v2 card

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Pi Pico SD Card Read/Write Issues

Post by jimmo » Fri Aug 06, 2021 2:37 am

thecomeback_king wrote:
Thu Aug 05, 2021 2:16 pm
File "/lib/sdcard.py", line 135, in init_card_v2
OSError: timeout waiting for v2 card
Where does /lib/sdcard.py come from?

How is the SDCard connected to your Pico? Is it using a third-party module? What does the wiring look like? (My guess is maybe the wiring, possibly the power, is incorrect)

User avatar
jcf
Posts: 60
Joined: Wed Mar 03, 2021 11:14 am

Re: Pi Pico SD Card Read/Write Issues

Post by jcf » Mon Aug 09, 2021 12:21 pm

I am currently also working with SD cards.
My experience was that about half of my cards worked fine, the other half did not work (the total was ca. 10).

Maybe a timing problem?

Post Reply