microSD card reading

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: microSD card reading

Post by pythoncoder » Mon Aug 21, 2017 3:58 pm

Roberthh wrote:I see that error:
OSError: SD card CSD format not supported
... Seen with both Samsung amd Sandisk cards.
I haven't tried the driver with the ESP8266 but I have used it with the Pyboard. SD cards are tricky things and I had to submit a PR before it would work with my recent Sandisk cards. I can't see any obvious reason why it should fail on the ESP8266, or why it should work after a hard reset.

At the point of failure there has been a good deal of successful communication with the card. With my bodger's hat on I wonder if a short delay before issuing

Code: Select all

if self.cmd(9, 0, 0, 0, False) != 0:
might help (the init_card_v2() method has a 50ms delay at the start of each loop). Not the most logical suggestion but perhaps worth a try ;)
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: microSD card reading

Post by Roberthh » Mon Aug 21, 2017 4:59 pm

@pythoncoder: Sorry, wrong message. Th error message I get is:

Code: Select all

  File "sdcard.py", line 91, in init_card
OSError: couldn't determine SD card version
That's one command earlier. And that goes away after a hard reset with power.
Update: A 50 ms delay before the line "r = self.cmd(8, 0x01aa, 0x87, 4)" does not change the picture, but a soft reset also helps.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: microSD card reading

Post by pythoncoder » Tue Aug 22, 2017 3:40 pm

My delay suggestion was of course predicated on the belief that the failure was in the CSD format code.

It would be interesting to investigate but my "to do" list is rather long right now. I did have some discussion with Damien on that driver. It doesn't show up in the Git history but my recollection is that I suggested a value of 5 in line 78 because the smaller value he was testing didn't work with my Sandisk cards. So it's evidently an empirical "hit it with a hammer until it submits" bit of code. Maybe it still has an issue for some types of card.

In any event the problem needs to be looked at by someone with a failing card...
Peter Hinch
Index to my micropython libraries.

kkumar326
Posts: 16
Joined: Wed Nov 15, 2017 5:30 am

Re: microSD card reading

Post by kkumar326 » Tue Jan 09, 2018 12:59 pm

I'm also getting this error on Sandisk SD Card.

Code: Select all

File "sdcard.py", line 91, in init_card
OSError: couldn't determine SD card version
Is there a solution for this now?

ZKDMun
Posts: 42
Joined: Thu Nov 24, 2016 2:34 pm
Location: Hamburg

Re: microSD card reading

Post by ZKDMun » Fri Apr 13, 2018 2:56 pm

crizeo wrote:
Tue Aug 08, 2017 10:44 am
For those still wondering how to setup an SD card module on the ESP8266 using MicroPython 1.9.1 (as the process changed a bit, it is a lot easier now):
  • Copy sdcard.py to the board (e.g. using ampy)
  • Wiring SD card module <-> ESP8266 (GPIO):
    • GND <-> GND
    • DO/MISO <-> MISO (12)
    • DI/MOSI <-> MOSI (13)
    • CLK/SCK <-> SCK (14)
    • CS <-> CS (15)
    • VCC <-> VCC 3.3V/5V (Check your modules specification! If it has a voltage regulator on it (as mine), it probably requires 4.5~5.5V input, otherwise 3.3V. As the ESP8266 only supplies 3.3V output, you may need an external 5V power source. Some modules like the Feather HUZZAH are powered by USB and therefore have a 5V output (called USB / VBUS on the Feather). If not, connect the external power sources VCC to the SD modules VCC (and power sources GND to ESP8266 GND). If your module works with 3.3V you can simply connect VCC <-> VCC 3.3V of course)
  • Hard reset the ESP8266 (this may be required or may not?)
  • Run the following commands on your ESP:

    Code: Select all

    import os, machine, sdcard
    sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))  # hard reset required after wiring
    #os.umount('/')       # only if you want to unmount flash (NOT REQUIRED!)
    vfs = os.VfsFat(sd)   # is this required?
    os.mount(vfs, '/sd')   # or '/' if you did the umount command before
    #os.listdir()         # you'll see a new '/sd' folder
    #os.statvfs('/sd')	 # size of sd card = [0]*[2] bytes (free: [0]*[3])
    os.chdir('sd')        # to change directory (if you kept '/')
    #os.listdir()         # you'll see the sd card's contents (or listdir('/sd') from outside)
