Simple SD Card/Read Write– Arduino Workshop

Simple SD Card Read Write

In this project, we will connect up an SD Card to an Arduino and by using the SD.h library to access the card, we will create a new file on the card, write some text to that file, print out the contents of that file, then delete the file.

This will teach you the basic concepts of accessing an SD card and reading and writing files to it. You will need an SD Card and some way of connecting it to an Arduino. The easiest way is to get an SD/MMC Card Breakout Board from various electronics hobbyist suppliers.

Required Component  SD Card Arduino:


2. Resistors

3. SD Card & Breakout

4. connecting wire

5. Breadboard

Never connect the Arduino’s output pins directly to the SD Card without
first dropping the voltage from them from 5V down to 3.3V or you will damage the SD Card

This book will help you to gain more knowledge about Arduino

Beginning Arduino

Circuit diagram SD Card Arduino

Simple SD Card Read Write

Digital pin 12 on the Arduino goes straight into pin 7 (DO) on the SD Card. Digital pins 13, 11, and 10 go via the resistors to drop the logic levels to 3.3V. The remaining connections to the SD card supply 3.3V to power the SD card via pin 4 and ground pins 3 and 6. Refer to the datasheet for your particular SD Card breakout board in case the pin outs differ from the circuit board above

Code SD Card Arduino:

#include <SD.h>
File File1;
void setup()
while (!Serial) { } // wait for serial port to connect.
// Needed for Leonardo only
Serial.println("Initializing the SD Card...");
if (!SD.begin()) {
Serial.println("Initialization Failed!");
Serial.println("Initialization Complete.\n");
Serial.println("Looking for file 'testfile.txt'...\n");
if (SD.exists("testfile.txt")) {
Serial.println("testfile.txt already exists.\n");
else {
Serial.println("testfile.txt doesn't exist.");
Serial.println("Creating file testfile.txt...\n");
File1 ="testfile.txt", FILE_WRITE);
Serial.println("Writing text to file.....");
String dataString;
File1 ="testfile.txt", FILE_WRITE);
if (File1) {
for (int i=1; i<11; i++) {
dataString = "Test Line ";
dataString += i;
Serial.println("Text written to file.....\n");
Serial.println("Reading contents of textfile.txt...");
File1 ="testfile.txt");
if (File1) {
while (File1.available()) {
// if the file isn't open, pop up an error:
else {
Serial.println("error opening testfile.txt");
// delete the file:
Serial.println("\nDeleting testfile.txt...\n");
if (SD.exists("testfile.txt")){
Serial.println("testfile.txt still exists.");
else {
Serial.println("testfile.txt has been deleted.");
void loop()
Nothing to see here

Make sure that your SD card has been freshly formatted in the FAT or FAT32 format. On a Mac, ensure the partition scheme is set to “Master Boot Record.” Run the program and open the serial monitor. The program will now attempt to write a file to the SD card, write some text to that file, read back the contents of the file and then finally delete the file. This will all be displayed on the serial monitor window. If everything goes well, you will get a readout like this:

Initializing the SD Card…
Initialization Complete.
Looking for file ‘testfile.txt’…
testfile.txt doesn’t exist.
Creating file testfile.txt…
Writing text to file…..
Text written to file…..
Reading contents of textfile.txt…
Test Line 1
Test Line 2
Test Line 3
Test Line 4
Test Line 5
Test Line 6
Test Line 7
Test Line 8
Test Line 9
Test Line 10
Deleting testfile.txt…
testfile.txt has been deleted.
Make sure that the card is either SD or SDHC format and that it is formatted with “Master Boot
Record” or MBR partitions.

All Arduino tutorial available Click here


 161 total views,  1 views today