ESP32 MicroPython Log Data with Timestamp to SD Card
This guide shows you how to build a data logger that records timestamped entries to a Micro SD Card using ESP32 and MicroPython. Here is what we will cover:
- Recording timestamped data into a single log file on the Micro SD Card.
- Organizing logs across multiple files on the Micro SD Card, creating a separate file for each day.
The date and time are obtained from a DS3231 RTC module and written to the Micro SD Card together with the sensor readings.
The logged data can include any type of measurement, for example:
- Readings from temperature, humidity, or other sensors.
- Access events from a door lock system.
To keep things simple, this guide uses readings from two analog input pins as sample data. You can easily modify the code to log any other type of information.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 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
Need help getting started with the Micro SD Card Module or RTC module? Check out these step-by-step tutorials covering pinouts, wiring, and programming:
- ESP32 MicroPython - Micro SD Card tutorial.
- ESP32 MicroPython - RTC module tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
The wiring table below shows how to connect the Micro SD Card Module and the DS3231 RTC Module to the ESP32:
| Micro SD Card Module | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| MISO | GPIO19 |
| MOSI | GPIO23 |
| SCK | GPIO18 |
| CS | GPIO5 |
| DS3231 RTC Module | ESP32 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GPIO21 |
| SCL | GPIO22 |
※ NOTE THAT:
If your shield (such as an Ethernet shield) already has a built-in Micro SD Card slot, you can skip the external module and simply insert the card directly into the shield.
ESP32 MicroPython - Recording Timestamped Data to Micro SD Card
Detailed Instructions
Here's instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Confirm that MicroPython firmware is loaded on your ESP32 board.
- If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
- Do the wiring as above image.
- Connect the ESP32 board to your computer with a USB cable.
- Open Thonny IDE on your computer.
- In Thonny IDE, go to Tools Options.
- Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
- Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM3 on Windows or /dev/ttyACM0 on Linux).
- Navigate to the Tools Manage packages on the Thonny IDE.
- Search "sdcard", then find the sdcard library provided by micropython-lib.
- Click on sdcard, then click Install button to install the SD card library.

- Ensure the Micro SD Card is formatted as FAT16 or FAT32 (look up instructions online if needed)
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your ESP32 by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device.
- Name the file main.py.
- Click the green Run button (or press F5) to execute the script.
- Check out the message in the Shell at the bottom of Thonny.
- Remove the Micro SD Card from the module
- Plug it into a USB SD Card reader connected to your PC
- Open the log.txt file on your computer — its content should look like this:

If you don't have a USB SD Card reader, you can verify the log file contents by running the following MicroPython code on the ESP32.
ESP32 MicroPython - Logging to Separate Daily Files
Storing all logs in one file causes it to grow large over time, making it harder to manage. The code below splits logs into multiple files:
- A new file is created for each day
- Each filename follows the format: YYYYMMDD.txt
After running for an extended period, if you:
- Remove the Micro SD Card from the module
- Plug it into a USB SD Card reader connected to your PC
- You will find multiple log files as shown below:
