Arduino - Log Data With Timestamp To SD Card
Maybe your like
In this tutorial, we are going to learn how to write log with timestamp to the Micro SD Card using Arduino. In detail, we will learn:
- Arduino - How to log data with timestamp a file on Micro SD Card
- Arduino - How to log data with timestamp a to multiple files on Micro SD Card , one file per day
The time information is get from a RTC module and written to Micro SD Card along with data.
The data that is logged to the Micro SD Card can be anything. For example:
- Data read from any sensor
- History of door look access
To be simple, this tutorial reads value from two analog pins as an example of data. You can easily to adapt the code for any kind of data.
Hardware Required
| 1 | × | Official Arduino Uno |
| 1 | × | Alternatively, DIYables STEM V3, Fully Compatible with Arduino Uno R3 |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) |
| 1 | × | Micro SD Card |
| 1 | × | Micro SD Card Module |
| 1 | × | Optionally, MicroSD to SD Memory Card Adapter |
| 1 | × | Real-Time Clock DS3231 Module |
| 1 | × | CR2032 battery |
| 1 | × | Jumper Wires |
| 1 | × | Recommended: Screw Terminal Block Shield for Arduino Uno |
| 1 | × | Recommended: Breadboard Shield for Arduino Uno |
| 1 | × | Recommended: Enclosure for Arduino Uno |
| 1 | × | Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
About Micro SD Card Module and RTC Module
If you do not know about Micro SD Card Module and RTC module (pinout, how it works, how to program ...), learn about them in the following tutorials:
- Arduino - Micro SD Card tutorial.
- Arduino - Write Variable to SD Card tutorial.
- Arduino - RTC module tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
If you use an Ethernet shield or any shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. You just need to insert the Micro SD Card to the Micro SD Card Holder on the shield.
Arduino - Log Data with Timestamp to Micro SD Card
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 4 #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 }Quick Steps
- Make sure that the Micro SD Card is formatted FAT16 or FAT32 (Google for it)
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- See the result on Serial Monitor.
- Detach the Micro SD Card from the Micro SD Card module
- Insert the Micro SD Card to an USB SD Card reader
- Connect the USB SD Card reader to the PC
- Open the log.txt file on your PC, it looks like below
If you do not have an USB SD Card reader, you can check the content of log file by running the below Arduino Code.
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 4 #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() { }Arduino - Log Data in multiple files
Writing log to a single file results in a big file size overtime and makes it difficult to check. The below code will write log file in multiple:
- One file per day
- The filename is the date informationL YYYYMMDD.txt
After a long run, If you:
- Detach the Micro SD Card from the Micro SD Card module
- Insert the Micro SD Card to an USB SD Card reader
- Connect the USB SD Card reader to the PC
- You will see the files as follows:
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
Function References
- Serial
- Arduino SD Card Library Reference
The Best Arduino Starter Kit
- See the best Arduino kit for beginner
See Also
- Arduino - Micro SD Card
- Arduino - Write Variable to SD Card
- Arduino - Read Config from SD Card
※ OUR MESSAGES
- We are AVAILABLE for HIRE. See how to hire us to build your project
- If this tutorial is useful for you, please give us motivation to make more tutorials.
- You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
Tag » Arduino Micro Sd Card Adapter Example
-
SD Card Module With Arduino: How To Read/Write Data
-
In-Depth Tutorial To Interface Micro SD Card Module With Arduino
-
Guide To SD Card Module With Arduino - Random Nerd Tutorials
-
Micro SD Card Tutorial : 6 Steps - Arduino - Instructables
-
Using SD Card Module With Arduino | Read/Write/Data Logger
-
How To Use The "MicroSD Card Adapter" With The Arduino Uno
-
Arduino Micro SD Card Module Tutorial - Circuit Digest
-
Arduino Tutorial: SD Card Module Micro SD Tutorial DIY. - YouTube
-
MicroSD Card Interfacing With Arduino - ElectronicWings
-
Micro SD Card | Arduino Tutorial
-
SD Card Module W/ Arduino: How To Read/Write Data - ElectroPeak
-
Interfacing Catalex Micro SD Card Module With Arduino
-
Generic Micro SD Storage Board, Micro SD TF Card Memory Shield ...