Format SD card for Arduino projects

Recently I had a project that needed to tally the free and used space on the SD card and display that on an LCD. I have been using the SDFAT library instead of the SD library. The SdFat library is the basis of the SD library, so it has more functionality than SD and works faster in many cases. In the SdFat library, there are functions that count free blocks on the SD card, which is how free space is figured out: free blocks * block size (512 bytes)

Here is the function:

unsigned int sd_free_space_MB(SdFat* sdfp)
{
uint32_t freeMB = sdfp->vol()->freeClusterCount();
freeMB *= sdfp->vol()->blocksPerCluster();
freeMB/=2048;
return freeMB;
}

The freeClusterCount counts number of clusters, and then multiply that with number of blocks per cluster and then divide by 2048 to factor out the 512 byte per block and 1,048,576 bytes per MB, i.e. blocks * 512 / 1,048,576 = blocks / 2,048.

This code ran so slow that it took 10 seconds on a 2GB SD card. I was appalled thinking that is how fast they go. I even wrote a message on the LCD “calculating free space” so the user won’t wonder what’s going on during the 10 second delay. But later I posted my code asking for help on the Arduino forum. fat16lib, the author of the library, helped me greatly. It turned out that my SD card was not formatted optimally. I should have formatted it with the formatter tool that fat16lib pointed out (many many posts ago on the forum):

https://www.sdcard.org/downloads/formatter_4/

After formatting, I only experienced a split second delay before Arduino counts all the blocks. I recommend you to format all your SD cards with this tool. Original Arduino posts are below:

http://forum.arduino.cc/index.php?topic=228128.msg1648437#msg1648437

http://forum.arduino.cc/index.php?topic=228201.msg1648655#msg1648655

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: