Index
In the previous lessons, you verified that the microphone and speaker on your ESP32 project are working correctly. Now it’s time to test the SD card — the place where you’ll later store logs, audio files, debugging data, and other assets.
This lesson will guide you through connecting an SD card to your ESP32 and testing mounting, reading, and writing data.
What You’ll Need
| Component | Purpose |
|---|---|
| ESP32 Board | Runs your code |
| MicroSD card | Storage media |
| SD card breakout / slot | Physical holder |
| Wires | Connect SD to ESP32 |
| USB cable | Programming & power |
Wiring the MicroSD Module
Connect your module as follows:
| MicroSD Module Pin | ESP32 S3/S3 Mini | ESP32 | ESP32-C3 | Function |
|---|---|---|---|---|
| VCC | 3V3 | 3V3 | 3V3 | Power (Most modules need 3V3) |
| GND | GND | GND | GND | Ground |
| CS | GPIO 42 | GPIO 5 | GPIO 10 | Chip Select |
| SCK | GPIO 2 | GPIO 18 | GPIO 2 | Clock |
| MOSI | GPIO 3 | GPIO 23 | GPIO 4 | Master Out Slave In |
| MISO | GPIO 46 | GPIO 19 | GPIO 3 | Master In Slave Out |
The Test Code
This code uses the standard SD.h library, which is robust and works with almost all ESP32 setups.
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
// Pins
#define SD_CS 42
#define SD_MOSI 3
#define SD_MISO 46
#define SD_SCLK 2
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Simple SD Card Test");
Serial.println("======================");
// Initialize SPI
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
// Initialize SD card
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println(" FAILED!");
Serial.println("Check:");
Serial.println("1. SD card inserted?");
Serial.println("2. Correct pins?");
Serial.println("3. Formatted as FAT32?");
while(1);
}
Serial.println(" OK!");
// Get card info
uint8_t cardType = SD.cardType();
Serial.print("Card type: ");
switch(cardType) {
case CARD_NONE: Serial.println("None"); break;
case CARD_MMC: Serial.println("MMC"); break;
case CARD_SD: Serial.println("SDSC"); break;
case CARD_SDHC: Serial.println("SDHC"); break;
default: Serial.println("Unknown");
}
if (cardType != CARD_NONE) {
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("Card size: %llu MB\n", cardSize);
// Create test file
File file = SD.open("/test.txt", FILE_WRITE);
if (file) {
file.println("Hello from ESP32!");
file.close();
Serial.println("Test file created: /test.txt");
}
// List files
Serial.println("\nFiles on SD card:");
File root = SD.open("/");
File entry = root.openNextFile();
while (entry) {
if (entry.isDirectory()) {
Serial.print(" [DIR] ");
} else {
Serial.print(" [FILE] ");
}
Serial.print(entry.name());
if (!entry.isDirectory()) {
Serial.print(" (");
Serial.print(entry.size());
Serial.print(" bytes)");
}
Serial.println();
entry = root.openNextFile();
}
root.close();
}
Serial.println("\n✅ SD Card test complete!");
}
void loop() {
// Nothing to do
delay(1000);
}
Common Issues & Fixes
| Issue | Solution |
|---|---|
| Card mount failed | Check wiring & correct CS pin; ensure card formatted FAT32 |
| Nothing printed | Make sure Serial Monitor is 115200 baud |
| Write fails | Ensure card not locked/no write protection |
| Read back strange data | Format card again, try smaller capacity card |
Note : Some ESP32 boards use SDMMC internally; if external SPI wiring fails, check your board’s default SD pins and use SD_MMC instead (advanced) — some boards have different defaults.

