I'm a bit of an arduino noob so bear with me... I have an ethernet shield with SD card reader (with my Diecimila). Got it connected to the network fine, but I'm getting problems reading from the SD card. If I use this program:
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
Serial.flush();
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.flush();
delay(1000);
File f = SD.open("TEST.TXT");
if(f) {
while(f.available()) {
Serial.write(f.read());
}
} else { Serial.println("No open"); }
}
void loop(void) {
}
I get no output whatsoever. If I comment out the SD.open part:
The output from the serial monitor is then "SD T". So I'm convinced there is a buffer overflow somewhere, but it's a bit of a mystery to me. If I add "Serial.println("Open OK");" right after "if(f) {" then I get a bunch of random characters. After a couple of resets it then started spitting out "SD" multiple times.
Just to try and rule out it being a bad board/shield, the analog web server example works absolutely fine.
I know this sounds strange, but you may be running out of memory. Not program memory, but SRAM. The diecimila has only 1K SRAM. From my understanding, the SD functions use a 512 byte SRAM buffer and other SRAM memory too.
No, that sounds perfectly reasonable. I've managed to hit the limit before doing things over the Serial port with the LCD library and some other external hardware (I'm far to used to these fancy x86 devices with their comparatively near infinite RAM
So I've probably got two options then:
I guess the FAT parts of the library are going to be the most resource intensive - so would it be a better idea to access the SD 'raw' (would this work kind of like writing to an external EEPROM?). I found some code to do this (Arduino Playground - SDCARD) - it seems pretty involved but might work well as a bit of a hack.
A board with more memory - perhaps one of the mega ones? (FWIW I'm up to about 13k of 14k available bytes in the program memory so this might be a better long term choice)
I use a Mega2560. It has 8k of SRAM. I do not open multiple SD files simultaneously, but fat16lib says you can. It takes 512 bytes (or more) SRAM for each file opened.
I am scratching my head here. I simply can not get my SD card to be detected with this simple sketch! I have a Leonardo+Ethernet/SD shield and nothing else. I use a 5V external PWS and obviously keep my USB connected. I have tryed the latest SdFat.h library and communication works fine so I know its not the card or the PINs... If you have any pointers. I would have suspected the exemples would have worked by know but it looks like not.
Marc
SurferTim:
Try this:
#include <SD.h>
void setup() {
Serial.begin(9600);
// disable w5100 SPI while setting up the SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);