Error while interfacing SD card and Arduino Nano ESP32

Hello every one. I am trying to interface an sd card with arduino Nano ESP32. The SD card is intitalising successfully but but mounting is failing due to 'Timeout waiting for response' error. I am using sdcard.py library.

The circuit diagram and code is attached below:

import machine, os, sdcard

# Ensure SPI host is available (initialization of SPI 1)
try:
    spi = machine.SPI(1, sck=machine.Pin(14), mosi=machine.Pin(13), miso=machine.Pin(12))
    spi.deinit()  # Deinitialize SPI 1 if necessary
except Exception:
    pass

# Initialize SPI interface and CS pin for SD card
cs = machine.Pin(21, machine.Pin.OUT)

# Initialize SD card with SPI interface and chip-select pin
try:
    sd = sdcard.SDCard(spi, cs)  # Pass the SPI instance and CS pin
    print("SD card initialized successfully.")
except Exception as e:
    print("Error initializing SD card:", e)
    raise
# Try increasing the timeout by adding a more generous wait for the SD card initialization
import time
time.sleep(1)  # Wait for 1 second to allow SD card to initialize

# Mount the SD card
try:
    os.mount(sd, '/sd')
    print("SD card mounted successfully.")
except Exception as e:
    print("Error mounting SD card:", e)

The error message is :

raw REPL; CTRL-B to exit

OKSD card initialized successfully.
Error mounting SD card: timeout waiting for response

Any help would be highly appreciated.

I moved your topic to an appropriate forum category @rati_c

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

What happens if you add a time.sleep(0.5) after:

try:
    os.mount(sd, '/sd')

It still gives the same error.

If you remove the SD card, will it say "Error initializing SD card"

try:
    sd = sdcard.SDCard(spi, cs)  # Pass the SPI instance and CS pin
    print("SD card initialized successfully.")
except Exception as e:
    print("Error initializing SD card:", e)
    raise

Yead after removing the sd its giving error: no SD card

OKError initializing SD card: no SD card
Traceback (most recent call last):
File "", line 15, in
File "/lib/sdcard.py", line 54, in init
File "/lib/sdcard.py", line 82, in init_card
OSError: no SD card

The obvious, does the card work in another reader? Is it formatted with right filesystem?

Yes I can read the card in my laptop and it is formatted to FAT32

Not so many ideas left, but one - if you got another microcontroller, wire up the reader to that, see if it initialize and mounts. Just to verify that the card reader works.

@ledsyn are you using the pin numbering from the Nano ESP 32 pin out diagram

if you type

from machine import SPI
SPI(1)

it will show the default SPI(1) hardware parameters including the pin numbers

SPI(id=1, baudrate=500000, polarity=0, phase=0, bits=8, firstbit=0, sck=48, mosi=38, miso=47)

EDIT we are looking at the GPIO numbering on the yellow tabs

Interesting numbering, from 38 to 47 in one step.

I don't know if @rati_c knows about this, we'll see.

@ledsyn @rati_c sorry ledsyn I addressed the wrong person in my last post.

There are a couple of ways to use an SD card in micropython here is one way without importing SPI and using the in built SDCard module.

# Format a micro SD card and copy a small text file to the card before using this test code
# Nano ESP32 SPI connections MOSI=38 MISO=47 SCK=48 CS=5
# Adapter connections DI=MOSI DO=MISO SCLK=SCK CS=CS GND=GND 3.3v=3.3v


import machine
import os
import time
from machine import SDCard,Pin

def file_exists(filename):
    
    try:
        os.stat(filename)
        return True
    
    
    except OSError:
        return False

prompt=input("Press enter to start:")
#sd=machine.SDCard(slot=2,sck=Pin(48),mosi=Pin(38),miso=Pin(47),cs=5)
sd=machine.SDCard(slot=2,cs=5)

vfs=os.VfsFat(sd)

os.mount(sd,'/sd')

mybool=file_exists('/sd/sample.txt')

print(mybool)

print("Files on SD card")

print(os.listdir('/sd'))

#Create / Open a file in write mode.
#Write mode creates a new file.
#If  already file exists. Then, it overwrites the file.
file = open("/sd/sample.txt","w")

# Write sample text
for i in range(20):
    file.write("Sample text = %s" % i)
    file.write("\n")
    
# Close the file
file.close()

#Again, open the file in "append mode" for appending a line
file = open("/sd/sample.txt","a")
file.write("This Text is Appended ")
file.close()


#Open the file in "read mode". 
#Read the file and print the text on debug port.
file = open("/sd/sample.txt", "r")
if file != 0:
    print("Reading from SD card\n")
    read_data = file.read()


    print (read_data)

file.close()

os.remove('/sd/sample.txt')
os.umount('/sd')
sd.deinit()

prompt=input("Press enter to end")

machine.reset()

If you intend to run a SD card and also other SPI peripherals like a TFT screen for example you probably need Peter Hinch's sdcard module, which it looks like you already have. The initialization would be as simple as the following


from machine import Pin,SPI
import sdcard

cs = Pin(5, Pin.OUT, value=1)
sd = sdcard.SDCard(SPI(1), cs)

vfs=os.VfsFat(sd)

os.mount(sd,'/sd')
1 Like

Your SDcard module includes level translation to allow a 5V board to be used with the 3.3V uSD card.

Could you explain the logic behind using that module with a 3.3V board that does not require such level translation?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.