SdFat Inconsistent Write Times


I’m datalogging at a very high speed using a teensy 3.6, an onboard microsd card, and SdFat. The data logging has looked great, but upon further inspection, I realized the datapoints occasionally skip a significant amount of time.

I wrote a short piece of code to demonstrate this. I typically get that the maximum amount of time to execute file.write() once is several hundred microseconds compared to a minimum of 1 microsecond:

#include "SdFat.h"
#include <avr/io.h>
#include <avr/wdt.h>

SdFatSdio sd;

SdFatSdioEX sdEx;

File file;

void setup() {

  // put your setup code here, to run once:
  if (!sdEx.begin()) {
  sdEx.initErrorHalt("SdFatSdioEX begin() failed");
// make sdEx the current volume.
String fName = "testSpeed.bin";
char nameArr[fName.length()];

if (!, O_RDWR | O_CREAT)) {
sdEx.errorHalt("open failed: ");
file.truncate(0);//truncate file size to 0

int buffSize =6;
char abuff[buffSize];

unsigned long diff = 0;
unsigned long mindiff = 10000000;
unsigned long maxdiff = 0;

for(int i=0;i<10000;i++)
  unsigned long preOp = micros();
  unsigned long postOp = micros();
  diff = postOp-preOp;



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

The average write speed is fine, but missing several hundred microseconds of data typically causes me a lot of problems when making use of the logged data. Does anyone know the reason for these occasional slow write times?

Thanks in advance for all the help!

The arduino sd libraries are buffer based, and file.write() only places the data in a 512 byte buffer. The buffer is physically written to the card when it is full or if file.close() or file.flush() is called. It is the physical writes which give the longer times.

There are examples of using multiple buffers to deal with this, so that one buffer is being written to while the other is being transferred to the card.

Take a look at the SdFat library examples for high speed data logging examples and use of multiple buffers.