I'm trying to use a microSD card with an Arduino (ATmega328). I'm using the SPI interface.

I've tried using the Arduino SD wrapper, but whenever I #include the SD.h header file the Arduino continuously resets itself. I think this is a RAM issue and it seems like this is the issue: http://code.google.com/p/sdfatlib/issues/detail?id=15

How would I go about doing this now? What library/method do I use?

  • \$\begingroup\$ Is FAT filesystem support important? Reading/writing to an SD card is fairly simple, but filesystems are hard \$\endgroup\$ Jul 25, 2011 at 15:54

2 Answers 2


Full RAM is almost certainly your problem. You want to keep an extra 200-400 bytes for the stack, depending on your program complexity. I tend to have a large stack, so I keep at least 400 open all the time. If it gets less than that free, it is time to optimize something.

Here is an example to show you how much you have left:

#define RAMSIZE 2048 //You can probably get this from another define somewhere

int availableMemory() {
    int size = RAMSIZE;
    byte *buf;
    while ((buf = (byte *)
        malloc(--size)) == NULL);
    return size;

void chkMem() {
    Serial.print("chkMem free= ");
    Serial.print(", memory used=");

Arduino release 0022 has been quite problematic for me. I instead use 0021 and grab fat16lib which is lighter and serves my data logging needs. If you can do without directories, use 8.3 filenames, and SD cards <= 2 GB, it's very helpful and works!

Finally, if you have a lot of string data in your program (like println debugging statements or other long strings, consider accessing those directly from the ATmega328's flash memory, which will also save RAM. I use a convenient library for this purpose, called Flash incidentally. See Flash. I also highly recommend Mikal's Streaming and PString library. The gent writes some really well-thought-out libraries IMHO.

Example using Flash lib:

Serial.print(F("really long debug message "));
  • \$\begingroup\$ There's a function almost identical to yours in the Arduino SD library. You'll have to adjust your result up by the 31 bytes that your test bench uses... :) \$\endgroup\$ Jul 25, 2011 at 17:42

I'm no expert on alternative FAT libraries, but the linked issue and a brief skim of the SD library tells me that it's something of a RAM hog. Your ATmega328 has 2kB of RAM. How much of that are you using in your application? Can that be reduced? For debugging this, create an empty sketch and use this function from SdFatUtil.cpp:

/** Amount of free RAM
 * \return The number of free bytes.
int SdFatUtil::FreeRam() { ...

to find out how much room your sketch has. It sounds like you tried to write the rest of the sketch first, and then add SD.h after the fact.

If you don't see any architectural problems (ie, you're not using any 1.5kB buffers, which would make it nearly impossible to use an SD library which requires 512B of RAM), I'd proceed by moving constant strings and constant definitions to Flash.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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