- copy sdcard.py to the board: check
- wiring sdcard-module: check
- hard reset: check
but:

Code: Select all

>>>import machine, os, sdcard
>>> sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
>>> vfs = os.VfsFat(sd)
>>> os.mount(vfs, '/sd')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
OSError: [Errno 19] ENODEV
Any idea about the problem and how to fix it?

I'm using a NodeMCU, a "CATALEX MicroSD Card Adapter", and a 8GB Micro-SDHC Class 10 Card.
The SD-Card is formatted to FAT32 (cluster sizes: 4.096)

I have found this one: https://github.com/micropython/micropython/issues/3622
With "os.mount(sd, '/')" I was not able to fix it.
Unfortunately, there was no information about what the problem with the sd-card exactly was... :/ any idea?

thx

edit: there is no label with information about the manufacturer of the sd-card - but the sd-card was delivered with the MicroSD Card Adapter as a combo-package.

ZKDMun
Posts: 42
Joined: Thu Nov 24, 2016 2:34 pm
Location: Hamburg

Re: microSD card reading

Post by ZKDMun » Sun Apr 15, 2018 10:39 am

With a second microSD-Card (a "Intenso SDHC 8GB Class10"):

Code: Select all

>>> import os, machine, sdcard
>>> sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "sdcard.py", line 54, in __init__
   File "sdcard.py", line 87, in init_card
   File "sdcard.py", line 135, in init_card_v2
OSError: timeout waiting for v2 card

ZKDMun
Posts: 42
Joined: Thu Nov 24, 2016 2:34 pm
Location: Hamburg

Re: microSD card reading

Post by ZKDMun » Thu Apr 19, 2018 2:25 pm

Silly me!

I found my mistake - therefore I used an another kind of NodeMCU, and now I use some NodeMCUs v3 from LoLin.
The Pins there are in some cases a little bit different - the Vin supports only 5V-INPUT, no OUTPUT (my another board has an Input-Output-5V) - so I have to connect the VCC with the "VU"-Pin (this one is the Pin on the LoLin-Board which delivers the 5V-Output) on the NodeMCUs v3 from LoLin.

Now everything works fine. (edit: for every SDCard I have)

I would like to apologize for wasting your time :oops: :(

Phloow
Posts: 2
Joined: Fri Oct 05, 2018 9:04 am

Re: microSD card reading

Post by Phloow » Fri Oct 05, 2018 10:01 am

I am having a challenge using the NodeMCU ESP8266 with the SDCard driver. I have a 2GB microSD formatted using Windows using the following options:
Filesystem - FAT32
Allocation Unit Size - 512bytes
Format options - Quick Format

I have followed all the above steps as given by "crizeo" but when I get to the os.mount(sd, "/")' i got the error:

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

My connection is as recommended and my SD card module is powered with 5V (not 3.3V). Need help!

Phloow
Posts: 2
Joined: Fri Oct 05, 2018 9:04 am

Re: microSD card reading

Post by Phloow » Fri Oct 05, 2018 11:27 am

Resolved! Apparently, 2GB card is the problem. Everything worked with a 4GB card. Thanks

jakub st
Posts: 2
Joined: Sun Nov 04, 2018 8:00 pm

Re: microSD card reading

Post by jakub st » Sun Nov 04, 2018 8:05 pm

Hello,
is there any option to use this code for 2GB cards ?
I've got a whole bunch of such SD cards I really want to use in my projects.
I've tried several format options but without success.

Post Reply