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