I started out by getting 3 of these SD Modules with header pins including SPI.
After questions about 5V operation with a schematic and some agonizing, I went ahead and ran it jumper wired directly to the Arduino. It ran fine. Test sketch v.1 looked for SD.begin(10) and printed that, and that was that. Didn't matter since it worked first time, every time.
My parts list:
Arduino rev 0 UNO
SD Module
8 female to male jumpers
I'm still running 0022, let me know of changes needed for other versions?
Here's the test/example sketch with parts and connections spelled out in the upper comments.
// Testing SD Module connected to Arduino UNO
// This UNO is running IDE version 0022
// Arduino SPI library and SD library are imported using the
// Sketch->Import Library pop-down menu. Both are standard.
// SD Lib reference: http://arduino.cc/en/Reference/SD
// I only needed 8 female-to-male jumper wires to connect module to UNO.
// No breadboard, no diodes or protective components needed.
// A cable header can plug right onto the module and split to wire it.
// This sketch looks for a file named text.txt and if it is
// present, tries to read and print the text. Then it tries to
// open a file named test.txt and write to that.
// The SD card used is formatted FAT, is FAT 16, FAT 32 may work.
// On mine I placed a text file named text.txt to test reading.
// This sketch does not explore limits or modes of speed.
// Connections are all by direct female to male jumper wires
// No other hardware than SD Module with card, UNO and jumpers
// SD Module pin --- UNO pin
// 5V --- 5V
// 3.3V --- 3.3V
// GND --- GND
// GND --- GND
// CS --- 10
// MOSI --- 11
// MISO --- 12
// SCK --- 13
#include <SD.h>
#include <SPI.h>
byte state = 0; // controls what operation is run
byte flag = 0; // general use True/False
byte B; // single byte buffer
char fname[ 12 ] = "text.txt";
File sdfile; // File object
void setup(void)
{
Serial.begin( 9600 );
// This code grew by Steps. The start was only First Step.
// First Step, if you haved wired correctly and have SD then success
byte f = SD.begin( 10 ); // test if card is recognized
Serial.println( f, DEC ); // show success as 1, fail as 0
if ( !f ) while( 1 ); // if fail then sketch ends
// Second Step for me was to read a file name pre-placed on the card
f = SD.exists( fname ); // test if file text.txt is present
Serial.println( f, DEC ); // show success as 1, fail as 0
// if ( !f ) while( 1 );
Serial.println( "\nTime to do the data things.\n" );
}
// Next Four Steps, 1 each state case
void loop(void)
{
switch( state )
{
case 0 : // open text.txt for read if possible
{
sdfile = SD.open( fname, FILE_READ ); // try to open text.txt
if ( !sdfile ) // if test.txt not found, notify and change state
{
Serial.print( "Unable to open for read: " );
Serial.println( fname );
// while( 1 );
state = 2;
}
else
{
state = 1; // text.txt found, change to state 1
}
}
break;
case 1 : // read text.txt and print contents
{
if ( sdfile.available())
{
B = sdfile.read();
Serial.print( B );
}
else // once data is read, close file and change state
{
sdfile.close();
// while( 1 );
state = 2;
}
}
break;
case 2 : // now try to open/create test.txt for append/write
{
strcpy( fname, "test.txt" );
sdfile = SD.open( fname, FILE_WRITE );
if ( !sdfile )
{
Serial.print( "Unable to open for write: " );
Serial.println( fname );
while( 1 ); // sketch stops if reach here
}
state = 3;
}
break;
case 3 : // test.txt opened for append or created for write
{
for ( byte i = 'a'; i <= 'z'; i++ ) // append lowercase alphas
{
sdfile.print( i ); // append == add to end of what is there
Serial.print( i ); // this file grows with each new run
}
Serial.println( "\nOver-write start of file: " );
sdfile.seek( 0 ); // test ability to set write pointer
// unlike some seek() functions, this one starts at 0 instead of 1
// write over a-j with 0-9
for ( byte i = '0'; i <= '9'; i++ )
{
sdfile.print( i );
Serial.print( i );
}
Serial.println( "\nand that's all she wrote, goodbye!" );
sdfile.close();
while( 1 ); // sketch stops here
}
break;
}
}