ESP32 S3 - Write Variable to SD Card
Learn how to write different types of variables directly to a Micro SD Card using the ESP32 S3, enabling persistent storage for sensor readings, configuration values, and structured data. This tutorial covers writing integers, floats, strings, character arrays, byte arrays, and key-value pairs to files on the SD card.
What you'll build:
- A circuit connecting the ESP32 S3 to a Micro SD Card Module over SPI
- A sketch that writes multiple variable types (int, float, string, char array, byte array) to a file on the SD card
- A sketch that stores variables as key-value pairs for structured, human-readable configuration files
- A foundation for data-logging and configuration-storage applications on the ESP32 S3

Hardware Preparation
Or you can buy the following kits:
| 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
The Micro SD Card Module is an interface board that bridges your ESP32 S3 to a Micro SD Card using the SPI communication protocol. It handles voltage regulation and signal translation so the ESP32 S3 can read and write card data reliably at 3.3 V logic levels, making it an essential component for any project that needs local persistent storage.
Key Specifications
The module communicates over SPI and supports Micro SD Cards formatted as FAT16 or FAT32. It accepts an operating voltage between 3.3 V and 5 V, making it compatible with a wide range of microcontrollers. The six-pin interface exposes VCC, GND, MISO, MOSI, SCK, and SS lines, and the module is well-suited for data-logging, configuration storage, and serving static web content from standalone IoT devices.
Unfamiliar with the Micro SD Card Module, including its pinout, functionality, and programming? Learn all about it in the ESP32 S3 - Micro SD Card tutorial.
Wiring Diagram
Connect the Micro SD Card Module to the ESP32 S3 following the diagram below, keeping all wires short and neat to reduce noise on the SPI bus. Double-check each connection before powering the board.

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
If you use an Ethernet shield or any shield that has a Micro SD Card Holder, you do not need to use the Micro SD Card Module. You just need to insert the Micro SD Card into the Micro SD Card Holder on the shield.
Safety Notes
Always verify that the SD card is fully seated in the module before running any code, as a loose card will cause initialization failures that can be difficult to diagnose. Use a quality, name-brand Micro SD Card to avoid intermittent read/write errors, and never remove the card while the ESP32 S3 is actively writing data to prevent file-system corruption.
| Micro SD Module Pin | ESP32 S3 Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| MISO | GPIO13 (MISO) |
| MOSI | GPIO11 (MOSI) |
| SCK | GPIO12 (SCK) |
| SS | GPIO10 (CS) |
ESP32 S3 - How to Write a Variable to a File on Micro SD Card
Code Overview
The following code demonstrates how to write multiple variable types to a single file on the Micro SD Card using the ESP32 S3. It writes an integer, a float, a string, a character array, and a byte array — each on its own line — giving you a practical template for storing diverse data types in a single persistent file.
The sketch covers:
- Writing an int variable to the Micro SD Card
- Writing a float variable to the Micro SD Card
- Writing a string variable to the Micro SD Card
- Writing a char array to the Micro SD Card
- Writing a byte array to the Micro SD Card
ESP32 S3 Code
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Prepare the SD Card: Insert the formatted Micro SD Card into the SD Card Module. Make sure it is formatted as FAT16 or FAT32 (search online for formatting instructions for your OS).
- Wire the Circuit: Connect the Micro SD Card Module to the ESP32 S3 following the wiring diagram above.
- Connect ESP32 S3: Plug the ESP32 S3 into your PC using the USB Type-C cable.
- Open Arduino IDE: Launch Arduino IDE on your computer.
- Select Board: Choose ESP32 S3 from the board menu.
- Select Port: Choose the correct COM port for your ESP32 S3.
- Copy Code: Copy the code provided above.
- Paste Code: Paste it into a new Arduino IDE sketch.
- Upload: Click the Upload button to flash the code to your ESP32 S3.
- View Results: Open the Serial Monitor to see the output.
- Check the SD Card: Detach the Micro SD Card from the module, insert it into the USB SD Card reader, connect to your PC, and open the esp32.txt file to verify the written values.
- Pro Tip: Always open Serial Monitor before uploading so you capture every initialization message from the very first boot.
Serial Monitor Output
Expected output:
Verify on 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 the PC
- Open the esp32.txt file on your PC to confirm the written values

ESP32 S3 - How to Write a Key-Value Pair to a File on Micro SD Card
Code Overview
The following code shows how to write each variable to the Micro SD Card as a key-value pair, producing a structured, human-readable file that is easy to parse back into your application. This format is especially useful for storing configuration settings that the ESP32 S3 can read back on startup using the ESP32 S3 - Read Config from SD Card tutorial.
ESP32 S3 Code
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Prepare the SD Card: Insert the formatted Micro SD Card into the SD Card Module.
- Wire the Circuit: Connect the Micro SD Card Module to the ESP32 S3 following the wiring diagram above.
- Connect ESP32 S3: Plug the ESP32 S3 into your PC using the USB Type-C cable.
- Open Arduino IDE: Launch Arduino IDE on your computer.
- Select Board: Choose ESP32 S3 from the board menu.
- Select Port: Choose the correct COM port for your ESP32 S3.
- Copy Code: Copy the code provided above.
- Paste Code: Paste it into a new Arduino IDE sketch.
- Upload: Click the Upload button to flash the code to your ESP32 S3.
- View Results: Open the Serial Monitor to see the key-value output.
- Check the SD Card: Remove the card and verify the file on your PC.
- Pro Tip: The key-value format pairs well with the Read Config tutorial, allowing you to save and restore device settings across reboots without reflashing.
Serial Monitor Output
Expected output:
Verify on 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 the PC
- Open the esp32.txt file on your PC to confirm the key-value pairs

Application Ideas
The ESP32 S3's processing power and built-in wireless connectivity make it an excellent platform for SD card–based variable storage in real-world deployments.
- Sensor Data Logger: Periodically write temperature, humidity, or pressure readings to the SD card for long-term trend analysis.
- Configuration Storage: Save user-defined settings as key-value pairs so the device restores its state after a power cycle.
- Calibration Records: Store calibration coefficients for analog sensors so they survive firmware updates and resets.
- Event Timestamping: Write event data (door openings, button presses, motion triggers) with timestamps for security or audit logs.
- Remote Deployment Logger: Deploy an ESP32 S3 in a location without internet access and collect data locally, then retrieve the SD card for batch upload.
- Multi-Sensor Aggregator: Combine readings from multiple sensors into a structured CSV file on the SD card for spreadsheet analysis.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenge Yourself
These exercises build progressively on the examples above to deepen your understanding of SD card variable storage with the ESP32 S3.
- Beginner: Modify the variable-writing sketch to use your own variable names and values, then verify the output on the SD card.
- Beginner: Add a line counter to the file so each written entry is numbered sequentially across multiple reboots.
- Intermediate: Write a sketch that logs the current value of millis() together with a simulated sensor reading every ten seconds, appending each entry to a growing CSV file.
- Intermediate: Combine the write-variable and read-config tutorials to build a round-trip test: write settings to the SD card, reboot, read them back, and print confirmation to the Serial Monitor.
- Advanced: Implement a rolling log that caps the file at 500 lines by reading the existing content, discarding the oldest entries, and rewriting the file each time a new variable set is appended.
- Advanced: Create a multi-variable logger that stores readings from three different sensors in separate key-value files and rotates each file daily using a timestamp-based filename scheme.