ESP32 UNO - Micro SD Card

Need more storage than the chip gives you? Add a Micro SD Card to the ESP32 Uno and keep logs, settings and text files on a card you can pop into any PC. A cheap SPI card module joins the card to the ESP32 Uno form board, and the built-in SD library handles the file system for you.

What you will do:

  1. Connect a Micro SD Card module to the four SPI lines
  2. Make a new text file when it is missing
  3. Append text to a file and print it back byte by byte
  4. Read a file line by line with readBytesUntil()
  5. Replace a file with fresh content
ESP32 Uno Micro SD Card

Components You Need

1×ESP32 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 .

Micro SD Card Module Basics

By the end of this part, you know what each module pin does and how to prep the card.

The module is a small holder board. The card sits in its slot, and the ESP32 Uno sends commands to the card through the module over SPI. Most modules also carry a 3.3V regulator and a logic buffer chip, so the card always runs at a safe voltage.

Item Value
Interface SPI (4 signal lines)
Module supply 5V on VCC (on-board regulator gives the card 3.3V)
Card file system FAT16 or FAT32
Typical use data logs, config files, text and CSV storage

Pinout

Micro SD Card Module Pinout
Module Pin Role
VCC power input, wire it to 5V
GND ground, shared with the board
MISO data from the card to the ESP32 Uno
MOSI data from the ESP32 Uno to the card
SCK clock signal made by the ESP32 Uno
SS (CS) chip select, any free digital pin set in code

Good to know: the SPI pins on the ESP32 Uno headers (D11, D12, D13) are the chip's default VSPI pins, so the SD library finds them with no extra setup.

Preparation

Do this once on your computer, before the card goes into the module.

  1. Slide the Micro SD Card into the USB 3.0 SD Card Reader and plug the reader into your PC.
  2. Check the file system. It must be FAT16 or FAT32. Cards above 32 GB often ship as exFAT, so reformat those to FAT32.

ESP32 Uno Pinout

Your code talks to GPIO numbers, while the board is printed with Uno labels. Use this diagram to match the D4, D11, D12 and D13 headers to their GPIOs.

ESP32 Uno pinout diagram

How to Connect

After this step, the card module sits on the SPI bus and has its own chip select line.

The wiring diagram between ESP32 Uno Micro SD Card Module

This image is created using Fritzing. Click to enlarge image

Micro SD Card Module Pin ESP32 Uno Pin
VCC 5V
GND GND
MISO D12 (GPIO19)
MOSI D11 (GPIO23)
SCK D13 (GPIO18)
SS (CS) D4 (GPIO17)

Good to know: ESP32 Uno pins run at 3.3V and are not 5V tolerant.

WARNING

Feed 5V only to a module that has a regulator and a buffer chip (the usual blue or red board). Its MISO line then stays at 3.3V. A bare card socket with no parts on it has no buffer, so wire its VCC to the 3.3V pin, never to 5V.

※ NOTE THAT:

A shield with its own card slot, like an Ethernet shield, can replace the separate module. Just insert the card into the shield. Make sure the shield routes SPI to D11 to D13 and uses D4 for CS. A shield that takes SPI only from the ICSP header may not reach the ESP32 Uno SPI pins.

Open a File on the Micro SD Card (Create It If Missing)

Here you get a sketch that checks for arduino.txt and makes an empty copy when it is not on the card.

ESP32 Uno Code

The sketch starts the card on GPIO17, asks SD.exists() about the file, and opens it in write mode for a moment if it is missing.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 17 // D4 (GPIO17) is the chip select line of the SD card module 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); // stop here, the card is not usable } Serial.println(F("SD Card is ready")); if (!SD.exists("/arduino.txt")) { Serial.println(F("arduino.txt doesn't exist. Creating arduino.txt file...")); // opening a missing file in write mode makes an empty file, then close it myFile = SD.open("/arduino.txt", FILE_WRITE); myFile.close(); } // look for the file one more time to confirm it is there 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() { }
Code Part What It Does
SD.begin(PIN_SPI_CS) mounts the card through CS on GPIO17
SD.exists("/arduino.txt") returns true when the file is on the card
SD.open(..., FILE_WRITE) then close() makes an empty file
while (1) halts the sketch if the card did not mount

