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: 67830

Comment

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

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Roosevelt Sebial left a comment for Roosevelt Sebial
"I have left a message to your given e-mail address Mr. Frank. Thanks!"
yesterday
Eric Delgado updated their profile
yesterday
Jose Gustavo Abreu Murta left a comment for Jose Gustavo Abreu Murta
"SPAM !!!!"
yesterday
Selig Plapp updated their profile
Thursday
Alexander Perez updated their profile
Thursday
Marco Mauro left a comment for Marco Mauro
"replyin in PM"
Thursday
AUGUTIN AZIAKOU updated their profile
Thursday
Dmitry Slepov posted a blog post

Control smart RGBW LEDs from your Tibbo Project System

Test application for the FPGA Tibbit (#57) in the smart LED controller configuration…See More
Wednesday
Profile IconAUGUTIN AZIAKOU, FRANK KWABENA and RossBurk joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
Profile IconOjasvi Gupta, Simran Singh, ebraam yousif doud and 6 more joined GarageLab (arduino, electronics, robotics, hacking)
Monday
Richard Reubenstone left a comment for David
"Hi, David. Please see the friends invite I sent you recently. I wanted to introduce you to…"
Sep 23
GarageLab posted a blog post
Sep 19
Faiyaz Ahamed is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 16
Sylvain Monette is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 15
vivid posted blog posts
Sep 14
Profile IconAutha Rachman, Nguyễn Anh Tú and mohammad nabe joined GarageLab (arduino, electronics, robotics, hacking)
Sep 14
vivid and Richard Reubenstone are now friends
Sep 9
vivid joined Marcelo Rodrigues's group
Thumbnail

Arduino

Group for the Arduino communitySee More
Sep 9
Anatolio Filho is now a member of GarageLab (arduino, electronics, robotics, hacking)
Sep 5
Richard Reubenstone added a discussion to the group Arduino
Thumbnail

RF Controlled Robot Using Arduino

I just joined GarageLab and the Arduino group. My son, Steven, is the Founder & CEO of…See More
Aug 30

© 2016   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service