Raspberry Pi Pico - Log Data with Timestamp to SD Card
This tutorial demonstrates how to create a data logger on the Raspberry Pi Pico using MicroPython. The main topics include:
- Writing timestamped records into a single log file on the Micro SD Card
- Splitting logs into separate daily files on the Micro SD Card (one file per day)
Timestamps are retrieved from a DS3231 RTC module and recorded alongside each data entry on the Micro SD Card.
The data you log could be virtually anything, for example:
- Sensor measurements (temperature, humidity, light, etc.)
- Event counts from a security lock or motion detector
For simplicity, this tutorial reads values from two analog input pins as sample data. Feel free to adapt the code to log whatever information your project requires.

Hardware Preparation
Or you can buy the following 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
For detailed information on the Micro SD Card Module and RTC module — including pin descriptions, wiring, and programming examples — refer to these tutorials:
- Raspberry Pi Pico - Micro SD Card tutorial.
- Raspberry Pi Pico - RTC module tutorial.
Wiring Diagram
https://newbiely.com/images/tutorial/raspberry-pi-pico-micro-sd-card-module-rtc-wiring-diagram.jpg
The wiring table below shows how to connect the Micro SD Card Module and the DS3231 RTC Module to the Raspberry Pi Pico:
| Micro SD Card Module | Raspberry Pi Pico |
|---|---|
| VCC | VBUS (5V) |
| GND | GND |
| MISO | GP4 |
| MOSI | GP3 |
| SCK | GP2 |
| CS | GP5 |
| DS3231 RTC Module | Raspberry Pi Pico |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SDA | GP0 |
| SCL | GP1 |
※ NOTE THAT:
When using a shield that already includes a Micro SD Card slot (for example, an Ethernet shield), no additional SD Card Module is required — just slide the card straight into the shield's built-in slot.
Raspberry Pi Pico - Recording Timestamped Data to Micro SD Card
Detailed Instructions
Follow the steps below to set up and run the code:
- Verify that Thonny IDE is installed on your PC.
- Make sure MicroPython firmware has been flashed onto your Raspberry Pi Pico.
- First-time users should consult the Raspberry Pi Pico - Getting Started guide for setup details.
- Assemble the circuit following the wiring diagram above.
- Plug the Raspberry Pi Pico into your computer via USB cable.
- Open Thonny IDE.
- Go to Tools Options, then in the Interpreter tab pick MicroPython (Raspberry Pi Pico).
- Check that the correct serial port is selected — Thonny usually finds it automatically, but manual selection may be needed (for example COM3 on Windows or /dev/ttyACM0 on Linux).
- Paste the code above into the Thonny editor window.
- Save the file to the Raspberry Pi Pico:
- Press Ctrl+S or click Save.
- When prompted, select the Raspberry Pi Pico location.
- Name the file main.py.
- Hit the green Run button (or press F5). The script will start executing.
- Review the output in the Shell panel at the bottom of Thonny.
Saving your script as main.py in the Pico's root directory means it will launch automatically whenever the board powers up or resets — ideal for standalone projects. Scripts saved under a different name must be started manually through the Thonny Shell.
- Make sure the Micro SD Card is formatted as FAT16 or FAT32 (look up instructions online).
- Install the sdcard library on the Raspberry Pi Pico via Thonny IDE:
- Go to Tools Manage packages in Thonny IDE.
- Type sdcard in the search box and install the micropython-sdcard package.
- Paste the code into Thonny's editor.
- Click Run to execute the code on the Raspberry Pi Pico.
- Watch the Shell for output:
- 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 — its content should look like this:


If a USB SD Card reader is not available, the following MicroPython code lets you read the log file directly on the Raspberry Pi Pico.
Raspberry Pi Pico - Splitting Logs into Daily Files
Logging everything into a single file makes it grow continuously, which becomes impractical over time. The code below addresses this by creating separate files:
- A new log file is generated each day
- File names follow the pattern YYYYMMDD.txt
After the logger has been running for some time:
- Eject the Micro SD Card from the module.
- Insert the card into a USB SD Card reader and connect it to your PC.
- You will find multiple date-stamped log files on the card:
