Go Down

Topic: FileLogger (Read 9 times) previous topic - next topic


I've created a new library for Arduino to append data to files in microSD memory cards. It is based on previous work by David Cuartiello, and it has been used in GPSLogger by Raúl Hevia.

This project focus in just one simple functionality: logging data to a file in SD cards from Arduino boards.

We are trying to get it 1) fast and 2) small, so this library can be used in other projects based on arduino with little impact on memory resources and performance.

By now, it takes about 175 mSecs to append 512 bytes to the file, so transfer rate is about 2925 bytes per second.

The library memory footprint is:

   * 3764 bytes in program memory (Flash)
   * 542 bytes in data memory (SRAM)


The library has been contributed as OpenSource unde GPL license, you can find source code here:


Does the SD card need to be prepared in any fashion, does it need to be formatted a certain way, before using it with this library?

Does the card need to have a file created, with a particular label,  before using it with the library?

Can data be added to more than one file on the SD card?

Using FileLogger, where would data be entered on the arduino board (which pin, or pins)?

My Hardware/Libraries already has an MMC folder, I'm assuming that it will be different from MMC folder that the FileLogger requires, which would result in a conflict. (I'm assuming)


Hi prakat,

yes, the SD card need to be formated with FAT16.
You need to create a file in the root folder, the name is not important as you will give the name of the file when you call the function append(). In fact you can log to more than one file specifying differente file names. The only restriction for the files is that the size must not be zero (at least one character must be written before using the liberay).
This library has been tested with the microSD shield by Libelium: http://www.sensor-networks.org/index.php?page=0827727742
Anyway I guess this should  work with any SPI aware shield.
There should be no conflict with mmc library, as I didn't modify it.

Hope this help


Thanks edugarcia!
I am looking forward to trying this out.

So far, I can't get the example to compile. I get:
In function 'void loop()':
error: 'File_logger' was not declared in this scope

The 3 .o files were created when I started the IDE. Am I missing something?

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll



try to update from subversion, I changed the example some days ago and forgot to change the example code. The point is that from now on you should use FileLogger::append() instead of File_logger.append().

sorry about it,



How is data read from the SD card?
Can data be entered directly to a PC from the card via USB ?

Thank you for your previous information.  :)



yes, data can be read directly from PC. You can open the file with any text editor (i.e. notepad).


What's your experience regarding when you can pull the card? Must power be off?
"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll



in my experience you can pull the card anytime, I haven't had any special care about it and never lost data.
Anyway, I have found from time to time (maybe 1 in 20 times) that the system doesn't recognise the card after inserting it, so write fails (the function append() returns != 0). A friend has proposed a workaround: to write some header in the file before writing the real data. If this write fails, the led blinks five times to inform, so you should remove and re-insert the card. It isn't a perfect solution, but it works.



I've uploaded a new release of the library, V05.
I have removed from MMC the SPI related code, and I have coupled MMC to the Arduino SPI Library instead, as some of you have reported having problems with SPI and this library.
By the way, if you are using SPI in your projects, you can remove Spi.cpp from FileLogger library, it will work.
Together with some small changes in code, we have won 163 bytes in Flash memory in this release, so total size is 3601 bytes now.

Enjoy it,

Eduardo García.


Will this library tolerate the following scenario?

1 - I write several lines to the SD Card
2 - I remove the SD card and re-insert it
3 - I write more lines to the SD Card

I am trying it with your example code, and I get file errors when I try to write after re-inserting the card.  The only way to fix it seems to be resetting the Arduino.




I guess there is some conflict with the monitor serial libraries that I'm using to read keystrokes and write messages, so from time to time I have found such errors, but I think if you use fileLogger in a standalone application, with no monitor, it will work fine.
Anyway, there is a workaround: anytime you find errors writing after re-inserting the card, close and reopen the serial line (click on the icon at the upper right). This will not reset Arduino, only the serial line.


@RB123, I had the same problem (I am using the SD card socket from Sparkfun, not MicroSD, with a Panasonic card). Also, mine is running on solar and if the voltage dropped and then came back up, the card would stop working.

I made a change in nanofat.cpp (this is a TOTAL hack, but I may go through it again when I have time). Basically it was erroring out in locateFileStart when it called mmc::readSector. I added code to line 183, after the second-to-last closing bracket in the function:
Code: [Select]
} [glow]else {

 return false;

So it will still return false to show it had an error, but it will reinitialize the card and usually works on the next write, although sometimes it take 2 writes to kick it back on.

I anybody with better coding skillz wants to improve on my hack, feel free.  :D



I'm using FileLogger lib.
I would like to create a function for logging info bat i have a problem with variables:

my code:

Code: [Select]

void write_log(char msg[]){
unsigned int length = (strlen(msg)+1);
 byte buffer[length];
 buffer = msg;
 FileLogger::append(FILE_LOG, buffer, length);

on my loop:

Code: [Select]

write_log("test 123 prova 123");

but error generated
Code: [Select]

In function 'void write_log(char*)':
error: incompatible types in assignment of 'char*' to 'byte [(((unsigned int)(((int)length) + -0x000000001)) + 1)]

please help me?


Go Up