0
0
AutocadHow-ToBeginner · 4 min read

How to Interface SD Card Module with Arduino: Simple Guide

To interface an SD card module with an Arduino, connect the module's SPI pins (MOSI, MISO, SCK, CS) to the Arduino's SPI pins and use the SD library to initialize and read/write files on the card. The CS pin must be defined correctly in your code to communicate with the SD card.
📐

Syntax

To use the SD card module, include the SD.h library and initialize the card with SD.begin(CS_PIN). Use File objects to open, read, write, and close files.

  • SD.begin(CS_PIN): Starts communication with the SD card on the chip select pin.
  • SD.open(filename, mode): Opens a file for reading or writing.
  • file.read(), file.write(): Read from or write to the file.
  • file.close(): Closes the file to save changes.
arduino
#include <SD.h>

const int CS_PIN = 10; // Chip Select pin for SD card module

void setup() {
  Serial.begin(9600);
  if (!SD.begin(CS_PIN)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("SD card initialized.");
}

void loop() {
  // Your code here
}
Output
SD card initialized.
💻

Example

This example shows how to write a text file to the SD card and then read it back, printing the content to the Serial Monitor.

arduino
#include <SD.h>

const int CS_PIN = 10;

void setup() {
  Serial.begin(9600);
  while (!Serial) {}

  if (!SD.begin(CS_PIN)) {
    Serial.println("SD card initialization failed!");
    return;
  }
  Serial.println("SD card initialized.");

  File dataFile = SD.open("test.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println("Hello, Arduino SD card!");
    dataFile.close();
    Serial.println("Data written to test.txt");
  } else {
    Serial.println("Error opening test.txt");
  }

  dataFile = SD.open("test.txt");
  if (dataFile) {
    Serial.println("Reading from test.txt:");
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  } else {
    Serial.println("Error opening test.txt for reading");
  }
}

void loop() {
  // Nothing here
}
Output
SD card initialized. Data written to test.txt Reading from test.txt: Hello, Arduino SD card!
⚠️

Common Pitfalls

Common mistakes when interfacing an SD card module with Arduino include:

  • Incorrect wiring of SPI pins (MOSI, MISO, SCK, CS).
  • Using the wrong chip select (CS) pin in SD.begin().
  • Not powering the SD card module properly (usually 3.3V or 5V depending on module).
  • Forgetting to close files after writing, which can cause data loss.
  • Using incompatible SD cards (some modules support only FAT16/FAT32).

Example of a common mistake:

// Wrong CS pin
const int CS_PIN = 4; // If your module uses pin 10, this will fail

void setup() {
  Serial.begin(9600);
  if (!SD.begin(CS_PIN)) {
    Serial.println("Initialization failed!");
  }
}

Correct it by setting the CS pin to the actual connected pin:

const int CS_PIN = 10; // Correct pin

void setup() {
  Serial.begin(9600);
  if (!SD.begin(CS_PIN)) {
    Serial.println("Initialization failed!");
  }
}
📊

Quick Reference

Summary tips for interfacing SD card module with Arduino:

  • Connect SD module SPI pins to Arduino SPI pins: MOSI (11), MISO (12), SCK (13), CS (usually 10).
  • Use SD.begin(CS_PIN) to initialize.
  • Open files with SD.open() and always close them after use.
  • Use 3.3V or 5V power as per your module specs.
  • Use FAT16 or FAT32 formatted SD cards for compatibility.

Key Takeaways

Connect the SD card module SPI pins correctly to Arduino SPI pins and define the correct CS pin in code.
Initialize the SD card with SD.begin(CS_PIN) before reading or writing files.
Always close files after writing to ensure data is saved properly.
Use compatible SD cards formatted with FAT16 or FAT32 for best results.
Check power requirements of your SD card module to avoid damage or malfunction.