i bought cheap ethernet shield use sd card slot data storage. have read (20 times) can find , spent many happy hours trying work. in example programs "initializing sd card...initialization failed!".
i have tried different sd libraries (arduino , adafruit (https://learn.adafruit.com/adafruit-data-logger-shield/for-the-mega-and-leonardo). have shield plugged on top of mega. there simple (and complete) instructions anywhere sd working?
my code:
i tried jumping pins 11,12,13 on ethernet shield pins 51,50,52 (i tried 50,51,52) respectably on mega board. nothing seems work. perhaps have bad shield? (un-wise-ass) help.
i have tried different sd libraries (arduino , adafruit (https://learn.adafruit.com/adafruit-data-logger-shield/for-the-mega-and-leonardo). have shield plugged on top of mega. there simple (and complete) instructions anywhere sd working?
my code:
code: [select]
/*
sd card basic file example
this example shows how create , destroy sd card file
the circuit:
* sd card attached spi bus follows:
** uno: mosi - pin 11, miso - pin 12, clk - pin 13, cs - pin 4 (cs pin can changed)
, pin #10 (ss) must output
** mega: mosi - pin 51, miso - pin 50, clk - pin 52, cs - pin 4 (cs pin can changed)
, pin #52 (ss) must output
** leonardo: connect hardware spi via icsp header
created nov 2010 david a. mellis
modified 9 apr 2012 tom igoe
modified 13 june 2012 limor fried
this example code in public domain.
*/
#include <spi.h>
#include <sd.h>
file 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("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 uno boards, 53 on mega) must left output
// or sd library functions not work.
// pinmode(ss, output);
pinmode(53, output);
// if (!sd.begin(chipselect)) {
if (!sd.begin(10,11,12,13)) {
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) {
// begin @ start of directory
dir.rewinddirectory();
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'); // we'll have nice indentation
}
// print 8.3 name
serial.print(entry.name());
// recurse directories, otherwise print file size
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();
}
}
i tried jumping pins 11,12,13 on ethernet shield pins 51,50,52 (i tried 50,51,52) respectably on mega board. nothing seems work. perhaps have bad shield? (un-wise-ass) help.
got working. for others having same problem:
don't use jumpers between pins on boards.
use arduino.cc version of sd.h
code works me:
don't use jumpers between pins on boards.
use arduino.cc version of sd.h
code works me:
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 16 mar 2011
by tom igoe
*/
// include sd library:
#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()
{
serial.begin(9600);
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(53, output); // change 53 on mega
pinmode(10,output); // pin 10 must left output can set high
digitalwrite(10,high); // pin 10 needs put high explicitly deselect wifi hdg104 chip[code]
digitalwrite(4,low);
// 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) {
}
[/code]
Arduino Forum > Using Arduino > Storage > SD Card on Ethernet Shield on MEGA not working
arduino
Comments
Post a Comment