Micropython Machine Module and SD cards

Hi.
I’ve been trying to figure out this device with miropython.

Trying to read a .wav file off an SD card.
I’ve been following this gitlab page

Here is my attempt.

import os
from machine import SDCard, Pin

sd = SDCard(slot=1, sck=Pin(6), mosi=Pin(3), miso=Pin(4), cs=Pin(29))
os.mount(sd, "/sd")

And the interpreter is telling me

ImportError: can’t import name SDCard

And as usual the interpreter is correct.
You can see below machine does not contain such a library. (OR maybe I don’t have the right version of the module installed)

>>> help(machine)
object <module 'machine'> is of type module
  __name__ -- machine
  mem8 -- <8-bit memory>
  mem16 -- <16-bit memory>
  mem32 -- <32-bit memory>
  unique_id -- <function>
  soft_reset -- <function>
  bootloader -- <function>
  reset -- <function>
  reset_cause -- <function>
  idle -- <function>
  freq -- <function>
  lightsleep -- <function>
  deepsleep -- <function>
  disable_irq -- <function>
  enable_irq -- <function>
  bitstream -- <function>
  dht_readinto -- <function>
  time_pulse_us -- <function>
  Signal -- <class 'Signal'>
  SoftI2C -- <class 'SoftI2C'>
  SoftSPI -- <class 'SoftSPI'>
  ADC -- <class 'ADC'>
  I2C -- <class 'I2C'>
  I2S -- <class 'I2S'>
  PWM -- <class 'PWM'>
  SPI -- <class 'SPI'>
  UART -- <class 'UART'>
  WDT -- <class 'WDT'>
  Pin -- <class 'Pin'>
  RTC -- <class 'RTC'>
  Timer -- <class 'Timer'>
  PWRON_RESET -- 1
  WDT_RESET -- 3

Any thoughts?
How does everyone else read an SD card?
Pix :heavy_heart_exclamation:

2 Likes

Hey Pixmusix,

From what I can tell from the code used in this link they are using a big if/else statement to determine what board is being used before they import the required library.

Looking at the documentation for SD card use in MicroPython I think this library call method is incorrect. I think it should be

import os
import machine
import sdcard

With ‘sdcard’ being called in lowercase and as a separate library.

The documentation at the below link made sense to me, hopefully you can make use of it:

Hopefully this helps you out!
Sam

1 Like

Ah! Progress.
Installed; no Import-Errors!
I did some reasearch and the tutorial i was following used an older version of this lib when it was a submodule of machine. Now it’s a standalone.

There was something that worried me I want to flash by someone.
Have a look at the pinout for the Audio BFF I’m working with.
It has four pins.


Default SD Card Pins

The SD Card is connected to the default SPI pins on the QT Py. SDCS is connected to pin A0.

  • MO - This is the SPI MOSI (M icrocontroller O ut / S erial I n) pin. It is used to send data from the microcontroller to the SD card. It is connected to the default MOSI pin on the QT Py.
  • MI - This is the SPI MISO (Microcontroller In / Serial Out) pin. It’s used for sending data from the SD card to the microcontroller. It is connected to the default MISO pin on the QT Py.
  • SCK - This is the SPI clock input pin. It is connected to the default SCK pin on the QT Py.
  • A0 - This is the SD chip select pin (SDCS).

But the constructor of sdcard is only asking for 2 pins: Data and Chip-Select.
It’s not asking me for a clock or or an MOSI.

def __init__(self, spi, cs, baudrate=1320000):

That gave me pause because I really don’t feel like corrupting a bunch of SD cards debugging this.

Check this page out, it’s constructor looks totally different.

https://docs.micropython.org/en/latest/library/machine.SDCard.html

It also implies its meant to be imported from machine.

Is there something obvious I’m missing?

1 Like

Ah hang on! I found the obvious thing I was missing.
The constructor is asking for the entire SPI, not the individual pins that make up that SPI.

import os
from machine import Pin, SPI
import sdcard

sd = sdcard.SDCard(SPI(0), Pin(29))
os.mount(sd, "/sd")
os.listdir('/')

I chose SPI0 based on this image.
https://learn.adafruit.com/assets/107201

I’m now getting time out errors, which may or may not be progress. :man_shrugging:
It might be that the sd card I’m using is 32gb and that’s just too big?

I think I’ll try a new SD card.
Any other ideas to try while I’m at it?

1 Like

Hmmm, no suggestions coming to mind for a time out error. The sdcard.py source code has multiple loops that run until that error is produced so it could be any number of things.

To large of an SD card seems like a decent thing to check. The only other thing that comes to mind is maybe some conflict with how the card is formatted?

1 Like

What’s the default baudrate?

I’ve had a look at the RP2 port of the machine library (are you using an RP2040?)

The SPI section looks like it defaults to 10MHz which might be a bit intense for a breadboarded circuit. If the speed is too high the signal integrity will be compromised (the transitions become mushy).

Try something lower eg.

spi = SPI(0, 100_000)  # Default assignment for SPI0, 100kHz

It might still be helpful to be explicit with your pins, just in case. Leave nothing to chance :smiley:

2 Likes

SOLUTION!

Bought a new 32gb SD card and formatted and resized it to fat32-16gb. That got me the data on the SD card.

Thanks @Samuel for suggesting that particular library that helped a bunch.

Something to keep in mind :slight_smile:

3 Likes