ESP8266 - Micro SD Card

This tutorial instructs you how to use the Micro SD Card with ESP8266. In detail, we will learn:

ESP8266 NodeMCU Micro SD Card

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Micro SD Card
1×Micro SD Card Module
1×Jumper Wires
1×Breadboard
1×USB 3.0 SD Card Reader
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Micro SD Card Module

The Micro SD Card Module is a link between ESP8266 and a Micro SD Card. It is able to establish a connection with the ESP8266 and can be used to hold the Micro SD Card. In other words, it serves as a bridge between the two.

The Micro SD Card Module Pinout

Micro SD Card Module pinout

The Micro SD Card Module has 6 pins:

  • VCC pin: This should be connected to the Arduino's 5V pin.
  • GND pin: This should be connected to the Arduino's GND.
  • MISO pin: (Master In Slave Out) This should be connected to the Arduino's MISO pin.
  • MOSI pin: (Master Out Slave In) This should be connected to the Arduino's MOSI pin.
  • SCK pin: This should be connected to the Arduino's SCK pin.
  • SS pin: (Slave Select) This should be connected to the pin specified in ESP8266 code as a SS pin.

Preparation

Ensure that the Micro SD Card is formatted as either FAT16 or FAT32 (you can search online for more information).

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 - How to open a file on Micro SD Card and create if not existed

ESP8266 Code

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Insert the Micro SD Card into the Micro SD Card module.
  • Make the connections between the Micro SD Card module and ESP8266 as per the wiring diagram.
  • Connect the ESP8266 to the PC with a USB cable.
  • Open the Arduino IDE and select the appropriate board and port.
  • Open the Serial Monitor in the Arduino IDE.
  • Copy and paste the following code into the Arduino IDE.
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS D8 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); if (!SD.exists("esp8266.txt")) { Serial.println(F("esp8266.txt doesn't exist. Creating esp8266.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("esp8266.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("esp8266.txt")) Serial.println(F("esp8266.txt exists on SD Card.")); else Serial.println(F("esp8266.txt doesn't exist on SD Card.")); } void loop() { }
  • Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
  • The Serial Monitor will show the result of the first execution.
COM6
Send
SD CARD INITIALIZED. esp8266.txt doesn't exist. Creating esp8266.txt file... esp8266.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Will be different
  • The outcome displayed on the Serial Monitor for subsequent executions will be distinct.
COM6
Send
SD CARD INITIALIZED. esp8266.txt exists on SD Card.
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may not observe the output on the Serial Monitor when you first upload your code, if it is done before opening the Serial Monitor.

  • Remove the Micro SD Card from the module.
  • Put the Micro SD Card into a USB SD Card reader.
  • Attach the USB SD Card reader to the computer.
  • Verify if the file is present or not.

ESP8266 - How to write/read data to/from a file on Micro SD Card

The following code:

  • Saves data to a file
  • Reads the content of the file character-by-character and displays it on the Serial Monitor
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS D8 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("esp8266.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to esp8266.txt myFile.println("Learn ESP8266 and SD Card"); // write another line to esp8266.txt myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } // open file for reading myFile = SD.open("esp8266.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } } void loop() { }
  • The Serial Monitor displayed the contents of the file.
COM6
Send
Created by ArduinoGetStarted.com Learn ESP8266 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

By default, the data will be added to the end of the file. If you restart ESP8266 with the code above, the text will be appended to the file again, and the Serial Monitor will display more lines like this:

COM6
Send
Created by ArduinoGetStarted.com Learn ESP8266 and SD Card Created by ArduinoGetStarted.com Learn ESP8266 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can remove the Micro SD Card from the module and use a USB SD Card reader to view its contents on your computer.

ESP8266 - How to read a file on Micro SD Card line-by-line

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS D8 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); // open file for writing myFile = SD.open("esp8266.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to esp8266.txt myFile.println("Learn ESP8266 and SD Card"); // write another line to esp8266.txt myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } // open file for reading myFile = SD.open("esp8266.txt", FILE_READ); if (myFile) { int line_count = 0; while (myFile.available()) { char line[100]; // maximum is 100 characters, change it if needed int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // print the character to Serial Monitor // \n character is escaped by readBytesUntil function Serial.write('\n'); // print a new line charactor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } } void loop() { }
  • The output displayed on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Line 1: Created by ArduinoGetStarted.com Line 2: Learn ESP8266 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

You may observe additional lines on Serial Monitor if the file's content has not been erased previously.

ESP8266 - How to overwrite a file on Micro SD Card

By default, the content will be added to the end of the file. The most straightforward way to overwrite a file is: delete the existing file and create a new one with the same name.

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS D8 File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } Serial.println(F("SD CARD INITIALIZED.")); SD.remove("esp8266.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("esp8266.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to esp8266.txt myFile.println("Learn ESP8266 and SD Card"); // write another line to esp8266.txt myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } // open file for reading myFile = SD.open("esp8266.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: error on opening file esp8266.txt")); } } void loop() { }
  • The outcome displayed on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. Created by ArduinoGetStarted.com Learn ESP8266 and SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Reboot the ESP8266
  • Verify if the material in the file is added to the Serial Monitor or not.

You can remove the Micro SD Card from the module and view its contents on your computer. A USB SD Card reader is necessary for this.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!