Ethernet Shield doesn't initialize SD Card


hi everyone. explain problem. have original arduino uno rev3 , hanrun ethernet shield. want build domotic house system , need ethernet shield display web page stored sd, can't initialize it! i'm using sketch:

code: [select]
/*
  sd card test

 this example shows how use utility libraries on the'
 sd library based in order info sd card.
 very useful testing card when you're not sure whether working or not.

 the circuit:
  * sd card attached spi bus follows:
 ** mosi - pin 11 on arduino uno/duemilanove/diecimila
 ** miso - pin 12 on arduino uno/duemilanove/diecimila
 ** clk - pin 13 on arduino uno/duemilanove/diecimila
 ** cs - depends on sd card shield or module.
  pin 4 used here consistency other arduino examples


 created  28 mar 2011
 by limor fried
 modified 9 apr 2012
 by tom igoe
 */
// include sd library:
#include <spi.h>
#include <sd.h>

// set variables using sd utility library functions:
sd2card card;
sdvolume volume;
sdfile root;

// change match sd shield or module;
// arduino ethernet shield: pin 4
// adafruit sd shields , modules: pin 10
// sparkfun sd shield: pin 8
const int chipselect = 4;

void setup()
{
  // open serial communications , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }


  serial.print("\ninitializing sd card...");
  // on ethernet shield, cs pin 4. it's set output default.
  // note if it's not used cs pin, hardware ss pin
  // (10 on arduino boards, 53 on mega) must left output
  // or sd library functions not work.
  pinmode(10, output);     // change 53 on mega
  pinmode(10, high);

  // we'll use initialization code utility libraries
  // since we're testing if card working!
  if (!card.init(spi_half_speed, chipselect)) {
    serial.println("initialization failed. things check:");
    serial.println("* card inserted?");
    serial.println("* wiring correct?");
    serial.println("* did change chipselect pin match shield or module?");
    return;
  } else {
    serial.println("wiring correct , card present.");
  }

  // print type of card
  serial.print("\ncard type: ");
  switch (card.type()) {
    case sd_card_type_sd1:
      serial.println("sd1");
      break;
    case sd_card_type_sd2:
      serial.println("sd2");
      break;
    case sd_card_type_sdhc:
      serial.println("sdhc");
      break;
    default:
      serial.println("unknown");
  }

  // try open 'volume'/'partition' - should fat16 or fat32
  if (!volume.init(card)) {
    serial.println("could not find fat16/fat32 partition.\nmake sure you've formatted card");
    return;
  }


  // print type , size of first fat-type volume
  uint32_t volumesize;
  serial.print("\nvolume type fat");
  serial.println(volume.fattype(), dec);
  serial.println();

  volumesize = volume.blockspercluster();    // clusters collections of blocks
  volumesize *= volume.clustercount();       // we'll have lot of clusters
  volumesize *= 512;                            // sd card blocks 512 bytes
  serial.print("volume size (bytes): ");
  serial.println(volumesize);
  serial.print("volume size (kbytes): ");
  volumesize /= 1024;
  serial.println(volumesize);
  serial.print("volume size (mbytes): ");
  volumesize /= 1024;
  serial.println(volumesize);


  serial.println("\nfiles found on card (name, date , size in bytes): ");
  root.openroot(volume);

  // list files in card date , size
  root.ls(ls_r | ls_date | ls_size);
}


void loop(void) {

}


notice added line [pinmode(10, high);] prevent w5100 chip bug sd can't initialized. seen lot of discussion without success

this works on mega 2560 , ethernet shield/sd card. note use 115200 on baud rate. how on device? recommend power cycling arduino after uploading code.
code: [select]
#include <sd.h>

file root;

void setup()
{
  // disable w5100 spi
  pinmode(10,output);
  digitalwrite(10,high);
 
  // open serial communications , wait port open:
  serial.begin(115200);

  serial.print("initializing sd card...");
  // on ethernet shield, cs pin 4. it's set output default.
  // note if it's not used cs pin, hardware ss pin
  // (10 on arduino boards, 53 on mega) must left output
  // or sd library functions not work.

  if (!sd.begin(4)) {
    serial.println("initialization failed!");
    return;
  }
  serial.println("initialization done.");

  root = sd.open("/");
 
  printdirectory(root, 0);
 
  serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printdirectory(file dir, int numtabs) {
   while(true) {
     
     file entry =  dir.opennextfile();
     if (! entry) {
       // no more files
       //serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numtabs; i++) {
       serial.print('\t');
     }
     serial.print(entry.name());
     if (entry.isdirectory()) {
       serial.println("/");
       printdirectory(entry, numtabs+1);
     } else {
       // files have sizes, directories not
       serial.print("\t\t");
       serial.println(entry.size(), dec);
     }
     entry.close();
   }
}




Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Ethernet Shield doesn't initialize SD Card


arduino

Comments