SD.write taking 3 seconds

I have a very large sketch that integrates just about every aspect of an Arduino (mega2560). Everything is working great except I have come across an interesting thing.

My sketch involves continuous communications over UDP to another device. If communications is broken for more than 300mS the other device drops off.

My sketch uses the SD card for lots of data storage from another network device. everything works ok except for....

When I open a file to write data the first 'write' takes 3 seconds!

PREAMBLE as per usual.... then tempfile =, FILE_WRITE);; Serial3.println(millis()); // test only to display timer start tempfile.write(byte (0)); // write anything.... Serial3.println(millis()); // test only to display timer end

Here I have put a display in there to show how long it takes to write a single byte - it ends up as ~3000 mS. After that all other bytes are written almost instantaneously!

Of course after this my other code times out and has to be reconnected.

Any one else seen the above behavior?


No, I have not seen that. Can you make a small test sketch and try a few things, like : formatting the SD card, an other SD card, without the seek, and so on.

Does the file get created by the open?

The first write may need to allocate a cluster. This can cause a scan of the FAT, File Allocation Table, and can take time to find the first free cluster if your card has lots of data.

If you formatted the card with a small cluster size it can take a very long time. Be sure to use SD Formatter, not an OS utility.