Latency writing SD card

Hi !

I've been strugguling for a few weeks now with my project. It consists in writing in a SD card, data comming from an encoder. For 1 turn, the encoder gives 16 384 impulsions and its speed can go from 5 to 15 RPM (rotation per minute).

I used 3 bytes in order to write high state time and time between 2 impulsions. I store them into an array of 1024 * 3 bytes. To store data in the array , I use interruptions and ercaguy library to use a counter for the time.

My problem is that sometimes (~every 10 turns at 15 RPM, don't know why), I lose 2 or 3 arrays, which represents ~3000 impulsions. The problem comes from the writing on the SD card (see picture 1. Picture 2 and 3 show expected signals).

In order to write on SD card, I tried different ways (I used several (class 4 and 10) SD cards).

I used an tempory buffer and when it was full, I did a memcopy to another array in order to not overwrite data (because I knew that writing on SD was no that fast) and in my loop, I wrote data to the SD. This method didn't look to perform well, so I switch to another one.

I used two buffer. When one was full, I switch to the second one for the same reasons explained above. This method was better than the previous as I lost less data but I'm still loosing some...

I also tried to change my code as I read many forums : use file.flush() and file.sync() to force data to be written, when you call, use O_WRITE | O_CREAT instead of FILE_WRITE, divide SPI clock to increase the speed, reduce the size of my arrays (it was even worther), but nothing solved my problem ...

Anyone can help me please ? (1.49 MB)

It seems nobody has answered, and I really don't have a clue either. Is there any chance it's a hardware issue, such as using a standard size SD card module that doesn't level-shift? Well, I'm just guessing. Maybe one of the experts will be more helpful.

I use a Arduino Ethernet Shield so there is a level shifter on it. But as I looked at a lot of forums, the key points for a fast datalogger as I'm using are good quality SD card and the size of the buffer to send to the SD.
Hope that experts will answer too, thanks for the reply :slight_smile:

To log data at this rate requires sophisticated data acquisition in an interrupt routine that queues data to the background or use of a multi-threaded RTOS.

SD cards can have as long as 200 ms write latency.

Most modern cards are faster but still have an occasional write latency of many ms.

This happens mostly due to wear leveling. The card pauses to copy data from on area of flash to another. There is no external way to control the wear leveling algorithm.

The latency may exist due to old SD card.
It also depends on the wear and tear of SD card.
There are no work-arounds to reduce the Latency period if the above-mentioned two conditions are met.
Try reducing the write speed to as low as possible even more, to decrease the inconsistencies in writing data to the SD card.

Apps4Rent | clouddesktoponline