SD card logging has time glitches

Hi guys,

I’m using CATALE microSD card module to log data of a couple of sensors (barometer which is conncted through SPI, and a neo8 GPS through serial) to the Arduino DUE board.

I just noticed that the loop-time (execution time of the loop) is glitchy as shown in the attached figure. Some times it takes much longer. other times it fluctuates between 1ms and 3ms.

After commenting out some codes, I realized the glitch is due to dataFile.print(…) calls. The SPI connection has no glitch. If I comment out the GPS codes, still I see these glitches.

Is there any mistake in my code that makes this problem? I use SDFat.h library and the file format is “filename.dat”

Thanks

  void loop()
{
      val_file = digitalRead(9);  //read pushbutton to close file
     if (val_file==0){

        dataFile.close();
       
        Serial.println("END");
      }
dataFile.println(dt);  

 t_now = micros();
 dt = t_now-t_prev;

 t_prev=t_now;

}

Loop_Time.png

The SD-card is slow at each page change. (every 1/2 kB)

So how can I log data on SD reliably? if SD is not reliable then what solutions are there

So how can I log data on SD reliably?

Take a look at your code. If you press the switch to close the file, you close the file. Then, you try to write to the closed file. That doesn't make sense.

The print() method doesn't actually print to the file. It prints to a buffer. When the buffer gets full, the contents are transferred to the file.

Writing to the buffer is fast. Committing the data to the file is not.

The switch is just to make sure that the data is saved in SD, and the experiemnt is finished. I don't want to put file.close and re open in in the loop, as it slows down the process.

steven2010:
The switch is just to make sure that the data is saved in SD, and the experiemnt is finished. I don't want to put file.close and re open in in the loop, as it slows down the process.

As an experiment, it is flawed.

if(switch is pressed)
close the file
else
write to the file