ESP32 S3 - SD Card

Learn how to use a Micro SD Card with your ESP32 S3 to store and retrieve data for your IoT projects. This tutorial covers everything from wiring the SD Card module to reading, writing, and managing files.

What you'll build:

  1. A circuit connecting the ESP32 S3 to a Micro SD Card Module over SPI
  2. A sketch that opens or creates a file on the SD card
  3. Programs that write, read, and overwrite file contents
  4. A foundation for data-logging and file-management applications
ESP32 S3 - 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×Breadboard
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 that the ESP32 S3 can read and write card data reliably at 3.3 V logic levels.

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.

Pinout

The Micro SD Card Module exposes six pins that map directly onto the SPI bus of the ESP32 S3.

Micro SD Card Module Pinout
  1. VCC: Power supply — connect to the ESP32 S3 5 V pin
  2. GND: Ground — connect to any GND pin on the ESP32 S3
  3. MISO: Master In Slave Out — data line from the SD card to the ESP32 S3
  4. MOSI: Master Out Slave In — data line from the ESP32 S3 to the SD card
  5. SCK: Serial Clock — timing signal driven by the ESP32 S3
  6. SS: Slave Select — chip-enable line, connect to any spare digital pin defined as CS in your code

Preparation

Before starting, you need to prepare your Micro SD Card so the ESP32 S3 can access it correctly.

  1. Insert the Micro SD Card into a USB 3.0 SD Card Reader
  2. Connect the reader to your PC
  3. Format the Micro SD Card as FAT16 or FAT32 (search online for formatting instructions for your OS)
  4. Safely eject the card once formatting is complete
  5. Insert the formatted card into the Micro SD Card Module

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

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 open a file on Micro SD Card and create if not existed

Code Overview

The following code demonstrates how to initialize the SD card over SPI and open a named file, creating it automatically when it does not already exist. On each boot the sketch checks for the file, reports whether it was found or freshly created, and exits — giving you a clean, repeatable way to set up persistent storage before any data is written.

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-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; void setup() { Serial.begin(115200); 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.")); if (!SD.exists("/esp32.txt")) { Serial.println(F("esp32.txt doesn't exist. Creating esp32.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("/esp32.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("/esp32.txt")) Serial.println(F("esp32.txt exists on SD Card.")); else Serial.println(F("esp32.txt doesn't exist on SD Card.")); } 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. Open Serial Monitor: Open the Serial Monitor before uploading (Tools > Serial Monitor)
  9. Copy Code: Copy the code provided above
  10. Paste Code: Paste it into a new Arduino IDE sketch
  11. Upload: Click the Upload button to flash the code to your ESP32 S3
  12. View Results: Check the Serial Monitor to see the file creation status
  13. Pro Tip: Always open Serial Monitor before uploading so you capture every initialization message from the very first boot.

Serial Monitor Output

First run (file doesn't exist yet):

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:23:45] SD CARD INITIALIZED. [2026-06-16 10:23:46] esp32.txt doesn't exist. Creating esp32.txt file... [2026-06-16 10:23:46] esp32.txt exists on SD Card.
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Subsequent runs (file already exists):

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:25:12] SD CARD INITIALIZED. [2026-06-16 10:25:12] esp32.txt exists on SD Card.
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

You may NOT see the output on Serial Monitor for the first run if your first upload is done before opening Serial Monitor.

Verify on PC:

  • Remove the Micro SD Card from the module
  • Insert it into your USB SD Card reader
  • Connect the reader to your PC
  • Open the SD card folder and verify the esp32.txt file was created

ESP32 S3 - How to write/read data to/from a file on Micro SD Card

Code Overview

The following code shows how to write text data to a file and immediately read it back, printing every character to the Serial Monitor. It opens or creates the target file, writes two lines of content, then seeks back to the beginning and streams each character out — giving you a straightforward way to confirm that what was written is what gets read.

