Unintended 5ms delay while executing simple loop()

Hello arduino.cc :slight_smile:

I’m Using the Genuino 101 With IDE Version 1.6.13.

While executing loop(), now and then the arduino has a delay of 5ms. Since im writing a project where it’s necessary to avoid this i’d like to ask you what causes that delay.

Here’s some testing code:

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

File acc;
void setup() {
  if (!SD.begin(4)) {
    return;
  }
  acc = SD.open("loopTest", FILE_WRITE);
}

void loop() {
  acc.println(millis());
  if (millis() > 5000) {
    acc.close();
  }
}

that is a part of the output file:

578
578
578
579
579
579
579
579
579
579
579
584
584
584
584
585
585
585
585
585
585
585
585
585

In the output we can see, that there is a gap between ms 579 and 584. The next gap is between 592
and 597.

Thanks in advance

I believe I have read here that the SD library will take some time to write a buffer to the SD card when the buffer fills. I can't tell from the snippet of data how many times per millisecond you are writing to the buffer but I can tell it is at least 9 times. If it is about 13 times (which you can tell from your data) that would be fast enough to fill a 512 byte buffer in the (roughly) 8 milliseconds between 584 and 592. You can expect to get a 5 millisecond delay every 8 milliseconds. You need to find (or write) an SD library that can do non-blocking writes if a five millisecond delay can't be tolerated.

Perhaps another solution is to send the bytes to another Arduino which does nothing but write to the SD card.

Thank you very much,

i've read something here [1] that proves that.

I'll update the thread when I will have fixed that issue for myself.

[1] Why is the SD library slow? - Troubleshooting - Arduino Forum