Slow writing to SD card

First time playing with SD cards. Seems to work fine, but is slower than I would have expected. Using an AFI BoArduino @ 16MHz, an AFI MicroSD card breakout board, and just the SD.h library that comes with Arduino 0022. Anyway, it takes ~380ms to write 32 bytes, just wondering how that compares with other experiences out there. Code excerpt:

  #include <SD.h>
  uint8_t v[32];
  File logFile;
  SD.begin(10);
  logFile = SD.open("log.txt", FILE_WRITE);
...

  Serial.println();
  Serial.print(millis(), DEC);
  Serial.println(" Start write");
  
  for ( i=0; i < (sizeof(v)/sizeof(v[0])); ++i )
  {
    logFile.write( v[i] );
  }

  Serial.println();
  Serial.print(millis(), DEC);
  Serial.println(" End write");

One of the factors that influences how long it takes to write to the SD card is HOW the file is opened. You didn't show that.

In the storage subsection is a discussion about SD.h versus sdfat.h. The first is a wrapper around the other., easy programming has its price ;) - http://arduino.cc/forum/index.php/topic,58209.0.html -

There are other interesting tips too - http://arduino.cc/forum/index.php/topic,59287.0.html -

And writing large chunks at once (e.g. 512 bytes iso 32) also speeds up/

PaulS: One of the factors that influences how long it takes to write to the SD card is HOW the file is opened. You didn't show that.

Sorry about that. Added it in above, but basically just as follows. I didn't post all the code because it's not all mine; and while that may not be an issue, allow me to check first.

The points about writing larger blocks make total sense, I should have thought of that. Although I'm not sure where I might put a 32KB block of data in an ATmega328 XD

Appreciate the feedback, all!

SD.begin(10);
logFile = SD.open("log.txt", FILE_WRITE);

When I get some time, I'll cook up a minimal sketch to play with and try the suggestions above. Then I can post the whole thing and that should be easier for everyone. I'll report back in the Storage section. Missed that one didn't I! :blush: Thanks again, all!

I should have thought of that. Although I’m not sure where I might put a 32KB block of data in an ATmega328

somewhere between 32 bytes and 32 Kbytes there is an optimum …:wink:

robtillaart: somewhere between 32 bytes and 32 Kbytes there is an optimum ....;)

LOL, indeed! I'll work on that! Even just using file.write(buf, len) might be interesting to try.

Guys, just want to say thanks again, you certainly got me barking up the right tree. The more I think about it, the more embarrassing it is, this is probably a lesson I only learned 35 years or so ago. Anyway it turns out that writing 32 bytes at once with file.write(buf, len) is a fair amount more efficient than writing a byte at a time (!) with file.write(data). Imagine such a thing!

I wrote a little sketch that basically only wrote to the SD card and found the following.

  • Writing 32 bytes one at a time takes 350-360ms.
  • Writing 32 bytes all at once takes an average of 14.2ms.
  • Writing 256 bytes all at once takes an average of 16.3ms.

I'll look into blocking it up more but for right now I'm a pretty happy guy, even if I do win the DOH! award for the day.

Hi Jack,

looking at your numbers slightly differently * Writing 1 byte all at once time takes 11.1 ms. * Writing 32 bytes all at once takes an average of 14.2ms. * Writing 256 bytes all at once takes an average of 16.3ms.

If I do a first order linear interpolation => time (in millis) = 13,89 + 0.01 * #bytes;

That means overhead is almost 14 ms per write independant of the number of bytes. However your 3 test values are all within one sector, (I assume) .

What is the size of the sectors of the SD card used?

robtillaart:
What is the size of the sectors of the SD card used?

Hi Rob,

I thought that 512-byte sectors were pretty much fixed (as opposed to allocation unit size which is variable). Not sure where things stand with Advanced Format (4K sectors) but as the SD card is formatted FAT16, I’m thinking that wouldn’t apply anyway. Internal organization no doubt differs, but if I understand correctly, the SD card would have to emulate the standard 512-byte sector.

Hi Jack,

have you seen this one - http://arduino.cc/forum/index.php/topic,59287.0.html

The way it is formatted affects writing speed.

Wow, impressive! Sped up the write speed by over 20 times. I might have to try that, thanks! Good example of what can be done by paying attention to details. There certainly is a lot going on there, emulating cylinders, heads, sectors per track and all, and then having to map that efficiently onto the flash memory architecture.