0
0
AutocadHow-ToBeginner · 4 min read

How to Use SD Library in Arduino for SD Card Access

To use the SD library in Arduino, include #include <SD.h>, initialize the SD card with SD.begin(chipSelectPin), and then use functions like SD.open() to read or write files. The library lets you easily manage files on an SD card connected to your Arduino.
📐

Syntax

The SD library provides functions to initialize the card, open files, read, write, and close them.

  • #include <SD.h>: Includes the SD library.
  • SD.begin(chipSelectPin): Initializes the SD card on the specified chip select pin.
  • SD.open(filename, mode): Opens a file with read or write mode.
  • 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 chipSelect = 10; // Chip select pin for SD card module

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

  File myFile = SD.open("example.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("Hello, SD card!");
    myFile.close();
  } else {
    Serial.println("Error opening file");
  }
}

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

Example

This example shows how to initialize the SD card, create a file, write a line of text, then read and print the file content to the Serial Monitor.

arduino
#include <SD.h>

const int chipSelect = 10;

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

  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");

  File dataFile = SD.open("data.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println("Arduino SD library example.");
    dataFile.close();
    Serial.println("Data written to file.");
  } else {
    Serial.println("Error opening file for writing.");
  }

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

void loop() {
  // No repeated code needed
}
Output
Initializing SD card... Initialization done. Data written to file. Reading file content: Arduino SD library example.
⚠️

Common Pitfalls

Common mistakes when using the SD library include:

  • Not using the correct chip select pin for your SD card module.
  • Forgetting to initialize the SD card with SD.begin() before accessing files.
  • Not closing files after writing, which can cause data loss.
  • Using incompatible SD cards (some cards may not work well with Arduino).
  • Not checking if the file opened successfully before reading or writing.
arduino
// Wrong: Not checking SD.begin() result
#include <SD.h>
const int chipSelect = 10;
void setup() {
  Serial.begin(9600);
  SD.begin(chipSelect); // No check if initialization succeeded
  File file = SD.open("test.txt", FILE_WRITE);
  file.println("Hello");
  file.close();
}

// Right: Check initialization and file open
#include <SD.h>
const int chipSelect = 10;
void setup() {
  Serial.begin(9600);
  if (!SD.begin(chipSelect)) {
    Serial.println("SD init failed");
    return;
  }
  File file = SD.open("test.txt", FILE_WRITE);
  if (file) {
    file.println("Hello");
    file.close();
  } else {
    Serial.println("File open failed");
  }
}
📊

Quick Reference

Here is a quick summary of key SD library functions:

FunctionDescription
SD.begin(chipSelectPin)Initialize SD card, returns true if successful
SD.open(filename, mode)Open a file for reading or writing
file.read()Read one byte from file
file.write(data)Write data to file
file.println(text)Write text with newline
file.close()Close the file to save changes
SD.exists(filename)Check if a file exists
SD.remove(filename)Delete a file

Key Takeaways

Always call SD.begin() with the correct chip select pin before accessing files.
Check if the SD card and files open successfully to avoid errors.
Close files after writing to ensure data is saved properly.
Use SD.open() with FILE_READ or FILE_WRITE modes to read or write files.
Not all SD cards are compatible; use common Arduino-compatible cards.