ESP32 S3 UNO - Micro SD Card

This ESP32 S3 Uno Micro SD Card tutorial shows you how to store and load files with an SD card module and the ESP32 S3 Uno form board. You will log text to a file, read it back character by character or line by line, and replace old data, all with simple Arduino code.

In this tutorial, you will:

  1. Wire a Micro SD Card module to the ESP32 S3 Uno over SPI
  2. Create a file on the Micro SD Card if it does not exist yet
  3. Write data to a file and read it back, one character at a time
  4. Read a text file line by line
  5. Append to a file, or overwrite it with new content
ESP32 S3 Uno Micro SD Card

Hardware Preparation

1×ESP32 S3 Uno-form Board
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

A microcontroller has very little memory of its own, so a Micro SD Card is a cheap way to save a lot of data. The Micro SD Card Module is the bridge between the two: it holds the card and lets the ESP32 S3 Uno talk to it over the SPI bus.

You can use it for data logging, saving settings, or keeping files that your project reads at startup.

Pinout

Micro SD Card Module Pinout

The module has 6 pins. Two of them are for power, and four are for SPI communication.

Power pins

The VCC pin goes to the 5V pin of the ESP32 S3 Uno, and the GND pin goes to GND. The module has its own 3.3V regulator for the card.

SPI pins

The MISO, MOSI and SCK pins connect to the matching SPI pins of the ESP32 S3 Uno. On this board, D11, D12 and D13 are the default SPI pins, just like on a classic Uno layout.

SS (chip select) pin

The SS pin (also called CS) tells the card when the board is talking to it. You can wire it to any free digital pin, as long as you use the same pin in the code. This tutorial uses D4 (GPIO19).

Preparation

Before you start, get the card ready on your computer. This saves you from the most common "SD card failed" error.

  1. Put the Micro SD Card into a USB 3.0 SD Card Reader and plug it into your computer.
  2. Make sure the card is formatted as FAT16 or FAT32. If not, format it with your computer's disk tool.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D4, D11, D12, D13) and their GPIO numbers when you wire the SD card module.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Connect the module as shown below. The SPI lines use the D11 to D13 headers, and the chip select line uses D4.

The wiring diagram between ESP32 S3 Uno Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

Micro SD Card Module Pin ESP32 S3 Uno Pin
VCC 5V
GND GND
MISO D12 (GPIO13)
MOSI D11 (GPIO11)
SCK D13 (GPIO12)
SS (CS) D4 (GPIO19)

WARNING

The ESP32 S3 Uno pins are 3.3V only and are NOT 5V tolerant. The common Micro SD Card Module has a level-shifter chip, so its MISO output is 3.3V even when VCC is 5V. If your module has no level shifter (a bare SD card socket), power it from 3.3V, never from 5V.

※ NOTE THAT:

If you have a shield with a Micro SD Card slot (for example, an Ethernet shield), you can use the slot on the shield instead of a separate module. Check that the shield uses D11 to D13 for SPI and D4 for CS. Some shields take SPI only from the ICSP header, and those may not work on this board.

ESP32 S3 Uno - How to open a file on Micro SD Card and create if not existed

The first step in any SD card project is to open a file. This sketch checks whether a file named arduino.txt exists on the card, and creates an empty one if it does not.

ESP32 S3 Uno Code

The sketch starts the SD card on the CS pin, then uses SD.exists() to look for the file. If the file is missing, it opens the file in write mode and closes it right away, which creates an empty file.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 19 // The ESP32 S3 Uno pin connected to the CS pin of SD card module (D4 = GPIO19) File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // on the ESP32 S3 Uno, file paths must start with "/" if (!SD.exists("/arduino.txt")) { Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file...")); // create a new file by opening a new file and immediately close it myFile = SD.open("/arduino.txt", FILE_WRITE); myFile.close(); } // recheck if file is created or not if (SD.exists("/arduino.txt")) Serial.println(F("arduino.txt exists on SD Card.")); else Serial.println(F("arduino.txt doesn't exist on SD Card.")); } void loop() { }

