ESP32 S3 - Log Data with Timestamp to SD Card

Learn how to build a timestamped data logger with the ESP32 S3 and a Micro SD Card Module. This tutorial shows you how to capture real-world data and pair it with accurate date and time information sourced from an RTC module before writing it persistently to the card.

What you'll build:

  1. A circuit connecting the ESP32 S3 to a Micro SD Card Module and a DS3231 RTC module
  2. A sketch that logs timestamped data entries to a single file on the Micro SD Card
  3. A second sketch that splits log output into daily files named by date
  4. A foundation for long-running IoT data-logging and monitoring applications
ESP32 S3 Log Data with Timestamp to Micro SD Card

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (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

Or you can buy the following kits:

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 and RTC Module

The Micro SD Card Module connects to the ESP32 S3 over the SPI bus and provides reliable FAT16/FAT32 file-system access for storing log files that can grow over days or weeks. The DS3231 RTC module keeps highly accurate time independently of the ESP32 S3's power state, ensuring that every log entry carries a meaningful date and time even after a board reset or power cycle. Together, these two modules form the backbone of a robust, standalone data-logging solution.

Key Specifications

Unfamiliar with the Micro SD Card Module or the RTC module, including their pinouts, functionality, and programming? Explore the comprehensive tutorials on these topics below:

Wiring Diagram

Connect the Micro SD Card Module and the DS3231 RTC module to the ESP32 S3 following the diagram below, keeping wires short to minimise noise on the SPI bus. Double-check every connection before powering the board to avoid initialisation errors.

The wiring diagram between ESP32 S3 Micro SD Card Module RTC

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always ensure the Micro SD Card is fully seated in the module and the card is formatted as FAT16 or FAT32 before running any logging code, as an unformatted or loose card will prevent initialisation. Never remove the card while the ESP32 S3 is actively writing data, as doing so can corrupt the file system and destroy previously recorded log entries.

Micro SD Module Pin ESP32 S3 Pin
VCC 5V
GND GND
MISO GPIO13 (MISO)
MOSI GPIO11 (MOSI)
SCK GPIO12 (SCK)
SS GPIO10 (CS)
DS3231 Pin ESP32 S3 Pin
VCC 3.3V
GND GND
SDA GPIO8 (SDA)
SCL GPIO9 (SCL)

ESP32 S3 - Log Data with Timestamp to Micro SD Card

Code Overview

The following code reads values from two analog pins on every loop iteration and appends a timestamped CSV line to a single log file on the Micro SD Card. The date and time are fetched from the DS3231 RTC module, so each record carries accurate wall-clock information that makes the log useful for analysis long after collection. For simplicity, analog pin readings are used as the example dataset — the same pattern applies to any sensor you wire into the circuit.

ESP32 S3 Code

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 10 // The ESP32 S3 pin connected to the CS pin of SD card module #define FILE_NAME "/log.txt" RTC_DS3231 rtc; File myFile; void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // SETUP RTC MODULE if (!rtc.begin()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } delay(2000); // delay 2 seconds }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Prepare the SD Card: Format the Micro SD Card as FAT16 or FAT32, then insert it into the SD Card Module
  3. Wire the Circuit: Connect the Micro SD Card Module and DS3231 RTC module to the ESP32 S3 following the wiring diagram above
  4. Connect ESP32 S3: Plug the ESP32 S3 into your PC using the USB Type-C cable
  5. Open Arduino IDE: Launch Arduino IDE on your computer
  6. Select Board: Choose ESP32 S3 from the board menu
  7. Select Port: Choose the correct COM port for your ESP32 S3
  8. Open Serial Monitor: Open the Serial Monitor before uploading (Tools > Serial Monitor)
  9. Copy Code: Copy the code provided above
  10. Paste Code: Paste it into a new Arduino IDE sketch
  11. Upload: Click the Upload button to flash the code to your ESP32 S3
  12. View Results: Check the Serial Monitor to confirm the log is being written
  13. Read the Log: Detach the Micro SD Card, insert it into a USB SD Card Reader, connect it to your PC, and open log.txt to review the recorded entries
  14. Pro Tip: Always open Serial Monitor before uploading so you capture every initialisation message from the very first boot.

Serial Monitor Output

Expected output:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

View the log on your PC:

  • Detach the Micro SD Card from the Micro SD Card module
  • Insert the Micro SD Card into a USB SD Card Reader
  • Connect the USB SD Card Reader to your PC
  • Open the log.txt file — it will look similar to the image below
ESP32 S3 Log Data with Timestamp to Micro SD Card

If you do not have a USB SD Card Reader, you can read the contents of the log file directly by uploading the following ESP32 S3 code.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 10 // The ESP32 S3 pin connected to the CS pin of SD card module #define FILE_NAME "/log.txt" File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } } void loop() { }

ESP32 S3 - Log Data in Multiple Files

Writing all log entries to a single file causes the file to grow indefinitely and makes it difficult to locate entries from a specific date. The following code addresses this by creating one log file per day, with each filename set to the date in YYYYMMDD.txt format so files are easy to identify and sort.

Code Overview

The following code queries the DS3231 RTC module on every loop iteration to determine the current date, then opens or creates the matching daily log file and appends a timestamped record. When midnight passes and the date changes, the code automatically switches to a new file — keeping your logs neatly partitioned without any manual intervention.

ESP32 S3 Code

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 10 // The ESP32 S3 pin connected to the CS pin of SD card module RTC_DS3231 rtc; File myFile; char filename[] = "/yyyymmdd.txt"; // filename (without extension) should not exceed 8 chars void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // SETUP RTC MODULE if (!rtc.begin()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } 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[1] = (year / 1000) + '0'; filename[2] = ((year % 1000) / 100) + '0'; filename[3] = ((year % 100) / 10) + '0'; filename[4] = (year % 10) + '0'; filename[5] = (month / 10) + '0'; filename[6] = (month % 10) + '0'; filename[7] = (day / 10) + '0'; filename[8] = (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: Issue encountered while attempting to open the file ")); Serial.println(filename); } delay(2000); // delay 2 seconds }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Prepare the SD Card: Format the Micro SD Card as FAT16 or FAT32, then insert it into the SD Card Module
  3. Wire the Circuit: Connect the Micro SD Card Module and DS3231 RTC module to the ESP32 S3 following the wiring diagram above
  4. Connect ESP32 S3: Plug the ESP32 S3 into your PC using the USB Type-C cable
  5. Upload Code: Copy, paste, and upload the multi-file logging code above
  6. Run Overnight: Let the ESP32 S3 run across midnight to observe automatic daily file rotation
  7. Read the Files: After a long run, remove the Micro SD Card, insert it into a USB SD Card Reader, and connect it to your PC — you will see one file per day as shown below
ESP32 S3 Log Data with Timestamp to Multiple Files
  1. Pro Tip: Set the RTC module to the correct time before your first long logging run so every timestamp is accurate from the very beginning.

Serial Monitor Output

Expected output while running:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
SD CARD INITIALIZED. -------------------- Writing log to 20260616.txt Writing log to 20260616.txt Writing log to 20260616.txt Writing log to 20260617.txt Writing log to 20260617.txt
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application Ideas

The ESP32 S3's processing power and built-in wireless connectivity make it an ideal platform for building long-running, timestamped data-logging systems in real-world environments.

  1. Environmental Monitor: Log temperature, humidity, and air-quality readings at regular intervals for indoor or outdoor climate studies.
  2. Access Control Log: Record every door-lock event with a precise timestamp to maintain a tamper-evident security audit trail.
  3. Agricultural Data Logger: Track soil moisture, light intensity, and temperature across growing seasons to optimise crop conditions.
  4. Industrial Sensor Recorder: Capture vibration, pressure, or current readings from machinery to support predictive maintenance programmes.
  5. Energy Consumption Tracker: Store daily power-usage statistics from a smart meter or current sensor for billing verification and efficiency analysis.
  6. Weather Station: Log barometric pressure, wind speed, and rainfall data with timestamps for local weather pattern analysis.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

The logging foundations above are ready to extend — try these progressively demanding exercises to deepen your skills.

  1. Beginner: Modify the single-file logger to write a header row (e.g., Timestamp,Value1,Value2) as the very first line so the CSV file opens correctly in spreadsheet software.
  2. Beginner: Add a third data column to the log by reading a real sensor (such as a DHT11 temperature sensor) instead of an analog pin.
  3. Intermediate: Implement a maximum file-size check so the logger automatically creates a new file (e.g., log_001.txt, log_002.txt) when the current file exceeds 1 MB.
  4. Intermediate: Add a WiFi upload step that, once per hour, reads the current log file and POSTs its contents to a remote server or cloud storage bucket.
  5. Advanced: Build a self-healing logger that detects SD card initialisation failures, retries several times with back-off delays, and sends an alert via the Serial Monitor when the card cannot be reached.
  6. Advanced: Combine the multi-file daily logger with a web server so you can browse and download individual day files from a browser on your local network without removing the SD card.

※ 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!