ESP8266 - Log Data with Timestamp to SD Card

This tutorial provides comprehensive instructions on logging data with a timestamp to a Micro SD Card using ESP8266. The key components covered include:

The timestamp is acquired from an RTC module and is stored on the Micro SD Card alongside the corresponding data. The stored information on the Micro SD Card can vary, encompassing readings from any sensor or a log of door lock access...

To illustrate the process, this tutorial utilizes readings from two analog pins. However, the provided code is easily adaptable to accommodate diverse types of data.

ESP8266 NodeMCU Log to Micro SD Card

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Micro SD Card
1×Micro SD Card Module
1×USB 3.0 SD Card Reader
1×Real-Time Clock DS3231 Module
1×CR2032 battery
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Micro SD Card Module and RTC Module

If you are unfamiliar with Micro SD Card Module and RTC module (including pinout, how it works, and how to program), please refer to the following tutorials:

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.

※ NOTE THAT:

If you are using an Ethernet shield or any other shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. All you need to do is insert the Micro SD Card into the Micro SD Card Holder on the shield.

ESP8266 - Log Data with Timestamp to Micro SD Card

/* * 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-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS D8 #define FILE_NAME "log.txt" RTC_DS3231 rtc; File myFile; void setup() { Serial.begin(9600); // SETUP RTC MODULE if (!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while (1); } 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.")); Serial.println(F("--------------------")); } void loop() { // open file for writing myFile = SD.open(FILE_NAME, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp DateTime now = rtc.now(); myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(FILE_NAME); } delay(2000); // delay 2 seconds }

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.
  • Ensure that the Micro SD Card is formatted as either FAT16 or FAT32 (you can find more information about this online).
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
  • Finally, view the outcome on the Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Remove the Micro SD Card from the Micro SD Card module.
  • Insert the Micro SD Card into an USB SD Card reader.
  • Attach the USB SD Card reader to the PC.
  • Open the log.txt file on your PC; it will appear as follows.
ESP8266 NodeMCU log to Micro SD Card with time information

If you do not possess an USB SD Card reader, you can inspect the contents of the log file by executing the ESP8266 Code below.

/* * 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-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS D8 #define FILE_NAME "log.txt" 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 reading myFile = SD.open(FILE_NAME, 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 ")); Serial.println(FILE_NAME); } } void loop() { }

ESP8266 - Log Data in multiple files

Logging to a single file can lead to a large size over time, making it hard to review. The code below will create multiple files, one for each day, with the filename containing the date information in YYYYMMDD.txt format.

/* * 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-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS D8 RTC_DS3231 rtc; File myFile; char filename[] = "yyyymmdd.txt"; // filename (without extension) should not exceed 8 chars void setup() { Serial.begin(9600); // SETUP RTC MODULE if (!rtc.begin()) { Serial.println(F("Couldn't find RTC")); while (1); } 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.")); Serial.println(F("--------------------")); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); // update filename filename[0] = (year / 1000) + '0'; filename[1] = ((year % 1000) / 100) + '0'; filename[2] = ((year % 100) / 10) + '0'; filename[3] = (year % 10) + '0'; filename[4] = (month / 10) + '0'; filename[5] = (month % 10) + '0'; filename[6] = (day / 10) + '0'; filename[7] = (day % 10) + '0'; // open file for writing myFile = SD.open(filename, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: error on opening file ")); Serial.println(filename); } delay(2000); // delay 2 seconds }

Once you have completed a lengthy run, if you:

  • Remove the Micro SD Card from the Micro SD Card module
  • Place the Micro SD Card into an USB SD Card reader
  • Connect the USB SD Card reader to the PC
  • You will be able to view the files as follows:
ESP8266 NodeMCU log to Micro SD Card multiple files

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!