On the ESP32 S3 Uno, every file path must start with a slash, like "/arduino.txt". Many other boards accept "arduino.txt", but the SD library in the ESP32 core needs the full path from the root folder. Without the slash, SD.open() and SD.exists() fail.

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Insert the card into the Micro SD Card module.
  3. Wire it up as shown in the diagram.
  4. Connect the board to your computer with a USB Type-C cable.
  5. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  6. Open the Serial Monitor and set the baud rate to 9600.
  7. Copy the code above and paste it into the Arduino IDE. The SD library is built into the ESP32 core, so you do not need to install anything.
  8. Upload the code by clicking the Upload button.
  9. Check the Serial Monitor. After the first upload, you should see:
∞
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
SD Card is ready arduino.txt doesn't exist. Creating arduino.txt file... arduino.txt exists on SD Card.
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Press the RESET button to run the sketch again. Now the file is already there:
∞
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
SD Card is ready arduino.txt exists on SD Card.
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

If you open the Serial Monitor after the first upload has finished, you may miss the output. Press the RESET button on the board to run the sketch again.

  1. Check the file on your computer. Take the card out of the module, put it in the USB SD Card reader, plug it into your computer, and look for arduino.txt.
  2. Tip: change the file name in the code (keep the "/" at the start) and upload again to create a second file.

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

Writing and reading are the two jobs you will do most often. This sketch adds two lines of text to arduino.txt, then reads the whole file back and prints it on the Serial Monitor one character at a time.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 19 // The ESP32 S3 Uno pin connected to the CS pin of SD card module (D4 = GPIO19) File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing (FILE_APPEND adds to the end; on the ESP32 S3 Uno, FILE_WRITE would erase the file) myFile = SD.open("/arduino.txt", FILE_APPEND); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to arduino.txt myFile.println("Learn ESP32 S3 Uno and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("/arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }

Notice that the file is opened with FILE_APPEND, not FILE_WRITE. On many other Arduino boards, FILE_WRITE adds new data to the end of the file. On the ESP32 S3 Uno, FILE_WRITE erases the file first, and FILE_APPEND is the mode that adds to the end. That is why this sketch uses FILE_APPEND.

After you upload the code, the Serial Monitor shows the file content:

∞
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
SD Card is ready Created by newbiely.com Learn ESP32 S3 Uno and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

The new text is added to the end of the file each time the sketch runs. If you reset the ESP32 S3 Uno, the two lines are written again, so you will see 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
SD Card is ready Created by newbiely.com Learn ESP32 S3 Uno and SD Card Created by newbiely.com Learn ESP32 S3 Uno and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

You can also take the card out and open arduino.txt on your computer with a USB SD Card reader.

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

Reading one line at a time is handy when each line holds one record, such as one sensor reading or one setting. This sketch appends the same two lines, then reads the file with readBytesUntil() and prints each line with its number.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 19 // The ESP32 S3 Uno pin connected to the CS pin of SD card module (D4 = GPIO19) File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); // open file for writing (FILE_APPEND adds to the end; on the ESP32 S3 Uno, FILE_WRITE would erase the file) myFile = SD.open("/arduino.txt", FILE_APPEND); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to arduino.txt myFile.println("Learn ESP32 S3 Uno and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("/arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }

The buffer holds up to 100 characters per line. If your lines are longer, make the line array and the limit in readBytesUntil() bigger.

The Serial Monitor shows:

∞
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
SD Card is ready Line 1: Created by newbiely.com Line 2: Learn ESP32 S3 Uno and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ NOTE THAT:

If the file already had content from an earlier sketch, you will see more lines, because this sketch also adds to the end of the file.

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

