# Micropython Machine Module and SD cards

**URL:** <https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060>\
**Category:** Support\
**Created:** [27 May 2024 03:01 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060 "2024-05-27T03:01:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [27 May 2024 03:01 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/1 "2024-05-27T03:01:10Z")

</div>

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

> **[Adafruit Audio BFF](https://learn.adafruit.com/adafruit-audio-bff/pinouts)**
>
> The Audio BFF is designed to fit onto the back of any QT Py or Xiao board. Onboard is a MAX98357 audio amplifier and picoblade-compatible connector for plugging in a 4 or 8 ohm speaker. We use A1 for the audio data, A2 for wordselect clock, and A3...

Trying to read a .wav file off an SD card.  
I’ve been following this [gitlab page](https://git.emacinc.com/micropython-public/micropython-i2s-examples/-/blob/emac-support/examples/easy_wav_player.py)

Here is my attempt.

```python
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)

```auto
>>> 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 ❣

---

<div class="post-metadata">

**Author:** ![Samuel](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/samuel/32/11763_2.png) [@Samuel](https://forum.core-electronics.com.au/u/Samuel)\
**Post date:** [27 May 2024 06:16 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/2 "2024-05-27T06:16:38Z")

</div>

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.

> [@Pixmusix](#):
>
> I’ve been following this [gitlab page](https://git.emacinc.com/micropython-public/micropython-i2s-examples/-/blob/emac-support/examples/easy_wav_player.py)

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:

> <https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py>

Hopefully this helps you out!  
Sam

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [28 May 2024 02:16 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/3 "2024-05-28T02:16:11Z")

</div>

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 ( **M** icrocontroller **I** n / **S** erial **O** ut) 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.

```auto
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.

> **[class SDCard – secure digital memory card — MicroPython latest documentation](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?

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [28 May 2024 03:23 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/4 "2024-05-28T03:23:27Z")

</div>

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.

```auto
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](https://learn.adafruit.com/assets/107201)

I’m now getting time out errors, which may or may not be progress. 🤷‍♂️  
[It might be that the sd card I’m using is 32gb and that’s just too big?](https://github.com/micropython/micropython-lib/issues/627)

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

---

<div class="post-metadata">

**Author:** ![Samuel](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/samuel/32/11763_2.png) [@Samuel](https://forum.core-electronics.com.au/u/Samuel)\
**Post date:** [28 May 2024 06:27 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/5 "2024-05-28T06:27:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [28 May 2024 06:27 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/6 "2024-05-28T06:27:17Z")

</div>

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](https://docs.micropython.org/en/latest/rp2/quickref.html#hardware-spi-bus) 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.

```python
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 😃

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [29 May 2024 04:14 UTC](https://forum.core-electronics.com.au/t/micropython-machine-module-and-sd-cards/20060/7 "2024-05-29T04:14:10Z")

</div>

### 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.

> [@Michael](#):
>
> If the speed is too high the signal integrity will be compromised (the transitions become mushy).

Something to keep in mind 🙂
