The ESP8266 is a great board to use for many IoT projects but they have a limited amount of flash storage space. Newer models being sold have 4MB of total flash which leaves a max of about 3MB for file storage depending on the image being used.
For projects that require a large amount of data logging you can easily expand the storage by using a cheap micro SD card adapter.
The same adapter design is sold under various different names on Amazon and AliExpress. I’m using the Catalex adapter board in this example but as long as they have the same pinout any module should work. It’s always a good idea to confirm the input voltage first though.
These modules can easily be connected to the ESP8266 using the hardware SPI interface on the 8266. There is a level shifting circuit on the adapter board which allows them to interface at 5V or 3.3V.
They need to be powered using 4.5 – 5.5 volts though. The 8266 doesn’t have a dedicated 5V output but you can cheat and use the VIN pin which passes through 5V through directly from the USB power connection.
ESP8266 to SD Adapter Wiring
|SD Card Module
Interfacing Using Micropython
To interface with the SD card adapter in Micropython you can use the sdcard.py module. Download the python file and then transfer the file to the 8266 using ampy. Replace COMXX with the com port in use on your system.
ampy --port COMXX put sdcard.py
Next you can connect to the boards serial REPL and test the import. If sdcard fails to import check that it copied to the board succesfully. You should see the file when you run os.listdir().
import machine, sdcard, os
The command below will initialize the SD card module. If you get the error “OSERROR: no SD card” then there is probably an issue with the wiring. Also check that your SD card is correctly seated in the adapter.
sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
The SD card can now be mounted to the file system. On success you should see ioctl messages in the output.
You can use the os.listdir() function to list any files on the SD card, just specify the path to the mount.
You can now read and write to files on the SD card using standard python methods.
myfile = open(‘/sd/myfile.txt’, ‘w’)
myfile = open(‘/sd/myfile.txt’, ‘r’)