SD card writing time problem

Dear all,

I am using Arduino Uno with LC-Tech card reader and 64 GB SanDisk memory class 10 with 80 MB/Sec speed.

I used Timer1 interrupt to set a flag and save millis() in a variable every 50 msec in order to save some float variables with milliseconds timestamps. When I use my code without memory, and save data directly on PC, everything is ok and I have an interval of 50 ms for my data. But, when I want to save them on SD Card (using SD lib, or SdFat lib), with same code, I have some missing data about every 83000 ms. The whole interval is 50ms but at some points (83 s) I have data after 100, 200, 250, up to 400 ms. What could be the reason for this?

Thank you

What could be the reason for this?

Lots of possible reasons. Without seeing your code, we'd only be guessing.

What I'm guessing, though, is that periodically, the output buffer fills up, and needs to be written to the card, and that takes time, during which your interrupt doesn't fire on time.

PaulS:
Lots of possible reasons. Without seeing your code, we’d only be guessing.

What I’m guessing, though, is that periodically, the output buffer fills up, and needs to be written to the card, and that takes time, during which your interrupt doesn’t fire on time.

dear Paul,
here is a simple code. It supposed to write some values every 50ms. The counter will show the number of each loop(or number of lines). it should reach to 12000 after 10min, but when the loop is finished i havesomething around 11920.

#include <SD.h>
#include<SPI.h>
#include<TimerOne.h>
#include<Wire.h>

File New;
int chipSelect=4;

float a=0.3456;
float b=0.5642;
float c=0.3467;
float d=0.0005;
float e=0.4512;
float f=0.1203;
float g=0.3222;
float h=0.5987;
float i=0.3402;
float j=0.6321;
unsigned long lastTime=0;
int flag=0;
long int counter=1;
int A=0;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(10,OUTPUT);
SD.begin(chipSelect);
Timer1.initialize(50000);
Timer1.attachInterrupt(intrpt);

while(!SD.begin(chipSelect)) { // Checking the availability of the SD card
Serial.println(“SD Card Error!”);
}
New=SD.open(“BBB.txt”,FILE_WRITE);
}

void intrpt(){
flag=1;
A++;
}

void loop() {
// put your main code here, to run repeatedly:

if(A>=200 && A<=12200){
if(flag==1){
counter++;
Serial.print(counter);Serial.print(" “);
Serial.print(a);Serial.print(” “);
Serial.print(b);Serial.print(” “);
Serial.print(c);Serial.print(” “);
Serial.print(d);Serial.print(” “);
Serial.print(e);Serial.print(” “);
Serial.print(f);Serial.print(” “);
Serial.print(g);Serial.print(” “);
Serial.print(h);Serial.print(” “);
Serial.print(i);Serial.print(” “);
Serial.print(j);Serial.println(” “);
New.print(counter);New.print(” “);
New.print(a);New.print(” “);
New.print(b);New.print(” “);
New.print(c);New.print(” “);
New.print(d);New.print(” “);
New.print(e);New.print(” “);
New.print(f);New.print(” “);
New.print(g);New.print(” “);
New.print(h);New.print(” “);
New.print(i);New.print(” “);
New.print(j);New.println(” ");
New.flush();
flag=0;
}
}
}

You are converting one long and 10 floats to string, twice, and writing the strings, plus 11 others, to the serial port and to the file buffer, and then to the file, and you think you can do all that in 50 milliseconds. You need to time how long it really takes to do all that, using micros() before the first Serial.print() call and after the flush() call. I'm guessing that, even at 115200, it takes longer than 50 milliseconds to do everything you need to do between the timer interrupts.

That's because Timer1 has a maximum length of 8.3 seconds or 8388.608mS or 8388608uS. Once it reaches that point it stops. I had this issue in one of my projects and we created a loop so that the timer would restart every 4 seconds.

PaulS:
You are converting one long and 10 floats to string, twice, and writing the strings, plus 11 others, to the serial port and to the file buffer, and then to the file, and you think you can do all that in 50 milliseconds. You need to time how long it really takes to do all that, using micros() before the first Serial.print() call and after the flush() call. I'm guessing that, even at 115200, it takes longer than 50 milliseconds to do everything you need to do between the timer interrupts.

the whole writing time on serial port and sd card takes something around 23 ms and there is no problem with that. 50ms is quite good. I have only every 83 sec a short delay and it happens only with sd card. without saving data on sd card, I have no problem in serial port even for one day.

Buleste:
That's because Timer1 has a maximum length of 8.3 seconds or 8388.608mS or 8388608uS. Once it reaches that point it stops. I had this issue in one of my projects and we created a loop so that the timer would restart every 4 seconds.

As you say it has max 8.3 sec, but this happens for me every 83 sec, and only when I am using sd card to save data on it. Without sd card, I don't have this problem on serial port.

In addition, I think this 8.3 sec is the max limit to using timer1 for each period and not its time.