The result is a single empty file in the root folder of the card.

Good to know: the SD library in the esp32 core needs a full path, so every file name in this guide starts with a slash ("/arduino.txt"). Without it, open and exists calls fail.

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Insert the card into the slot of the module.
  3. Build the circuit by following the wiring table above.
  4. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  5. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  6. Open the Serial Monitor and choose 9600 baud.
  7. Paste the sketch into a new Arduino IDE window. No library install is needed, since SD ships with the esp32 core.
  8. Click Upload and wait for "Done uploading".
  9. Read the first run output:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 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
ESP32 Dev Module on COM15
2
  1. Press EN (reset) to run it again. Now the file is found at once:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD Card is ready arduino.txt exists on SD Card.
Ln 11, Col 1
ESP32 Dev Module on COM15
2

※ NOTE THAT:

The first output can scroll past before the Serial Monitor opens. Press the EN button and the sketch prints everything again.

  1. Confirm on the PC. Move the card to the USB reader, plug it in, and look for arduino.txt in the root folder.
  2. Pro tip: always unplug the USB cable or hold EN before you pull the card out. Pulling it during a write can damage the file system.

Write Text and Read It Back Byte by Byte

With this sketch, two text lines land in arduino.txt and the full file comes back on the Serial Monitor, one byte at a time.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 17 // D4 (GPIO17) is the chip select line of the SD card module 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); // stop here, the card is not usable } Serial.println(F("SD Card is ready")); // FILE_APPEND keeps the old text and adds new text at the end (FILE_WRITE on the ESP32 Uno clears the file) myFile = SD.open("/arduino.txt", FILE_APPEND); if (myFile) { myFile.println("Created by newbiely.com"); // first text line saved to the card myFile.println("Learn ESP32 Uno and SD Card"); // second text line saved to the card myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open the same file again, this time only to read it myFile = SD.open("/arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // take one byte from the card Serial.print(ch); // show that byte on the Serial Monitor } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
Code Part What It Does
SD.open("/arduino.txt", FILE_APPEND) opens the file and moves to its end
myFile.println(...) saves one line of text plus a newline
SD.open(..., FILE_READ) reopens the file in read-only mode
myFile.read() inside while (available()) pulls each byte until the end of the file

Good to know: on the ESP32 Uno, FILE_WRITE wipes the file before it writes. To add to the end, use FILE_APPEND, as this sketch does.

Serial Monitor output:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD Card is ready Created by newbiely.com Learn ESP32 Uno and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2

※ NOTE THAT:

Every run appends the same pair of lines. After one reset, the file holds four lines:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD Card is ready Created by newbiely.com Learn ESP32 Uno and SD Card Created by newbiely.com Learn ESP32 Uno and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2

To view the text on a PC, put the card in the USB reader and open arduino.txt with any text editor.

Read a File Line by Line

This part gives you a numbered printout, which suits files where each line is one record.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 17 // D4 (GPIO17) is the chip select line of the SD card module 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); // stop here, the card is not usable } Serial.println(F("SD Card is ready")); // FILE_APPEND keeps the old text and adds new text at the end (FILE_WRITE on the ESP32 Uno clears the file) myFile = SD.open("/arduino.txt", FILE_APPEND); if (myFile) { myFile.println("Created by newbiely.com"); // first text line saved to the card myFile.println("Learn ESP32 Uno and SD Card"); // second text line saved to the card myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open the same file again, this time only to read it myFile = SD.open("/arduino.txt", FILE_READ); if (myFile) { int line_count = 0; while (myFile.available()) { char line[100]; // room for 100 characters per line, make it bigger for long lines int line_length = myFile.readBytesUntil('\n', line, 100); // grab bytes up to the next newline line_count++; Serial.print(F("Line ")); Serial.print(line_count); Serial.print(F(": ")); Serial.write(line, line_length); // send the line text to the Serial Monitor // readBytesUntil() drops the '\n', so add it back below Serial.write('\n'); // end the printed line } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
Code Part What It Does
char line[100] buffer for one line, up to 100 characters
readBytesUntil('\n', line, 100) copies bytes until a newline and returns the count
line_count++ keeps the line number for the printout
Serial.write(line, line_length) prints exactly the bytes that were read

