ESP8266 (NodeMCU) and SD Cards
I have decided to play around with connecting MicroSD cards to the ESP8266. From various examples sketches that came with Arduino, I know that it is possible but have never looked into the detail, thinking that it requires some dedicated hardware.
Fascinatingly, no such dedicated hardware is needed and with a bit of wiring, you can directly wire SD cards to the ESP8266.
This is because the SD card is not a dumb storage but actually has a microcontroller inside it, so you can communicate with it just like you would communicate with other microcontrollers. In addition, since it works on 3.3V just like our ESP8266, no level shifter is required unlike when connecting SD cards with Arduinos.
SD cards supports multiple protocols:
- The “SD” protocol which is used by most modern peripherals such as your PC or mobile phone. This protocol is faster but it is more complicated.
- The “SPI” protocol which is a slower protocol but is simpler and is used by microcontrollers that do not have a lot of IO or processing power. The ESP8266 supports this protocol natively.
I find it very interesting why they would spec out the SD card in such a way that it must support multiple protocols as it surely must add to the complexity of the SD card. My guess would be that it was added to support simpler host devices and cannot be removed due to backward compatibility issue.
To learn more about communicating with the SD card, the simplified spec can be downloaded for free from the SD association. The Physical Layer Simplified Specification is what you should look for. One thing I have found is that the SPI interface is not supported by SDUC cards (cards with capacity of more than 2 TB).
One reason I am writing this article is because I could not find a straightforward diagram of connecting an SD card to the NodeMCU. DiyIOt and Mischianti has provided me with great reference, but I want to record my work down so I can easily refer to it in the future when I have a project where I actually need SD card access.
If you have a Micro SD card adapter lying around, it is very to solder some Dupont headers onto it so we can connect it with the ESP8266. The contacts on the SD card is conveniently spread such that we can directly solder the headers onto it.
If you don’t have an adapter, don’t want to solder or want a more elegant solution, you can pick up a “Micro SD shield” from AliExpress. Make sure you get one without level shifting circuit since the ESP8266 already works on 3.3V.
The SD card and the NodeMCU should be wired up as follows. Note that the full-sized SD card has 2 ground pins but a Micro SD only has one ground pin. Only one from the adapter is connected to the Micro SD. For my adapter, it’s Pin 6, but I’m not sure if it’s universal for all adapters or not.
You can find example of sketches under the “ESP8266SdFat” directory. Most of them will work without any modification.
For example, the “SdInfo” sketch can be used to retrieve the card information. With this, you know that your connection is working.
Another interesting thing to check is how fast can our ESP8266 read and write to SD cards. The “bench” sketch provides us with this benchmark. The initial result shows that I can write at 210 KB/s and read at 882 KB/s. While it’s disappointing to see data rates in KB/s, it is surely more than enough for most sensor/IoT use-cases.
However, after checking the benchmark code, it is shown that the code uses buffer size of 512 bytes, which is one SD card block. The SdFat library supports reading and writing multiple blocks in one command which may be faster. Thus I have modified the benchmark to test multiple buffer sizes and this is the result.
It can be seen that increasing the buffer size greatly improve the speed of SD card access, possibility because the inner working of the SD card work at a block size a lot larger than 512 bytes. However, because the ESP8266 only has 80 KB of RAM, using a large buffer size may be impractical and it is also doubtful whether the ESP8266 can produce/process this amount of the data in the first place.
It is very easy to attach a Micro SD card to the ESP8266 microcontroller and it’s a good choice if you need access to a storage larger than the built-in flash memory for your use-case. The speed is very decent as well.