Write problem with Arduino Uno + Sd shield Seed Studio

Hi everybody.
I need your help!

I wrote a simple project on Arduino UNO with SD shield Seed Studio V4.0 (and V4.1) that writes a log file using the SDFAT library.

I can open the file no problem and start writing but after 1 minute all the write operations fail (even after an arduino restart).

this is the source code:

/*
 * Simple data logger.
 */
#include <SPI.h>
#include <SdFat.h>

// SD chip select pin.  Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;

// Interval between data records in milliseconds.
// The interval must be greater than the maximum SD write latency plus the
// time to acquire and write data to the SD to avoid overrun errors.
// Run the bench example to check the quality of your SD card.
const uint32_t SAMPLE_INTERVAL_MS = 200;

// Log file base name.  Must be six characters or less.
#define FILE_BASE_NAME "Data"
//------------------------------------------------------------------------------
// File system object.
SdFat sd;

// Log file.
SdFile file;

// Time in micros for next data record.
uint32_t logTime;

//==============================================================================
// User functions.  Edit writeHeader() and logData() for your requirements.

const uint8_t ANALOG_COUNT = 4;
//------------------------------------------------------------------------------
// Write data header.
void writeHeader() {
  file.print(F("micros"));
  for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
    file.print(F(",adc"));
    file.print(i, DEC);
  }
  file.println();
}
//------------------------------------------------------------------------------
// Log a data record.
void logData() {
  uint16_t data[ANALOG_COUNT];

  // Read all channels to avoid SD write latency between readings.
  for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
    data[i] = analogRead(i);
  }
  // Write data to file.  Start with log time in micros.
  file.print(logTime);

  // Write ADC data to CSV record.
  for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
    file.write(',');
    file.print(data[i]);
  }
  file.println();
}
//==============================================================================
// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))
//------------------------------------------------------------------------------
void setup() {
  const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
  char fileName[13] = FILE_BASE_NAME "00.csv";

  Serial.begin(9600);
  while (!Serial) {} // wait for Leonardo
  delay(1000);

  Serial.println(F("Type any character to start"));
  while (!Serial.available()) {}

  // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }

  // Find an unused file name.
  if (BASE_NAME_SIZE > 6) {
    error("FILE_BASE_NAME too long");
  }
  while (sd.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } else {
      error("Can't create file name");
    }
  }
  if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
    error("file.open");
  }
  do {
    delay(10);
  } while (Serial.read() >= 0);

  Serial.print(F("Logging to: "));
  Serial.println(fileName);
  Serial.println(F("Type any character to stop"));

  // Write data header.
  writeHeader();

  // Start on a multiple of the sample interval.
  logTime = micros()/(1000UL*SAMPLE_INTERVAL_MS) + 1;
  logTime *= 1000UL*SAMPLE_INTERVAL_MS;
}
//------------------------------------------------------------------------------
void loop() {
  // Time for next record.
  logTime += 1000UL*SAMPLE_INTERVAL_MS;

  // Wait for log time.
  int32_t diff;
  do {
    diff = micros() - logTime;
  } while (diff < 0);

  // Check for data rate too high.
  if (diff > 10) {
    error("Missed data record");
  }

  logData();

  // Force data to SD and update the directory entry to avoid data loss.
  if (!file.sync() || file.getWriteError()) {
    error("write error");
  }

  if (Serial.available()) {
    // Close file and stop.
    file.close();
    Serial.println(F("Done"));
    while(1) {}
  }
}

Thanks,
Marco

Sorry!!!!!

I was wrong code!!

This the correct code:

#include <SPI.h>
#include <SD.h>
// #include <SdFat.h>

const int chipSelect = 10;

// SdFat SD;

void setup() {
  delay(2000);
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }



  Serial.print("Initializing SD card...");
  pinMode(4, OUTPUT);
  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:
	delay(10000);
    return;
  }
  Serial.println("card initialized.");
}

void loop() {

	
	unsigned long  _time;
	static int lap = 0;;
	char stringa[100];
	
  // 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 += ",";
    }
  }*/
  

	_time = millis();
	lap++;
	
	sprintf(stringa, "-> lap=%d    Time=%ld Test di circa 50 byte \r\n", lap, _time);
  // 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("datalog1.txt", FILE_WRITE);

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

but after 1 minute all the write operations fail

I can not confirm this. I ran the second posted code to 20,000 laps in time=436,611. As far as I can tell, all the data was written to the card. There may have been a write error somewhere in all the 1 mb of data, but I didn't see it.

I would focus on the sd card and your module. I used a Class4 Kingston 4gb micro sd card. and my module is a Catalex with an ic logic level shifter rather than the resistance network.

Thanks for your reply!
I had the same hunch: yesterday I used a bunch of different Transcend MicroSd Hc Class4 4GB with no avail.
Now I'm making some tests using SD Transcend 2GB 133x formatted in Fat16 and for the time being everything works just fine.

I'll keep you posted.

Thanks a lot
Marco