Sometimes you want a fresh file instead of a growing one, for example to save only the latest settings. The simple way is to delete the old file and then create a new one with the same name.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 19 // The ESP32 S3 Uno pin connected to the CS pin of SD card module (D4 = GPIO19) File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD Card is either missing or has failed!")); while (1); // don't do anything more: } Serial.println(F("SD Card is ready")); SD.remove("/arduino.txt"); // delete the file if existed // create new file by opening file for writing myFile = SD.open("/arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // write a line to arduino.txt myFile.println("Learn ESP32 S3 Uno and SD Card"); // write another line to arduino.txt myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open file for reading myFile = SD.open("/arduino.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("Error: Unable to open file arduino.txt")); } } void loop() { }

The sketch calls SD.remove("/arduino.txt") first, then opens the file with FILE_WRITE and writes two new lines. On the ESP32 S3 Uno, FILE_WRITE also starts from an empty file by itself, but the delete step keeps the idea clear and works the same on other boards.

The Serial Monitor shows:

∞
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
SD Card is ready Created by newbiely.com Learn ESP32 S3 Uno and SD Card
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Press the RESET button on the ESP32 S3 Uno.
  2. Check the Serial Monitor again. The content stays at two lines, because the old file is replaced every time.

You can also take the card out and check arduino.txt on your computer with a USB SD Card reader.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Function References

These pages explain the functions used in this tutorial in more detail.

FAQ

Why does the file name start with "/" on the ESP32 S3 Uno?

The SD library in the ESP32 core works with full paths from the root folder of the card. So you must write "/arduino.txt", not "arduino.txt". Without the slash, SD.open() returns an empty file object and SD.exists() returns false.

Why does FILE_WRITE erase my file on the ESP32 S3 Uno?

On the ESP32 core, FILE_WRITE opens the file in "w" mode, which clears it first. On the UNO R4 and other AVR-style boards, FILE_WRITE adds to the end. To add data to the end on the ESP32 S3 Uno, use FILE_APPEND.

Which pins does the Micro SD Card module use on the ESP32 S3 Uno?

It uses the SPI pins D11 (GPIO11) for MOSI, D12 (GPIO13) for MISO and D13 (GPIO12) for SCK. These are the default SPI pins of the ESP32 S3 Uno, so SD.begin() needs only the CS pin. This tutorial uses D4 (GPIO19) for CS.

What card size and format should I use?

Use a Micro SD Card formatted as FAT16 or FAT32. Cards up to 32 GB are usually FAT32 by default. Bigger cards often come as exFAT, so format them to FAT32 first.

Can I power the module from 3.3V instead of 5V?

A module with a regulator and level shifter is made for 5V, and on 3.3V it may not work well. A bare SD socket with no level shifter must use 3.3V. Check your module before you wire it.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them and write the GPIO number in code. For a second SPI device's CS pin or a data-logging sensor, GPIO35 to GPIO42 are a good choice, and GPIO15/16 are best for analog sensors. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
"SD Card is either missing or has failed!" Card not inserted, wrong wiring or wrong CS pin Insert the card fully, check the wiring table, and make sure PIN_SPI_CS is 19 (D4)
SD.begin() fails with a new card Card is formatted as exFAT or NTFS Format the card as FAT32 (or FAT16) on your computer
File is not found or cannot be opened File path has no leading slash Write the path as "/arduino.txt", not "arduino.txt"
Old data disappears after each write FILE_WRITE clears the file on the ESP32 S3 Uno Use FILE_APPEND to add data to the end of the file
Card works sometimes, then fails Loose jumper wires or weak power Use short wires, power the module from the 5V pin, and check the GND wire
Board resets or the card gets hot A bare SD socket is powered from 5V Power a module without a level shifter from 3.3V only
No COM port in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, or hold BOOT while pressing RESET
Nothing shows on the Serial Monitor Wrong baud rate or output sent before the monitor opened Set the baud rate to 9600 and press the RESET button

Learn More

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