/* * 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-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; void setup() { Serial.begin(115200); 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.")); // open file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt 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() { }

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:30:22] SD CARD INITIALIZED. [2026-06-16 11:30:22] Created by newbiely.com [2026-06-16 11:30:22] Learn ESP32 S3 and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

The data will be appended to the end of the file by default. If you reboot the ESP32 S3 with the above code, the text will be appended again and the Serial Monitor will show more lines:

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:32:45] SD CARD INITIALIZED. [2026-06-16 11:32:45] Created by newbiely.com [2026-06-16 11:32:45] Learn ESP32 S3 and SD Card [2026-06-16 11:32:45] Created by newbiely.com [2026-06-16 11:32:45] Learn ESP32 S3 and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Verify the file:

  • Safely remove the Micro SD Card from the module
  • Insert it into your USB SD Card reader
  • Connect to your PC and open the file to view its contents

ESP32 S3 - How to read a file on Micro SD Card line-by-line

Code Overview

The following code demonstrates reading file contents line by line rather than character by character, which is far more practical when your file contains structured data or log entries. It uses readStringUntil('\n') to extract each line and numbers the output so you can instantly see how many lines the file contains.

/* * 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-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; void setup() { Serial.begin(115200); 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.")); // open file for writing myFile = SD.open("/esp32.txt", FILE_WRITE); if (myFile) { myFile.println("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt 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) { int line_count = 0; while (myFile.available()) { char line[100]; // maximum is 100 characters, change it if needed int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // print the character to Serial Monitor // \n character is escaped by readBytesUntil function Serial.write('\n'); // print a new line charactor } myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file esp32.txt")); } } void loop() { }

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 14:15:33] SD CARD INITIALIZED. [2026-06-16 14:15:33] Line 1: Created by newbiely.com [2026-06-16 14:15:33] Line 2: Learn ESP32 S3 and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

You may see more lines on Serial Monitor if the content of the file was not deleted before running this sketch.

ESP32 S3 - How to overwrite a file on Micro SD Card

Code Overview

The following code shows the simplest technique for overwriting a file: delete the existing file and create a new one with the same name before writing fresh content. Because the SD library appends by default, this delete-and-recreate pattern is the most reliable way to ensure the file contains only the data from the current run.

By default, content will append to the end of the file. The simplest way to overwrite a file is to delete the existing file and create a new one with the same name.

/* * 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-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; void setup() { Serial.begin(115200); 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.")); 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("Created by esp32io.com"); // write a line to esp32.txt myFile.println("Learn ESP32 and SD Card"); // write another line to esp32.txt 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() { }

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 16:42:18] SD CARD INITIALIZED. [2026-06-16 16:42:18] Created by newbiely.com [2026-06-16 16:42:18] Learn ESP32 S3 and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Test the overwrite:

  • Press the reset button on your ESP32 S3
  • Check the Serial Monitor output
  • Verify that the content is NOT duplicated
  • The file should contain only one set of lines

Verify on PC:

  • Remove the Micro SD Card from the module
  • Insert it into your USB SD Card reader
  • Connect to your PC and open the file
  • Confirm the content matches the expected output

Application Ideas

The ESP32 S3's processing power and built-in wireless connectivity make it an excellent companion for SD card–based storage in real-world deployments.

  1. Climate Data Logger: Record temperature and humidity readings at regular intervals for long-term environmental monitoring.
  2. Standalone Sensor Station: Store sensor readings locally without relying on a WiFi connection, then upload in batch when connectivity is available.
  3. GPS Location Logger: Log GPS coordinates with timestamps to track routes or asset movements.
  4. Security Event Log: Save motion-detection or door-contact events with timestamps to a rolling log file.
  5. Agricultural Monitor: Record soil-moisture, light, and temperature data over growing seasons for analysis.
  6. Power Consumption Meter: Store daily energy-usage statistics for appliances or solar installations.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

...VIDEO esp32-s3/micro-sd-card.mp4

...VIDEO

Challenge Yourself

The examples above give you a solid foundation — now push further with these progressively demanding exercises.

  1. Beginner: Modify the open-file sketch to create a file with your own custom name and write a personal message to it.
  2. Beginner: Add a timestamp to each written line using the millis() function so you can see when each entry was recorded.
  3. Intermediate: Write a sketch that logs simulated sensor values every five seconds and appends them with timestamps to a CSV file on the SD card.
  4. Intermediate: Build a Serial Monitor menu that lets you choose to read, write, or delete a file by sending single-character commands.
  5. Advanced: Implement a circular log that keeps only the last 100 entries, automatically deleting the oldest record when the limit is reached.
  6. Advanced: Create a multi-sensor data logger that writes each sensor's readings to a separate file and rotates files daily using a real-time clock module.

Function References

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