ESP32 MicroPython Write Variable to SD Card
This guide shows you how to store different types of MicroPython variables onto a Micro SD Card using the ESP32. Here is what we will cover:
- Storing a string value on the Micro SD Card with ESP32 and MicroPython.
- Saving an integer value to the Micro SD Card with ESP32 and MicroPython.
- Recording a floating-point number on the Micro SD Card with ESP32 and MicroPython.
- Writing a byte array to the Micro SD Card with ESP32 and MicroPython.
- Saving data in key-value format on the Micro SD Card with ESP32 and MicroPython.
This guide gives you a complete walkthrough for persisting various variable types to a Micro SD Card, making it easy to store and retrieve data later. If you need to read key-value pairs back from the SD Card and parse them into integer, float, or string types, check out the ESP32 MicroPython - Read Config from SD Card tutorial.

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
New to the Micro SD Card Module? Find out everything about its pins, how it works, and how to program it in the ESP32 MicroPython - Micro SD Card 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 to the ESP32:
| Micro SD Card Module | ESP32 |
|---|---|
| VCC | 5V |
| GND | GND |
| MISO | GPIO19 |
| MOSI | GPIO23 |
| SCK | GPIO18 |
| CS | GPIO5 |
※ 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 - Saving Variables to a File on Micro SD Card
The following MicroPython code demonstrates how to:
- Save an int value to the Micro SD Card
- Save a float value to the Micro SD Card
- Save a string to the Micro SD Card
- Save a byte array to the 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 esp32.txt file on your computer — its content should look like this:

ESP32 MicroPython - Storing Key-Value Pairs on Micro SD Card
Detailed Instructions
- Copy the above code and paste it into Thonny's editor.
- Click the green Run button (or press F5) to run the script.
- View the output in the Shell at the bottom of Thonny.
- Take out the Micro SD Card from the module
- Connect it to your PC using a USB SD Card reader
- Open the esp32.txt file — you should see content like this:
