This is a simple tutorial on how to use a SD card with Arduino. On this tutorial, we used the Breakout Board for SD-MMC Cards from SparkFun.

First, get a SD Card, put in your computer and create a text file named "test.txt" in the card root.
Get your protoboard and connect the breakout board to your Arduino using the schematic above. The resistors will work as a voltage divider for adapt the voltage level from Arduino (5V) to the SD Card (3.3V).

Then, download the SD Fat library and extract to the libraries folder of your Arduino installation.

Open the Arduino IDE and go to File > Examples > SD Fat and open the ReadWriteSDFat example. In the source code, change the chipSelect value to 10 (highlighted in red). The source code should be:

// Ported to SdFat from the native Arduino SD library example by Bill Greiman

// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 10;
/*
SD card read/write

This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe
modified by Bill Greiman 11 Apr 2011
This example code is in the public domain.

*/
#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {
Serial.begin(9600);

// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

// open the file for write at end like the Native SD library
if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening test.txt for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");

// close the file:
myFile.close();
Serial.println("done.");

// re-open the file for reading:
if (!myFile.open("test.txt", O_READ)) {
sd.errorHalt("opening test.txt for read failed");
}
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
int data;
while ((data = myFile.read()) > 0) Serial.write(data);
// close the file:
myFile.close();
}

void loop() {
// nothing happens after setup
}

 

 

Now, upload the code to your Arduino board and after that open your Serial Monitor.

If everything went right, you will see a "writing to test.txt" and "testing 1,2,3" messages in the Serial Monitor window. So, get your SD card and check in your computer if there is something in the test.txt file.

 

 

References: 

http://code.google.com/p/sdfatlib/

http://arduino.cc/playground/  

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206874649

Views: 34377

Tags: arduino, sd, tutorial

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

GarageLab posted a blog post

HAVOC: NASA’s proposed cloud city over Venus

The surface of Venus is a notorious hell-hole. If an atmospheric pressure roughly 90 times greater…See More
30 minutes ago
roberto premoli's photo was featured

premobox25-per-web-001

premoboard+cubieboard+hdd2.5''features:-3LAN-5USB-2WIFI-500GB HDD…
yesterday
Veritus's photo was featured

Battery Pack

Created a closed system of power in a wearable back pack. Back pack had 5 power outlets each at 220…
yesterday
NELU NASTASOIU's photo was featured
yesterday
wesley wortman's photo was featured
yesterday
GarageLab posted blog posts
yesterday
sunny lai left a comment for sunny lai
"thanks for hearing from you, it is my pleasure"
Tuesday
Enahoro Olori updated their profile
Monday
Profile IconRobert Sitton, Mr Abraham Akim and Magnus Nordin joined GarageLab (arduino, electronics, robotics, hacking)
Monday
GarageLab posted blog posts
Dec 12
Profile Icongiacomo, dan rules and Jay Thornton joined GarageLab (arduino, electronics, robotics, hacking)
Dec 11
GarageLab posted a blog post
Dec 10
GarageLab replied to Lou Liberatore's discussion LiquidCrystal_I2C library in the group Arduino
" Hi Lou Lberatone, I think you Hi Lou Lberatone, I think you probably are using the libraries…"
Dec 10
GarageLab joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Dec 10
GarageLab posted blog posts
Dec 9
Profile Iconbenjamin black and sunny lai joined GarageLab (arduino, electronics, robotics, hacking)
Dec 9
Profile IconJjdelala and Sathish GP joined GarageLab (arduino, electronics, robotics, hacking)
Dec 8
Lou Liberatore added a discussion to the group Arduino
Thumbnail

LiquidCrystal_I2C library

I have been trying to get an LCD to work via I2C.  When I try to verify the sketch (from various…See More
Dec 7
Lou Liberatore joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Dec 7
Anuj Shah updated their profile
Dec 7

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service