Each line of the file shows up with its number in front. Raise both 100 values if your lines are longer.

Output on a fresh card:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD Card is ready Line 1: Created by newbiely.com Line 2: Learn ESP32 Uno and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2

※ NOTE THAT:

This sketch appends before it reads. If arduino.txt still holds text from the earlier sketch, you get Line 3, Line 4 and so on.

Replace Old File Content with New Text

Use this pattern when you want only the newest data in a file, for example the last saved setting.

A file opened for appending grows forever. To start clean, the sketch deletes the old file and then makes a new one under the same name.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-micro-sd-card */ #include <SD.h> #define PIN_SPI_CS 17 // D4 (GPIO17) is the chip select line of the SD card module 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); // stop here, the card is not usable } Serial.println(F("SD Card is ready")); SD.remove("/arduino.txt"); // throw away the old file, if there is one // write mode makes a fresh, empty file with the same name myFile = SD.open("/arduino.txt", FILE_WRITE); if (myFile) { myFile.println("Created by newbiely.com"); // first text line saved to the card myFile.println("Learn ESP32 Uno and SD Card"); // second text line saved to the card myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } // open the same file again, this time only to read it myFile = SD.open("/arduino.txt", FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // take one byte from the card Serial.print(ch); // show that byte on the Serial Monitor } myFile.close(); } else { Serial.print(F("Error: Unable to open file arduino.txt")); } } void loop() { }
Code Part What It Does
SD.remove("/arduino.txt") deletes the old file, if one exists
SD.open(..., FILE_WRITE) creates an empty file with the same name
println() pair writes the two new lines
read loop prints the new content back

The file holds exactly two lines after each run. FILE_WRITE on the ESP32 Uno would clear the file anyway, but the remove call makes the intent clear in the code.

Serial Monitor output:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
SD Card is ready Created by newbiely.com Learn ESP32 Uno and SD Card
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Press EN on the ESP32 Uno.
  2. Watch the output. It stays at two lines, since the file is rebuilt on every boot.

Want proof on the PC? Open arduino.txt from the card reader. It has two lines only.

Video Tutorial

See the ESP32 Uno build in action in the video below.

ESP32 Uno vs UNO R4 for This Project

This table lists what changes when you move the SD card sketches from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
CS pin in code 4 17 (D4)
SPI pins D11, D12, D13 D11 (GPIO23), D12 (GPIO19), D13 (GPIO18)
File path "arduino.txt" works needs "/arduino.txt"
Append mode FILE_WRITE appends FILE_APPEND appends, FILE_WRITE clears
Board in Arduino IDE Arduino UNO R4 WiFi or Minima ESP32 Dev Module

The wiring stays the same, header for header. Only the CS number, the leading slash and the open mode change in code. Use a module with a buffer chip so 5V never reaches an ESP32 Uno pin.

Go Wireless: Read Your Log from a Phone

Here is one way to check stored data without pulling the card.

A data logger is more useful when you can peek at it from your desk. The ESP32 Uno has WiFi and Bluetooth, so it can read lines from the card and push them to a web page or a phone app. For example, send each line of arduino.txt to a web monitor, or show the last ten records in a table.

Language References

Use these pages to look up each function in the sketches.

