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:

  1. A circuit connecting the ESP32 S3 to a Micro SD Card Module over SPI
  2. A sketch that writes multiple variable types (int, float, string, char array, byte array) to a file on the SD card
  3. A sketch that stores variables as key-value pairs for structured, human-readable configuration files
  4. A foundation for data-logging and configuration-storage applications on the ESP32 S3
ESP32 S3 - Write Variable to 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×Jumper Wires
1×Optionally, MicroSD to SD Memory Card Adapter

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

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.

The wiring diagram between ESP32 S3 Micro SD Card Module

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

/* * 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-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 10 // The ESP32 S3 pin connected to the CS pin of SD card module File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); SD.remove("/esp32.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println(myInt); // write int variable to SD card in line myFile.println(myFloat); // write float variable to SD card in line myFile.println(myString); // write String variable to SD card in line myFile.println(myCharArray); // write char array to SD card in line myFile.write(myByteArray, 5); myFile.write("\n"); // new line for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("/esp32.txt", 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 esp32.txt")); } } void loop() { }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. 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).
  3. Wire the Circuit: Connect the Micro SD Card 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. Copy Code: Copy the code provided above.
  9. Paste Code: Paste it into a new Arduino IDE sketch.
  10. Upload: Click the Upload button to flash the code to your ESP32 S3.
  11. View Results: Open the Serial Monitor to see the output.
  12. 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.
  13. Pro Tip: Always open Serial Monitor before uploading so you capture every initialization 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
[2026-06-16 10:15:22] SD CARD INITIALIZED. [2026-06-16 10:15:22] -------------------- [2026-06-16 10:15:22] -52 [2026-06-16 10:15:22] -12.70 [2026-06-16 10:15:22] HELLO [2026-06-16 10:15:22] newbiely.com [2026-06-16 10:15:22] 12345 [2026-06-16 10:15:22] 1,2,3,4,5
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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 writes variable to Micro SD Card

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

/* * 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-write-variable-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 10 // The ESP32 S3 pin connected to the CS pin of SD card module File myFile; int myInt = -52; float myFloat = -12.7; String myString = "HELLO"; char myCharArray[] = "esp32io.com"; byte myByteArray[] = {'1', '2', '3', '4', '5'}; 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.")); Serial.println(F("--------------------")); SD.remove("/esp32.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.print("myInt="); // write key to SD card myFile.println(myInt); // write int variable to SD card in line myFile.print("myFloat="); // write key to SD card myFile.println(myFloat); // write float variable to SD card in line myFile.print("myString="); // write key to SD card myFile.println(myString); // write String variable to SD card in line myFile.print("myCharArray="); // write key to SD card myFile.println(myCharArray); // write char array to SD card in line myFile.print("myByteArray="); // write key to SD card myFile.write(myByteArray, 5); myFile.write("\n"); // new line myFile.print("myByteArray2="); // write key to SD card for (int i = 0; i < 5; i++) { myFile.write(myByteArray[i]); // new line if (i < 4) myFile.write(","); // comma } myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } // open file for reading myFile = SD.open("/esp32.txt", 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 esp32.txt")); } } void loop() { }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Prepare the SD Card: Insert the formatted Micro SD Card into the SD Card Module.
  3. Wire the Circuit: Connect the Micro SD Card 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. Copy Code: Copy the code provided above.
  9. Paste Code: Paste it into a new Arduino IDE sketch.
  10. Upload: Click the Upload button to flash the code to your ESP32 S3.
  11. View Results: Open the Serial Monitor to see the key-value output.
  12. Check the SD Card: Remove the card and verify the file on your PC.
  13. 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:

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
[2026-06-16 11:42:05] SD CARD INITIALIZED. [2026-06-16 11:42:05] -------------------- [2026-06-16 11:42:05] myInt=-52 [2026-06-16 11:42:05] myFloat=-12.70 [2026-06-16 11:42:05] myString=HELLO [2026-06-16 11:42:05] myCharArray=newbiely.com [2026-06-16 11:42:05] myByteArray=12345 [2026-06-16 11:42:05] myByteArray2=1,2,3,4,5
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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
ESP32 S3 writes key-value to Micro SD Card

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.

  1. Sensor Data Logger: Periodically write temperature, humidity, or pressure readings to the SD card for long-term trend analysis.
  2. Configuration Storage: Save user-defined settings as key-value pairs so the device restores its state after a power cycle.
  3. Calibration Records: Store calibration coefficients for analog sensors so they survive firmware updates and resets.
  4. Event Timestamping: Write event data (door openings, button presses, motion triggers) with timestamps for security or audit logs.
  5. 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.
  6. 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.

  1. Beginner: Modify the variable-writing sketch to use your own variable names and values, then verify the output on the SD card.
  2. Beginner: Add a line counter to the file so each written entry is numbered sequentially across multiple reboots.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

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