Blinkerton Labs
  • Blog
  • Projects
    • AutoBoat
    • Blinkly Blink
    • Wireless Temperature Monitor
  • Reference
    • PCB Design
  • About

Working with the Arduino SD Shield

9/1/2014

 
Short version: only use 8 characters for file names when using the Arduino SD library.

Recently, I added the Adafruit SD Shield to a project so i could log some GPS data.  The Adafruit site has a great code walk through of how to use the shield. The Arduino reference site has some great information about the SD Library as well.

The first time I tried writing files to the SD card, I was trying something like "20140829144001_File.txt", and it did not work at all.  Through some experimentation, I found that the file name "SomeFile.txt" worked.

I took a look at the example Adafruit code and found the following.

// create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }

I noticed their file name was 8 characters, the same as my "SomeFile.txt" that worked.
After doing some research I came across the following information about working with the SD shield from the Arduino site.

"FAT file systems have a limitation when it comes to naming conventions. You must use the 8.3 format, so that file names look like “NAME001.EXT”, where “NAME001” is an 8 character or fewer string, and “EXT” is a 3 character extension. People commonly use the extensions .TXT and .LOG. It is possible to have a shorter file name (for example, mydata.txt, or time.log), but you cannot use longer file names."

One thing I must have passed over on the Arduino site the first time I looked at the SD library reference was the following.
"The library supports FAT16 and FAT32 file systems on standard SD cards and SDHC cards. It uses short 8.3 names for files."

I would really like to use a date field in the file name to keep track of things, but I just put that in the first column of the data.
Since I couldn't make unique file names, I would have to iterate like the Adafruit strategy.  Instead of limiting the max file count to 100, I wanted to do 1000.  So I modified the Adafruit to do the following.

// create a new file
  for (uint8_t i = 0; i < 1000; i++) {
    int indexHolder = i;
    filename[7] = indexHolder%10 + '0';
    indexHolder = indexHolder / 10;
    filename[6] = indexHolder%10 + '0';
    indexHolder = indexHolder / 10;
    filename[5] = indexHolder%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist 
      break;  // leave the loop!
    }
  }

    Author

    Mainly random electronics projects.  Sometimes other topics.

    Archives

    January 2020
    March 2019
    January 2019
    July 2017
    April 2016
    January 2015
    September 2014
    March 2014

    Categories

    All
    Arduino

    RSS Feed

Proudly powered by Weebly