Arduino Nano - Log Data with Timestamp to SD Card
This tutorial instructs you how to write log with timestamp to the Micro SD Card using Arduino Nano. Specifically, we will cover:
- Arduino Nano - Logging data with timestamp to a single file on Micro SD Card
- Arduino Nano - Logging data with timestamp to multiple files on Micro SD Card, one file per day
The time is obtained from a RTC module and stored in a Micro SD Card together with the data.
The information that is stored on the Micro SD Card can be any type of data. This could include:
- Data collected from a sensor
- A record of door lock accesses
In a nutshell, this tutorial takes the values from two analog pins as an illustration of data. It is straightforward to modify the code to fit any type of data.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of Micro SD Card Module and RTC Module
If you are unfamiliar with Micro SD Card Module and RTC module, including their pinouts, how they work, and how to program them, the following tutorials can help:
- Arduino Nano - Micro SD Card tutorial.
- Arduino Nano - Write Variable to SD Card tutorial.
- Arduino Nano - RTC module tutorial.
Wiring Diagram
- You can use male-to-female jumper wires to connect the micro SD Card module to Arduino Nano
This image is created using Fritzing. Click to enlarge image
- Or you can plug the micro SD Card module to breadboard and then use the male-to-male jumper wires
This image is created using Fritzing. Click to enlarge image
Arduino Nano - Log Data with Timestamp to Micro SD Card
Detailed Instructions
- Ensure that the Micro SD Card is formatted as either FAT16 or FAT32 (you can find instructions on how to do this via a Google search).
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
- Check the results in the Serial Monitor.
- 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 computer; it will appear as follows.
If you do not possess an USB SD Card reader, you can examine the content of the log file by executing the Arduino Nano Code below.
Arduino Nano - Log Data in multiple files
Logging to a single file can lead to a large file size over time and make it hard to review. The code below will split the log into multiple files, with:
- One file per day
- The filename is the date information in YYYYMMDD.txt format
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
- Plug the USB SD Card reader into your PC
- You will be able to view the files as follows: