[EN] ESP8266 MicroSD-Card Reader (Adapter)

This article is an example of using an SD-Card Read/Write module to store and read data in JSON format, which is a guideline for readers to use in the future.

(Figure. 1)

SD-Card module

(Figure. 2) micro SD-Card Reader module
(Figure. 3) micro SD-Card Reader module

The SD-Card module that we choose to use this time is as shown in Figure 2 and 3, this module is a board with a socket for connecting SD-Card or MMC Card via SPI bus.

(Figure. 4) The list of pins on MicroSD Card Reader module

From Figure 4, we can see that the pin required to be used are:

  1. VCC for connecting power supply to modules This module requires 5VDC voltage.
  2. GND for connecting to the ground
  3. CS for connecting to the CS pin of the SPI bus to use it to determine the module to work or not.
  4. MOSI for connecting to MOSI pin in SPI bus to transfer data from microcontroller to SD-Card module.
  5. SCK for connecting to the SCK pin of the SPI bus to be used as the communication bus clock pin.
  6. MISO for connecting to MISO pin of SPI bus to use as a pin to read data from SD-Card module to microcontroller board.

Pin connection

Connecting the SD-Card module to the ESP8266 board is as follows.

Micro SD Card ReaderESP8266 (NodeMCU)
VccVin หรือ 5VDC
CD (CS)D8 (GPIO15)
(Figure. 5)
(Figure. 6)

Using the SD-Card module requires the sdcard.py library that Adafruit developed. An example of the implementation steps is in the code8-1.

#code8-1 : list
import sdcard, os
from machine import Pin, SPI
# กำหนดขา CS
sdCSPin = Pin(15)
# using SPI port 1
spi = SPI(1)
# create object sdcard
sd = sdcard.SDCard(spi, sdCSPin)
# connected to folder /sd
os.mount(sd, '/sd')
# read files list in /sd
listFiles = os.listdir('/sd')
if len(listFiles) > 0:
    print("{file(s) in /sd}".format(listFiles))
    print("no file!")
# unmount the folder
# stop using SPI port

Before running the program, you need to download sdcard.py and upload it to the ESP8266, otherwise there will be error “the library file cannot be found”


MicroPython supports JSON via the JSON class library. In the example program code8-2, variable named devices was created as a dictionary, then generate id with a value of 1, create ‘board’ in devices as a string ‘esp8266’ and create “ios” for storing pin’s function, which is pin 4 for “sda”, pin 5 for “scl”, pin 12 for “MISO”, pin 13 for “MOSI”, pin 14 for “SCK”, pin 15 for “CS” then converts the dictionary into a string and displays the string. Finally, converts the string back to the dictionary and displays the result.

result = json.dumps( obj ) converts dictionary to string
result = json.loads( str ) converts string to dictionary

# code8-2
import json
# create dictionary
devices = {}
devices['id'] = 1
devices["board"] = "esp8266"
devices["ios"] = [{4:"sda",5:"scl",12:"MISO",13:"MOSI",14:"SCK",15:"CS"}]
# converts dictionary into string
myProject = json.dumps(devices) 
print("myProject type:{},value:{}".format(type(myProject), myProject))
# converts string back to dictionary
data = json.loads(myProject)
print("data type:{},value:{}".format(type(data),data))

Example code

The example code8-3 is a program that put code8-2 and saves it in a file named me.json, then read the data from me.json and display the results.

# code8-3
import json
import sdcard, os
from machine import Pin, SPI
# define pin CS
sdCSPin = Pin(15)
# use SPI port 1
spi = SPI(1)
# create sdcard object
sd = sdcard.SDCard(spi, sdCSPin)
# mount sd card to /sd
os.mount(sd, '/sd')
# create dictionary 
devices = {}
devices['id'] = 1
devices["board"] = "esp8266"
devices["ios"] = [{4:"sda",5:"scl",12:"MISO",13:"MOSI",14:"SCK",15:"CS"}]
# converts dictionary to string
myProject = json.dumps(devices) 
# write myProject into SD-Card
me = open('/sd/me.json','w')
# read data from /sd/me/json
me = open('/sd/me.json','r')
text = me.read()
# converts to dictionary
data = json.loads(text)
# display
# unmount sd card
# stop using SPI port


From this article, readers will be able to connect the SD-Card module to the ESP8266 board via SPI bus communication and write a program to connect to the SD-Card or microSD-Card file system, and learn how to use JSON as a data storing tool that allows us to convert structured data to strings. And can convert strings back to structured data. More details about using a dictionary can be found in the Python programming documentation.

Finally, the JarutEx team hopes the article will be useful to those who are interested. And can be improved to be used in the next step.

And have fun with programming.

If you want to discuss something, feel free to leave comments below.

(C) 2020, Danai Jedsadathitikul and Jarut Busarathid
ปรับปรุง 2010-10-20

Leave a Reply

Your email address will not be published. Required fields are marked *