NodeMCU : Interfacing SD Card with ESP8266
|I have written many blogs on NodeMCU , but SD card example was missing. in this blog we will see how to mount SD card in NodeMCU filesystem, access files from SD card using NodeMCU’s file API.
We will be using SD card in SPI mode, first we need to connect the SD card breakout board with NodeMCU.
Connecting SD card board to NodeMCU :
You don’t need any level shifter, you can directly connect from SD card breakout to NodeMCU, if your breakout board already has a level shifter supply 3v3 for VCC pin of breakout board. Connect SD card board to NodeMCU according to following connections
By default FatFS module is disabled in nodemcu firmware, you need to build the firmware with FatFS support your self, or you can use the online build tool
Goto nodemcu-build.com , build firmware according to your needs, don’t forget to check the FatFS support at the bottom of the page
Load the new firmware and enter the following commands in the serial console, it will list the files in root directory of the SD card.
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8); vol = file.mount("/SD0", 8); file.chdir('/SD0'); l = file.list(); for k,v in pairs(l) do print("name:"..k..", size:"..v) end
We can open a file using file.open(‘/SD0/filename’), you can check out the file API for list of functions available, following snippet will print the contents of the foo.txt file
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8); vol = file.mount("/SD0", 8); file.open("/SD0/foo.txt") print(file.read()) file.close()
if the file is too big, the nodemcu will reset itslef, you might need yield the control to the system in the read loop by calling tmr.wdclr()
Hi Sankar,
I used the same nodemcu board and sdcard board but I am unable to mount the SD card.
The following line
vol = file.mount(“/SD0”, 8);
gives me nil in vol.
Wiring is proper.
nodemcu 3.3v –> sdcard board Vcc
nodemcu GND –> sdcard board GND
nodemcu D5 –> sdcard board SCK
nodemcu D6 –> sdcard board MISO
nodemcu D7 –> sdcard board MOSI
nodemcu D8 –> sdcard board CS
I also tried powering up sdcard board with external Arduino +5V volts but the same issue persists. I also tried switching CS pin from D8 to D2 and issued the command: “vol = file.mount(“/SD0″, 2);” but the same problem is there. vol is still nil and it doesn’t mount.
Hi,
check if the SD card is formatted as FAT or not
Yes it is formatted as FAT. I am able to read/write successfully using Arduino and the same sdcard module with same sdcard.
Hey did u find any solution coz i ran into a similar problem.
Hey,
My pin connections and wiring everything is fine still i am getting initiallization error stating card not present!!
what could be the possible errors
from nodemcu docs
“If the card doesn’t work when calling file.mount() for the first time then re-try the command. It’s possible that certain cards time out during the first initialization after power-up.”
i want to use SD0,SD1,SD2,CMD,CLK for my SD card interface with SPI communications but iam unable to get the output can u give any idea.
ESP8266 D5,6,7,8, are engaged with other function
Did you find out about this? I would like to do the same thing
It uses hardware SPI port, I think you can’t remap the IOs
How about the SPI pinout of the module. Here you uses the HSPI pin, but according to documentation, theres SPI with MISO, MOSI, CLK and CS pin too. I am trying to incorporate the SD card to my scrolling text matrix so that it will enable the SD card text data and display the text written on it when I am not sending command via WEB server to display what I write and submit to the module.
My scrolling text uses the HSPI port.
Hi Shanker, I can’t build firmware from “https://nodemcu-build.com/”, when upload new firmware, my board is blocked (continous flashing blue led). Could you please share your file nodemcu-master “.bin”? Thanks in advance for your help. Kind Regards.
Hi Gustavo, Unfortunately I couldn’t locate my bin file.
How about the SPI pinout of the module. Here you uses the HSPI pin, but according to documentation, theres SPI with MISO, MOSI, CLK and CS pin too. I am trying to incorporate the SD card to my scrolling text matrix so that it will enable the SD card text data and display the text written on it when I am not sending command via WEB server to display what I write and submit to the module.
My scrolling text uses the HSPI port.