Free Shipping over ₹1199

ESP32 Micro SD Card with SD Adapter

Introduction

This project shows how to read files from an SD Card (with SD Adapter) using an ESP32 via SPI communication.
We connect the SD Adapter directly to the ESP32 using defined SPI pins and access a file named example.txt.
The file content is read and displayed on the Serial Monitor, demonstrating simple SD card data handling.
Perfect for data logging, text storage, or SD-based project configurations without needing a separate SD module.

Required Components

  • ESP32 Board
  • Micro SD Card
  • SD Card Adapter
  • Wires

Circuit Diagram

  • SD Card Adapter VCC  → ESP32 3.3V
  • SD Card Adapter GND → ESP32 GND
  • SD Card Adapter SD_MOSI → ESP32 GPIO 23
  • SD Card Adapter SD_MISO → ESP32 GPIO 19
  • SD Card Adapter SD_SCLK → ESP32 GPIO 18
  • SD Card Adapter SD_CS → ESP32 GPIO 5

Code / Programming

  • Install Required Library (via Arduino Library Manager).
  • Go to the “Libraries” tab on the left-hand side of the screen.
  • Click on the “Library Manager” button (book icon) at the top of the Libraries tab.
  • In the Library Manager window, type “SD” in the search bar.
  • Locate the “SD” library click on the “Install” button next to it.
  • Wait for the library to be installed, and you’re ready to use the SD library in your projects.
#include <SPI.h>
#include <SD.h>

#define SD_MOSI     23
#define SD_MISO     19
#define SD_SCLK     18
#define SD_CS       5

void setup() {
  Serial.begin(115200);
  while (!Serial);  // Wait for Serial Monitor

  Serial.println("Initializing SD card...");
  SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);

  if (!SD.begin(SD_CS)) {
    Serial.println("Card failed or not present.");
    return;
  }

  Serial.println("Card initialized.");

  File file = SD.open("/example.txt");
  if (file) {
    Serial.println("example.txt contents:");
    while (file.available()) {
      Serial.write(file.read());
    }
    file.close();
  } else {
    Serial.println("Error opening example.txt");
  }
}

void loop() {
  // Nothing here
}

Steps After Uploading the Code:

  1. Open Serial Monitor
    • In Arduino IDE, go to Tools > Serial Monitor
    • Set the baud rate to 115200
  2. Expected Output
    If everything is connected properly and the SD card is working, you’ll see:
Initializing SD card...
Card initialized.
example.txt contents:
[Contents of your example.txt file]

3. If There’s an Issue, you might see:

Initializing SD card...
Card failed or not present.

Troubleshooting

  • Make sure SD Adapter is in Full Size SD Card slot, not microSD without adapter.
  • Format the SD card as FAT32 using your computer.
  • Ensure file name is example.txt and it’s in the root directory of the SD card.
  • Double-check your wiring:
ESP32        SD Adapter
------       -----------
GPIO 23  ->  MOSI
GPIO 19  ->  MISO
GPIO 18  ->  SCK
GPIO 5   ->  CS
GND      ->  GND
3.3V     ->  VCC (use 5V only if adapter supports it)

    Leave a Reply

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

    Need Help?