Troubleshooting

Problem Possible Cause Solution
"SD Card is either missing or has failed!" at boot Card not seated, a loose SPI wire, or CS set to the wrong number Push the card in until it clicks, recheck the four SPI wires, and keep PIN_SPI_CS at 17
Mount fails with a large new card Card uses exFAT or NTFS Reformat it to FAT32 on your PC
exists() always says false The path has no leading slash Write "/arduino.txt" in every SD call
Earlier text is gone after a reset The file was opened with FILE_WRITE Switch to FILE_APPEND for logging
Readings work for a while, then stop Long jumper wires or a weak GND link Shorten the wires and add a second GND wire
Module gets warm or the board resets A bare socket is fed 5V Move VCC of a bufferless socket to 3.3V
No COM port shows up CH340 driver missing or a charge-only cable Install the CH340 driver and use a data USB cable
"Failed to connect" or "Waiting for download" The board did not enter upload mode Hold the BOOT button while the upload starts
Serial Monitor is blank or shows junk Baud rate mismatch or the monitor opened late Pick 9600 baud and press EN

FAQ

Do I need to install a library for the SD card on the ESP32 Uno?

No. The SD library is part of the esp32 by Espressif Systems core. Once the core is installed, just add #include at the top of the sketch.

Can I change the CS pin to another header?

Yes. Any free output pin works for CS. Change the PIN_SPI_CS value to the new GPIO number. Avoid A2 to A5 (input only) and D8 or D10 (boot strapping pins).

How big can the Micro SD Card be?

The library reads FAT16 and FAT32. Cards up to 32 GB usually come as FAT32. A 64 GB or larger card works after you reformat it to FAT32 with a suitable tool.

Why does the ESP32 Uno code use "/arduino.txt" and not "arduino.txt"?

The esp32 core uses a virtual file system that expects paths from the root. A name without the slash points to nothing, so the open call returns an empty file object.

What is the difference between FILE_WRITE and FILE_APPEND here?

On the ESP32 Uno, FILE_WRITE opens a file empty, like a new page. FILE_APPEND keeps the content and moves to the end. The UNO R4 code uses FILE_WRITE for both jobs, so this is the main code change.

Is the 5V VCC wire safe for the 3.3V ESP32 Uno pins?

It is safe with a standard module that has a regulator and a buffer chip, because its MISO output is 3.3V. A bare card socket must be powered from 3.3V, since its pins connect straight to the card.

Can I use an Uno shield with an SD slot on the ESP32 Uno?

Often yes, if the shield takes SPI from D11 to D13 and CS from D4. Shields that use only the ICSP header for SPI do not connect to the ESP32 Uno SPI pins.

Can I log sensor data to the card every few seconds?

Yes. Open the file with FILE_APPEND, print one line with the value and a time stamp, then close it. Closing after each write keeps the data safe if power drops.

Why does the board not start when I connect another module to D8 or D10?

D8 (GPIO12) and D10 (GPIO5) are boot strapping pins. A module that pulls them the wrong way at power-on stops the boot. The SD wiring in this guide does not use them.

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

The ESP32 Uno has extra holes near the power header. GPIO32 and GPIO33 offer I/O, PWM and analog on ADC1, so they still read analog values while WiFi is on. GPIO15 offers I/O, PWM and analog on ADC2. Add pin headers with a soldering iron, then use the GPIO number in your sketch. For a logger, GPIO32/33 suit an analog sensor, or a second CS line for another SPI device. Two warnings: GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC spot) are boot strapping pins, and a wrong connection can stop the board from booting or uploading, so use GPIO32/33 first. Never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They are wired to the flash chip, and using them crashes the board.

Can I see the file content on a web page instead of the Serial Monitor?

Yes. The ESP32 Uno has WiFi, so it can read the file and send each line to a browser. The DIYables Web Apps library makes this easy with a ready Web Monitor page.

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!