Go Down

Topic: Cheap, quick, working SD for Arduino. (Read 1 time) previous topic - next topic

GoForSmoke

I started out by getting 3 of these SD Modules with header pins including SPI.
http://dx.com/p/sd-card-module-slot-socket-reader-for-arduino-arm-mcu-133709

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.
Code: [Select]

// 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;

  }
}


I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Osgeld

yea that IC is a level shifter =)

whats the number on the chip? I bet its a 4050, or a 245
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

GoForSmoke

#2
Nov 29, 2012, 07:02 am Last Edit: Nov 29, 2012, 07:18 am by GoForSmoke Reason: 1
What chip? I have 3 for close inspection... that's the LM1117.

I see a regulator where power goes in, 4 10 uF caps, 4 10k resistors and the SD holder.
There's a board under those, the only chip is in the UNO.

BTW, I have read the how and why smaller SD cards write quicker. 128M is better than 512M, etc.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Osgeld

on the link you posted there is a 6 pin SOIC on the SD breakout board
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

GoForSmoke

#4
Nov 29, 2012, 07:20 am Last Edit: Nov 29, 2012, 07:26 am by GoForSmoke Reason: 1
I think you mistake the regulator, an LM1117 which has 3 pins on one side.

BTW, I have asked the company for prices on 1 - 20 of several other items to see how this shapes out. Some of those are strictly 3.3V and they're all interesting.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up