I'm trying to datalog to a EyeFi SD card by writing to a file with a jpg extension. Here's that portion of my code so far:

// see if the directory exists, create it if not.
if( !SD.exists("/DCIM/100NIKON") ) 
  if( SD.mkdir("/DCIM/100NIKON") ) 
    Serial.print("File directory created: ");
  else {
    error("File directory not created");
  Serial.print("File directory exists: ");

// Test file
char filename[] = "DSCN0000.JPG";

if (! SD.exists(filename))
  logfile = SD.open(filename, FILE_WRITE); // only open a new file if it doesn't exist

if (! logfile)
  error("Couldnt create file");

The Arduino creates the directory, but the file is still saved to the root. I would love some help or tips on this.

1 Answer 1


You're creating a directory, and then not writing the file into it.

You need to change the filename to /DCIM/100NIKON/DSCN0000.JPG";

Note that SD.open() takes the full filepath to the file, not just the file*name*.

From the Arduino Docs:

The file names passed to the SD library functions can include paths separated by forward-slashes, /, e.g. "directory/filename.txt". Because the working directory is always the root of the SD card, a name refers to the same file whether or not it includes a leading slash (e.g. "/file.txt" is equivalent to "file.txt"). As of version 1.0, the library supports opening multiple files.

(emphasis mine)

  • Oooh, I was thinking that after creating a directory with mkdir, the directory will automatically become the working directory. Thanks a lot! Aug 20, 2014 at 23:51
  • @user2218339 - Nope, and as far as I can tell, there isn't even a way to change the working directory at all. It's basically hard-wired to /. Aug 21, 2014 at 0:34

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.