Free Shipping over ₹1299

Lesson 4: Testing the SD Card

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

ComponentPurpose
ESP32 BoardRuns your code
MicroSD cardStorage media
SD card breakout / slotPhysical holder
WiresConnect SD to ESP32
USB cableProgramming & power

Wiring the MicroSD Module

Connect your module as follows:

MicroSD Module PinESP32 S3/S3 MiniESP32 ESP32-C3Function
VCC3V33V33V3Power (Most modules need 3V3)
GNDGNDGNDGNDGround
CSGPIO 42GPIO 5GPIO 10Chip Select
SCKGPIO 2GPIO 18GPIO 2Clock
MOSIGPIO 3GPIO 23GPIO 4Master Out Slave In
MISOGPIO 46GPIO 19GPIO 3Master 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

IssueSolution
Card mount failedCheck wiring & correct CS pin; ensure card formatted FAT32
Nothing printedMake sure Serial Monitor is 115200 baud
Write failsEnsure card not locked/no write protection
Read back strange dataFormat 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.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Need Help?