Maker-pi-pico MicroPython SDCard Example?

Discussion about project that uses other controller, or non controller based project such as using logic gates, FPGA, ARM, Mechanical ....etc. Welcome to post it here

Maker-pi-pico MicroPython SDCard Example?

Postby pete.hollyer » Sun Sep 19, 2021 4:09 am

Does anyone have a MicroPython SDCard Example which works with the Maker-Pi-Pico ?
I've tried everything I can find on the internet but I cannot get them to work with the built in SD Card.

Everything works great with Circuitpython just NOT MicroPython.

CODE: SELECT_ALL_CODE

import machine
import sdcard
import uos

# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(15, 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(10),
                  mosi=machine.Pin(11),
                  miso=machine.Pin(12))

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

# Mount filesystem
vfs = vos.VfsFat(sd)                                     <-------Dies Here
uos.mount(vfs, "/sd")

# Create a file and write something to it
with open("/sd/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("/sd/test01.txt", "r") as file:
    data = file.read()
    print(data)



Here is the error I get:

Traceback (most recent call last):
File "<stdin>", line 23, in <module>
File "sdcard.py", line 238, in readblocks
OSError: [Errno 5] EIO

I Tracked into the problem,

Micropython was NOT liking my SD card. Even though Circuitpython liked it.
I changed cards to a 32 GB card ( was a 2 BG Card) and it read.

CODE: SELECT_ALL_CODE
from machine import Pin, SPI
import sdcard
import os

def OpenSD():
sd_spi = SPI(1, sck = Pin(10, Pin.OUT), mosi = Pin(11, Pin.OUT), miso = Pin(12, Pin.OUT))
# Create SDCard object
sd = sdcard.SDCard(sd_spi, Pin(15, Pin.OUT))
os.mount(sd, "/sd")
os.listdir("/sd")
print("Mounted")

def CloseSD():
os.umount("/sd")
print("UMount")


Problem was ALL mine! Sorry.
Please Close Issue
pete.hollyer
Freshie
 
Posts: 7
Joined: Sun Sep 19, 2021 3:54 am

Return to Misc Project

Who is online

Users browsing this forum: No registered users and 1 guest

cron