12

I'm creating a data-logger system which logs data in SD card at a high speed of about 20000-30000 bytes per second. But the SD library in arduino currently writes data at about 4500-5000 bytes per second which is too slow.

I've read this hack to improve the write speed, but I'm wondering if a more direct approach is possible, maybe by adjusting the SPISettings.

  • 1
    You might try the SdFat library - it's much more efficient than the default SD library. – Majenko Aug 20 '16 at 19:25
  • You might also want to make sure your SD card is of good quality, although 5000 bytes seems too low for any kind of SD card. – Len Aug 22 '16 at 9:10
11

OK. So, I tried the SDFat lib. This library is definitely better than the default SD library that comes with adruino. But that is not how I solved my problem of low data-rate.

I followed the instruction of the author of SDFat library from this post.

According to fat16lib, to increase the data rate we need to use flush() wisely. We would want to write() the data in each cycle, but we only need to flush() once every 100 cycles or so depending on how much data is being written in each cycle. Also, be sure to keep the flag in SD.open() as 'O_WRITE | O_CREAT' instead of 'FILE_WRITE'.

This sure increased the speed by a great factor. But I needed more!

Storing the data in binary (check out this blog) improved the performance even more.

My current speed is approximately 100-120 KBps (that's kilo bytes) with a class 4 SD card!

Finally, I want to thank you guys for your help.

4

Many factors would decide if you can reach this wanted speed. Only some of these.


1. Your Software

The SdFat Library is faster than the standard SD Library of the Arduino IDE. It also has an easy to use compatibility function with the standard SD Library. Try it out.

2. Your Hardware

You should use a high class SD Card. As you maybe know SD Cards are sorted into performance classes. Most people in the internet recommend a SanDisk SD Card.

  • I will try this in a few days and let you know the results. Thanks. – Ashish Ranjan Aug 23 '16 at 7:16
4

Avoid the handshake getting a buffer!

Use SD.write(buf,size);

Hi every one, I'm working in a project with the same issue. I was following the same steps of yours and got exactly the same numbers. I've just fixed it out. The problem is the handshake when you call the SD.write().

Instead:

//for each loop, it is going to make a handshake
while(<yourCondition>){
    SD.write(<yourValue>);
}

Do:

char buf[length];
while(yourCondition){
    buf[index] = yourValue;
}
SD.write(buf,index);//only one handshake

In my project the first one I got 4100 bytes, and the second one using a buffer with 128 (buf[128]) I got 145408 bytes in my project. Good enough.

  • 1
    I don't know what you mean by handshake, because AFAIK SD uses a (512 kB) buffer internally anyway. But I also observed that writing individual bytes with File.write is extremely slow and the compiler doesn't seem to be able to optimize it (after all it's just copying one byte to the internal bufffer and checking if buffer is full and SPI transmission should start). Using my own buffer outside and File.write(buffer,size) brought extreme performance improvement also in my application. – oliver Jun 7 '18 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.