Micro SD example - library SD "datalogger"

I'm a newbie, I have just uploaded the example sketch "datalogger" from the SD library.

I' m using an original Arduino UNO Rev 3 + Ethernet shield with SD .

Sd card funtion well, but sometime the firmare stop, without any message.

After some tests to discover where the programm didn' function, I changed the original sketch in the string declaration , in this way :

Original :

String dataString = "";

modified

String dataString = "123456789";
String dataString = "";

Now, the program function without any stop.
Could the String class occupy a lot of RAM, causing some problems ?

Is it possible that nobody was aware of this problem in the example, or I'm doing some other mistakes ?

Thanks

/////////////////////////////////////////////////////////////////////////////////////////////////////

/*
SD card datalogger

This example shows how to log data from three analog sensors
to an SD card using the SD library.

The circuit:

  • analog sensors on analog ins 0, 1, and 2
  • SD card attached to SPI bus as follows:
    ** MOSI - pin 11
    ** MISO - pin 12
    ** CLK - pin 13
    ** CS - pin 4

created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
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);

// 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.");
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}

Read this topic. It covers how to initialize the SD so it won't fail.
http://arduino.cc/forum/index.php/topic,125646.0.html

Sd card funtion well, but sometime the firmare stop, without any message.

After some tests to discover where the programm didn' function, I changed the original sketch in the string declaration , in this way :

Original :

String dataString = "";

A likely source of your problem is right there. There are major problems with the String class. Quit using it.

@PaulS: That may be part of it, but this is the major problem:

 Serial.print("Initializing SD card...");
  // this enables the w5100 SPI, so it will step all over the SD comm
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  // but it probably won't be initialized correctly 
  // because the w5100 will be answering some of the time.
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

SurferTim,

Disabling all SPI devices except the SD is required to initialize the SD. However this may not be the problem here since the SD library make SS an output and sets it high. For 328 chips SS is pin 10 so the code to disable w5100 is in SD.h. For Mega this is often the problem with initialization of the SD on the Ethernet shield.

From D:/arduino-1.0.1/libraries/SD/utility/Sd2Card.cpp at line 228.

  // SS must be in output mode even it is not chip select
  pinMode(SS_PIN, OUTPUT);
  digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin

@fat16lib: That is good to know. I try to write code that will work on all hardware. It eliminates the possibility of the problem happening to the Mega user (like me).