Is there anyone mount Sdcard successfully on ESP32 ??

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
becorey
Posts: 1
Joined: Thu Aug 13, 2020 4:48 pm

Re: Is there anyone mount Sdcard successfully on ESP32 ??

Post by becorey » Thu Aug 13, 2020 4:54 pm

I agree that it is fussy about which brand/manufacturer of SD card can work with micropython.

On a Lolin D32 Pro dev board, I found that this PNY 16GB did NOT work:
https://www.amazon.com/gp/product/B07T7 ... UTF8&psc=1

While this Sandisk 8GB does work successfully:
https://www.amazon.com/gp/product/B00MH ... UTF8&psc=1

Code: Select all

def mountSD(self, sdDir = '/sd'):
        # https://docs.micropython.org/en/latest/library/machine.SDCard.html
        sd = machine.SDCard(slot = 2, width = 1,
                            sck = self.pins['CLK'], miso = self.pins['MISO'],
                            mosi = self.pins['MOSI'], cs = self.pins['CS_SD'])
        os.mount(sd, sdDir)   # creates /sd
        return sd
The Sandisk 8GB card mounts successfully, I can access /sd. But with the PNY 16GB card it gives the error:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 95, in <module>
  File "main.py", line 28, in __init__
  File "main.py", line 52, in mountSD
OSError: 16
MicroPython v1.12-663-g9883d8e81 on 2020-08-12; ESP32 module with ESP32

TGAlbertin
Posts: 1
Joined: Thu Oct 29, 2020 4:40 pm

Re: Is there anyone mount Sdcard successfully on ESP32 ??

Post by TGAlbertin » Tue Nov 03, 2020 3:47 am

hey everyone! I'm working with a Esp32 nodeMCU made by Ai-thinker. With this code I could mount a microSD but has couldn't write or read nothing, could someone help me?

import machine, sdcard, os
from machine import Pin, SPI
vspi = SPI(2, baudrate=80000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19)) # funciona
microsd = sdcard.SDCard(vspi, machine.Pin(5))
os.mount(microsd, '/sd')
os.listdir('/')
os.chdir('/sd')
os.listdir('/sd')
open("/sd/dados.txt", "w").write("12345678910\n")
open("/sd/dados.txt", "r").read()
os.umount('/sd')

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

Re: Is there anyone mount Sdcard successfully on ESP32 ??

Post by Roberthh » Tue Nov 03, 2020 6:56 am

According to my experience SD card access in the ESP32 port is very fragile. It does not work with every card, and sometimes it requires several hard resets and card unplug and re-plug until it works.
Besdes that I miss closing the file after writing. If I add these (and the mounting succeeded), it get data into the files.

steanrdr
Posts: 1
Joined: Sat May 15, 2021 11:26 am

Re: Is there anyone mount Sdcard successfully on ESP32 ??

Post by steanrdr » Sat May 15, 2021 12:02 pm

Hi all,
I read several entries how to ge the SDcard on an ESP32 dev board working. I mixed the information from verious entries to get the below. ESP32 has sevral SPI interfaces. I used slot 2, which should be the hardware SPI following the Micropython documentation.
This slot can work with 1-bit access only. I use currently a 8GB sdard. I will try with a bigger one as soon as I have one.

I can read and write with this code:

Code: Select all


from machine import Pin, SPI
import os
import sdcard

## SPI bus for SDcard
# use slot 2 which should be the hardware based and best to use
vspi = SPI(2, baudrate=80000000, polarity=1, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
vspi.init() 
sd = sdcard.SDCard(vspi, Pin(5))

os.mount(sd,"/sd")
fd=open('/sd/dfrobot.txt','rw')
fd.write('hello this is my ESP32 with 8GB sdard')
fd.seek(0)
print('Result read from file: ', fd.read())
fd.close()
print(os.listdir('/sd'))
os.umount("/sd")

If you have a better way, please let me know. Thank you

penitko
Posts: 2
Joined: Fri May 14, 2021 8:37 pm
Location: Finland

Re: Is there anyone mount Sdcard successfully on ESP32 ??

Post by penitko » Sat May 15, 2021 3:29 pm

# main.py - mounts and lists content of sd card. High speed (HSPI) Pins are 12, 13, 14, 15. No inner nor outer pullups are used.
# One can try to reduce time.sleep delays. Not yet tried by myself. Waveshare sd card module is used.
# When formatting sd card, remember to perform over write format as one of the SD Card's option is.
# I am using SD Card Formatter 5.0.1 from Tuxera Inc.
# My esp32 is Joy-IT esp32 wroom-32 dual processor version : https://joy-it.net/en/products/SBC-NodeMCU-ESP32
# Prints out :
"""
1
2
['System Volume Information', 'hello.txt']
Files on filesystem:
====================
System Volume Information/ Size: 0 by
IndexerVolumeGuid Size: 76 by
WPSettings.dat Size: 12 by
hello.txt Size: 17 by
"""

def print_directory(path, tabs = 0):
import os

for file in os.listdir(path):
stats = os.stat(path+"/"+file)
filesize = stats[6]
isdir = stats[0] & 0x4000

if filesize < 1000:
sizestr = str(filesize) + " by"
elif filesize < 1000000:
sizestr = "%0.1f KB" % (filesize/1000)
else:
sizestr = "%0.1f MB" % (filesize/1000000)

prettyprintname = ""
for i in range(tabs):
prettyprintname += " "
prettyprintname += file
if isdir:
prettyprintname += "/"
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))

