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

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 blog posts
yesterday
Gaston Lieutier is now a member of GarageLab (arduino, electronics, robotics, hacking)
Tuesday
GarageLab posted blog posts
Tuesday
Volkan Coskun posted a discussion

Problem with my motordriver and dc motor

GarageLab enthusiasts!I am trying to build a balancing robot with two wheels, using the polulu gear…See More
Monday
Profile IconHugo Saffir and Volkan Coskun joined GarageLab (arduino, electronics, robotics, hacking)
Monday
GarageLab posted blog posts
Jul 24
Profile IconJoseph Long and Pablo Costas joined GarageLab (arduino, electronics, robotics, hacking)
Jul 23
GarageLab posted blog posts
Jul 22
Profile Iconvictor ghenno, Michel Souza Medeiros, ANAND S and 6 more joined GarageLab (arduino, electronics, robotics, hacking)
Jul 21
GarageLab posted blog posts
Jul 20
Chris replied to Chris's discussion Newbie question ......suitable power supply
"I just had a reply on another forum does this make sense? "The device you purchased is not…"
Jul 16
Profile IconPedro A Vazquez, Dylan, Feliks Gadzinski and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Jul 16
Chris posted a discussion

Newbie question ......suitable power supply

Hi guysFirstly I have no knowledge whatsoever of Electronics and RF, my background back in the UK…See More
Jul 16
GarageLab posted a blog post

Micro:Bit — BBC gets a million kids into embedded dev

In the Early 1980s, the BBC launched a project to teach computer literacy to a generation of…See More
Jul 16
Profile IconDavid Bissonnette, Bob Blanco, Mark Pind and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Jul 15
GarageLab posted blog posts
Jul 15
Phan Minh Kha commented on Thulana Vimukthi's blog post GPS GUIDED AUTONOMOUS ROBOT
"bool condition = ((angleDegrees -10) < headingDegrees) && ((angleDegrees +10) >…"
Jul 14
Phan Minh Kha commented on Thulana Vimukthi's blog post GPS GUIDED AUTONOMOUS ROBOT
"dist_calc=sqrt((((flon1)-(x2lon))*((flon1)-(x2lon)))+(((x2lat-flat1)*(x2lat-flat1))));…"
Jul 14
GarageLab posted blog posts
Jul 13
GarageLab posted blog posts
Jul 8

© 2015   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service