SD writing speed

Hi,
I’m trying to write sensor’s data to a Micro SD card:
Toshiba-Exceria
32GB
MicroSD HC
When I try to write data to it in every loop w/o a delay, the program gets stuck and crashes.
What am I doing wrong, and how can I solve it?
Do I need to add a delay?
Or is it possible to skip the failure and continue with the loop?

Loop Code:

void loop()
{
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

Read_Write();

dataFile.close();
}

void Read_Write()

{

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; // Variables read from MPU6050
int8_t AcXG,AcYG,AcZG;
// Read data:
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true); // request a total of 14 registers
AcX = Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
AcXG = (AcX/4714);
AcYG = ((AcY - 328)/3768);
AcYG = ((AcY +1170)/5266);

String dataString = “”; // string for assembling the data to log:

File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
if (Serial_plus_SD)
Serial.println(dataString);
}
// if the file does not open, pop up an error:
else
errorFW();

return;
}

void stampC()
// function that writes a mark of calibration
{
// Local variables:
String dataString = " CALIBRATION ";

// Open the file in append mode:
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// If the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
if (Serial_plus_SD)
Serial.println(dataString);
}
// if the file does not open, pop up an error:
else
errorFW();

return;
}

void blinkL(unsigned long T, int N)
// function that blinks the LED N times with a delay of T millis.
{
bool statusL = false;
for (int i=0; i<N; i++)
{
delay(T);
statusL = !statusL;
digitalWrite(LEDpin, statusL);
}
return;
}

void errorFW()
// function that informs about an error while writing in the file, and “holds on” the sketch
{
if (Serial_plus_SD)
Serial.println(“Error opening datalog.txt.”);

// In any case, blink LED quickly to indicate trouble:
blinkL(200UL, 10);
// LED off and “nothing else”:
digitalWrite(LEDpin, LOW);
while(1)
;

return;
}

RoyEdrey:
Hi,
I’m trying to write sensor’s data to a Micro SD card:
Toshiba-Exceria
32GB
MicroSD HC
When I try to write data to it in every loop w/o a delay, the program gets stuck and crashes.
What am I doing wrong, and how can I solve it?
Do I need to add a delay?
Or is it possible to skip the failure and continue with the loop?

Loop Code:

void loop()
{
   File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

Read_Write();

dataFile.close();
}

void Read_Write()

{

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; // Variables read from MPU6050
 int8_t AcXG,AcYG,AcZG;
 // Read data:
 Wire.beginTransmission(MPU);
 Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
 Wire.endTransmission(false);
 Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
 AcX = Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
 AcY = Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
 AcZ = Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
 Tmp = Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
 GyX = Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
 GyY = Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
 GyZ = Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
AcXG = (AcX/4714);
AcYG = ((AcY - 328)/3768);
AcYG = ((AcY +1170)/5266);

String dataString = “”; // string for assembling the data to log:

File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

if (dataFile)
 {
   dataFile.println(dataString);
   dataFile.close();
   if (Serial_plus_SD)
     Serial.println(dataString);
 }
 // if the file does not open, pop up an error:
 else
   errorFW();
 
 return;
}

void stampC()
// function that writes a mark of calibration
{
 // Local variables:
 String dataString = " CALIBRATION ";

// Open the file in append mode:
 File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// If the file is available, write to it:
 if (dataFile)
 {
   dataFile.println(dataString);
   dataFile.close();
   if (Serial_plus_SD)
     Serial.println(dataString);
 }
 // if the file does not open, pop up an error:
 else
   errorFW();
 
 return;
}

void blinkL(unsigned long T, int N)
// function that blinks the LED N times with a delay of T millis.
{
 bool statusL = false;
 for (int i=0; i<N; i++)
 {
   delay(T);
   statusL = !statusL;
   digitalWrite(LEDpin, statusL);
 }
 return;
}

void errorFW()
// function that informs about an error while writing in the file, and “holds on” the sketch
{
 if (Serial_plus_SD)
   Serial.println(“Error opening datalog.txt.”);

// In any case, blink LED quickly to indicate trouble:
 blinkL(200UL, 10);
 // LED off and “nothing else”:
 digitalWrite(LEDpin, LOW);
 while(1)
   ;

return;
}

You need to be explicit by what you mean “gets stuck and crashes”.

You are opening the same file twice, both time for write? What do you actually expect to happen?

The first time you open it in the main loop then you call Read_Write() which reopens the same file.

I would change your design.

Create one global FILE object.
Open it during setup.
Write your samples to it.
Every so many seconds or minutes call file.flush(); this will actually push the data to the sdCard.
On EVERY file() call/function do error checking. If the function fails handle the failure.
Create some mechanism to do a safe shutdown: maybe a switch that means close the file and wait for power off.

don’t use the STRING object. It has a reputation of begin very RAM hungry. Learn how to use cstrings.

Chuck.