# recursively print directory contents
if isdir:
print_directory(path+"/"+file, tabs+1)


def f1():
import uos
from machine import SDCard, Pin
import machine
import time
import gc

#gc.enable()
#gc.collect()

#pins = [5, 18, 19, 23]
#pins = [12, 13, 14, 15]
#for pin in pins:
# machine.Pin(pin).init(-1, machine.Pin.PULL_UP)

print(str(1))
time.sleep(0.5)
print(str(2))

#sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(18), miso=Pin(19), mosi=Pin(23), cs=Pin(5), freq=20000000)
sd = machine.SDCard(slot=3, width=1, cd=None, wp=None, sck=Pin(14), miso=Pin(12), mosi=Pin(13), cs=Pin(15), freq=20000000)

time.sleep(0.5)
uos.mount(sd, "/sd2")
time.sleep(0.5)
print(uos.listdir("/sd2"))

#calling function
print("Files on filesystem:")
print("====================")
print_directory("/sd2")


time.sleep(0.5)
uos.umount("/sd2")

#gc.collect()
#gc.disable()

# end of f1():

f1()

Code: Select all

# main.py - mounts and lists content of sd card. High speed (HSPI) Pins are 12, 13, 14, 15. No inner nor outer pullups are used.
# One can try to reduce time.sleep delays. Not yet tried by myself. Waveshare sd card module is used. 
# When formatting sd card, remember to perform over write format as one of the SD Card's option is.
# I am using SD Card Formatter 5.0.1 from Tuxera Inc.
# My esp32 is Joy-IT esp32 wroom-32 dual processor version : https://joy-it.net/en/products/SBC-NodeMCU-ESP32
# Prints out :
"""
1
2
['System Volume Information', 'hello.txt']
Files on filesystem:
====================
System Volume Information/               Size:       0 by
   IndexerVolumeGuid                     Size:      76 by
   WPSettings.dat                        Size:      12 by
hello.txt                                Size:      17 by
"""

def print_directory(path, tabs = 0):
    import os
    
    for file in os.listdir(path):
        stats = os.stat(path+"/"+file)
        filesize = stats[6]
        isdir = stats[0] & 0x4000
    
        if filesize < 1000:
            sizestr = str(filesize) + " by"
        elif filesize < 1000000:
            sizestr = "%0.1f KB" % (filesize/1000)
        else:
            sizestr = "%0.1f MB" % (filesize/1000000)
    
        prettyprintname = ""
        for i in range(tabs):
            prettyprintname += "   "
        prettyprintname += file
        if isdir:
            prettyprintname += "/"
        print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
        
        # recursively print directory contents
        if isdir:
            print_directory(path+"/"+file, tabs+1)
                

def f1():
    import uos
    from machine import SDCard, Pin
    import machine
    import time
    import gc
    
    #gc.enable()
    #gc.collect()

    #pins = [5, 18, 19, 23]
    #pins = [12, 13, 14, 15]
    #for pin in pins:
    #    machine.Pin(pin).init(-1, machine.Pin.PULL_UP)

    print(str(1))
    time.sleep(0.5)
    print(str(2))

    #sd = machine.SDCard(slot=2, width=1, cd=None, wp=None, sck=Pin(18), miso=Pin(19), mosi=Pin(23), cs=Pin(5), freq=20000000)
    sd = machine.SDCard(slot=3, width=1, cd=None, wp=None, sck=Pin(14), miso=Pin(12), mosi=Pin(13), cs=Pin(15), freq=20000000)
        
    time.sleep(0.5)
    uos.mount(sd, "/sd2")
    time.sleep(0.5)
    print(uos.listdir("/sd2"))
    
    #calling function
    print("Files on filesystem:")
    print("====================")
    print_directory("/sd2")
    
    
    time.sleep(0.5)
    uos.umount("/sd2")
    
    #gc.collect()
    #gc.disable()

# end of f1():

f1()


Post Reply