I have a data logging project where I am logging data from an MPU6050. It logs the data every 5 milliseconds. It is quite good and I have even implemented a real time clock. The problem is that it takes twoce as long every 15th reading. Like the following:
I guess my question is: Any advice on how to make the arduino not drop these values. I have tried SDfat and sd and there is no difference. I guess the latency comes form the string conversion. Any ideas?
Write latency is inherent to SD cards. The SD spec allows up to 200 milliseconds latency for a write. Occasional write latencies of 100 milliseconds are not uncommon.
You must design your logger to allow for latency. I have posted several example fast loggers.
The cleanest way is to use a RTOS with two threads and read sensors in a high priority thread and buffer the data to be written to the SD in a lower priority thread.
Another way is to read the sensor in an ISR and buffer the data for loop. AnalogIsrLogger20120810.zip posted in the above location is a very fast logger that reads the Arduino ADC in an ISR and write the the data in loop().
Several other demo sketches are in fastLoggerBeta20110802.zip. This file is now a bit old.
Find 'Improving Write Performance' and see the diagram showing multiple sector write vs single sector write. Each time a block of NAND flash is to be written to, it first gets erased and then you write 1 or many sectors before the next erase. You get 2:1, at least your program is being efficient.
The size of the card can make a difference. Small cards, like 128M have smaller blocks to erase.
Chan's website is very good but most of the stuff about performance of SD cards is out of date.
Wear leveling in modern TLC flash can dominate latency. Modern cards move data that has not been changed for a long time to a new physical location. Flash controllers have various amounts of RAM buffering and use different algorithms so generalizations like small cards are faster just isn't true.
Write latency also depends on the access pattern. 328 Arduinos have limited memory so there are not many caching options.
Small cards are often formatted with small clusters which increases the overhead since the FAT must be accessed often.
For embedded applications cards with SLC flash tends to be best. Modern consumer cards use MLC or TLC flash. Industrial SD cards mostly use SLC flash. I have had good luck with this cheap Industrial card http://www.newegg.com/Product/Product.aspx?Item=9SIA12K0CT6829.
No card has uniform low write latency. All cards have occasional longer latencies. You must design fast data loggers with this in mind to avoid data loss.