Reading and Writing Files from an SD Card with an Arduino
June 23, 2015 by Tim YoungbloodIn some Arduino applications, it is advantageous to be able to store and retrieve information locally. You can do this with a Secure Digital, or SD, card. An SD card is a non-volatile memory card used extensively in portable devices, such as mobile phones, digital cameras, GPS navigation devices, handheld consoles, and tablet computers. Another type of SD Card is the Micro SD card. Measuring only 15 mm x 11 mm x 1 mm, it is the smallest memory card available. It is about one-quarter the size of a normal-sized SD card, or about the size of a fingernail.
You can use an SD card with your Arduino system to store and retrieve information
In some Arduino applications, it is advantageous to be able to store and retrieve information locally. You can do this with a Secure Digital, or SD, card. An SD card is a non-volatile memory card used extensively in portable devices, such as mobile phones, digital cameras, GPS navigation devices, handheld consoles, and tablet computers. Another type of SD Card is the Micro SD card. Measuring only 15 mm x 11 mm x 1 mm, it is the smallest memory card available. It is about one-quarter the size of a normal-sized SD card, or about the size of a fingernail.

As shown in the figure above, a micro SD card has 8 pins. The table below describes the function of each pin.
Pin Name Description
1 NC not connected
2 CS Chip Select/Slave Select (SS)
3 DI Master Out/Slave In (MOSI)
4 VDD Supply voltage
5 CLK Clock (SCK)
6 VSS Supply voltage ground
7 DO Master In/Slave Out (MISO)
8 RSV Reserved
If you were to try interfacing this SD card yourself, you would have to ensure that you connected the pins of the SD card to the appropriate pins of the Arduino. Since we are using a commercially-available shield, this is not an issue. All we need to do is to declare the default CS (chip select) pin of the Arduino as OUTPUT. This is pin 53 on our Arduino MEGA. On the Ethernet shield, the CS pin is pin number 4. You need to specify this in the code for the SD card to work properly.
Experiment 1
In this experiment, we will learn how to read a file from the SD card.
Hardware Required
- 1 x micro SD card
- 1 x Ethernet shield module
- 1 x Arduino Mega2560

Code
To read from the SD card, we will use the SD.h library. This code assumes that the file "ourfile.txt" has already been written to the SD card.
#include <SD.h>
const int cs = 4;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing card...");
// make sure that the default chip select pin is declared OUTPUT
pinMode(53, OUTPUT);
// see if the card is present
if (!SD.begin(cs))
{
Serial.println("Card failed to initialize, or not present");
return;
}
Serial.println("card initialized.");
// open the file named ourfile.txt
File myfile = SD.open("ourfile.txt");
// if the file is available, read the file
if (myfile)
{
while (myfile.available())
{
Serial.write(myfile.read());
}
myfile.close();
}
// if the file cannot be opened give error report
else {
Serial.println("error opening the text file");
}
}
void loop()
{
}
Experiment 2
In this experiment, we will learn how to create a file, write it, and then read it from SD card.
Hardware Required
We will use the same hardware as the previous experiment
Code
To write a file to the SD card and to read that file, we will again use the SD.h library.
#include <SD.h>
File myfile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing card...");
// declare default CS pin as OUTPUT
pinMode(53, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization of the SD card failed!");
return;
}
Serial.println("initialization of the SDcard is done.");
myfile = SD.open("textFile.txt", FILE_WRITE);
if (myfile)
{
Serial.print("Writing to the text file...");
myfile.println("Congratulations! You have successfully wrote on the text file.");
myfile.close(); // close the file:
Serial.println("done closing.");
} else
{
// if the file didn't open, report an error:
Serial.println("error opening the text file!");
}
// re-open the text file for reading:
myfile = SD.open("textFile.txt");
if (myfile)
{
Serial.println("textFile.txt:");
// read all the text written on the file
while (myfile.available())
{
Serial.write(myfile.read());
}
// close the file:
myfile.close();
} else
{
// if the file didn't open, report an error:
Serial.println("error opening the text file!");
}
}
void loop()
{
}
Video
Give this project a try for yourself! Get the BOM.