How to Read and Write to SD Card Using Arduino
To read and write to an SD card using Arduino, use the
SD library which provides functions like SD.begin() to initialize the card, SD.open() to open files, and file methods like read() and write(). Connect the SD card module to Arduino SPI pins, then use these functions to create, write, and read files on the card.Syntax
The SD library is used to interact with the SD card. Key functions include:
SD.begin(chipSelectPin): Initializes the SD card on the specified chip select pin.SD.open(filename, mode): Opens a file with a given name and mode (FILE_WRITEorFILE_READ).file.print()orfile.println(): Writes text to the file.file.read(): Reads data from the file.file.close(): Closes the file to save changes.
Always check if SD.begin() and SD.open() succeed before proceeding.
arduino
if (!SD.begin(10)) { Serial.println("Initialization failed!"); return; } File dataFile = SD.open("example.txt", FILE_WRITE); if (dataFile) { dataFile.println("Hello, SD card!"); dataFile.close(); } else { Serial.println("Error opening file"); }
Example
This example writes a line to a file named test.txt on the SD card, then reads and prints its content to the Serial Monitor.
arduino
#include <SPI.h> #include <SD.h> const int chipSelect = 10; void setup() { Serial.begin(9600); while (!Serial) {} if (!SD.begin(chipSelect)) { Serial.println("SD card initialization failed!"); return; } Serial.println("SD card initialized."); File myFile = SD.open("test.txt", FILE_WRITE); if (myFile) { myFile.println("Hello from Arduino!"); myFile.close(); Serial.println("Data written to test.txt"); } else { Serial.println("Error opening test.txt for writing"); } myFile = SD.open("test.txt"); if (myFile) { Serial.println("Reading from test.txt:"); while (myFile.available()) { Serial.write(myFile.read()); } myFile.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 from Arduino!
Common Pitfalls
Common mistakes when working with SD cards on Arduino include:
- Not initializing the SD card with
SD.begin()before accessing files. - Using the wrong chip select pin; it depends on your Arduino model and wiring.
- Forgetting to close files with
file.close(), which can cause data loss. - Trying to open files without checking if the file opened successfully.
- Using incompatible SD cards (some cards may not work well with Arduino).
Always verify wiring and test with simple read/write code first.
arduino
/* Wrong way: Not checking SD.begin() and file open */ #include <SD.h> void setup() { Serial.begin(9600); SD.begin(10); // No check for success File f = SD.open("file.txt", FILE_WRITE); // No check f.println("Test"); f.close(); } /* Right way: Check initialization and file open */ #include <SD.h> void setup() { Serial.begin(9600); if (!SD.begin(10)) { Serial.println("SD init failed"); return; } File f = SD.open("file.txt", FILE_WRITE); if (f) { f.println("Test"); f.close(); } else { Serial.println("File open failed"); } }
Quick Reference
Here is a quick summary of key functions and their purpose:
| Function | Description |
|---|---|
| SD.begin(chipSelectPin) | Initialize SD card, returns true if successful |
| SD.open(filename, mode) | Open a file for reading or writing |
| file.print()/file.println() | Write text to the file |
| file.read() | Read data from the file |
| file.close() | Close the file to save changes |
Key Takeaways
Always initialize the SD card with SD.begin() and check its success before file operations.
Use SD.open() with proper mode and always check if the file opened successfully.
Close files after writing to ensure data is saved correctly.
Connect the SD card module to the correct SPI pins and chip select pin on your Arduino.
Test with simple read/write examples before adding complexity.