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

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
13 hours ago
Raghuvir G posted discussions
19 hours ago
Jagajeet K posted a discussion

Digital IO – LED Blink – Arduino for Beginners

I have a doubt in the below link  …in that I used 220 ohm resistor for LED …Is Resistor have any…See More
19 hours ago
Profile IconRalph Wade, Jagajeet K, Raghuvir G and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
GarageLab posted blog posts
Tuesday
Enrico M. posted a status
"Building the Apollo console version I"
Monday
Lena Cook is now a member of GarageLab (arduino, electronics, robotics, hacking)
Monday
Paul Denton updated their profile
Nov 21
GarageLab posted a blog post
Nov 21
Profile IconRichard Coplan and Juan V. Sanchez-Andres joined GarageLab (arduino, electronics, robotics, hacking)
Nov 21
Kshitij Dadhekar commented on GarageLab's blog post Tutorial: How to use the Cellular Shield (SM5100B) with Arduino
"Hello,       Can you tell me what is the resistance of the speaker to be…"
Nov 19
GarageLab posted a blog post
Nov 19
Profile IconKshitij Dadhekar, Marvellous Mark and Enrico M. joined GarageLab (arduino, electronics, robotics, hacking)
Nov 19
david chan commented on GarageLab's blog post Tutorial: Humidity and Temperature Sensor with Arduino
"How do you see the output "
Nov 18
Enrico M. posted a discussion

Power supply circuit

Hi guys,I'm building some sort of wood case to arduino mega with LCD, keypad, LED's ...I also want…See More
Nov 18
GarageLab posted blog posts
Nov 18
Profile Icondavid chan, uday harihar, CVassiliou and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Nov 18
Jeff Bowen updated their profile
Nov 17
Michael Shaw replied to Jean-Guillaume LALANNE's discussion Smallest Arduino with BLE stack and push button integrated
"Check out Tiny circuits for open source: https://tiny-circuits.com/"
Nov 16
rozzers commented on rozzers's video
Nov 14

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service