Skip to Content

Creating new files on OpenLog with Arduino

Sparkfun has made a small electrongic device known as OpenLog. This device takes a micro SD memory card (up to 2 GB), and will automatically create a file on power up and then log anything it sees on a serial input line. Even better, if you hook it up to a computer, you can enter into a command mode and create new files, delete existing files, create directories, etc. Out of the box, this is a great tool for logging larger amounts of data (up to 2 GB, which is LOTS) with very little effort. All in a package about the size of a quarter. And flexible enough to be hooked up to many different data sources - including an Arduino.

If you are happy with the default file names of "LOG01.txt", where the "01" automatically increments on power cycle, then you are all set. However if you want create your own file names, then there are some hurdles to jump through. Connecting the OpenLog to your computer and creating a file is one thing. But getting your Arduino to create files on the fly turns out to be a little more challenging.

Reading the documentation, you can see you need to send a CTRL-Z (ASCII 26) to the device to enter command mode, and then the command "new myfile.log". However, what is lacking in the docs are the specific details. This information on it's own is not sufficient.

First, creating a new file is only the first step. You then have to tell OpenLog to append to that file. So now we have 3 commands we need:

CTRL-Z
new myfile.log
append myfile.log

And this still will not work quite right. So I next considered the line-feeds. I run on Linux, so naturally assume that a carriage return is sufficient. However Windows boxes need a carriage return/line feed. It would seem the OpenLog is expecting the CR/LF sequence. Though I was able to omit that step after some testing.

The one other piece of important information is timing. Each commands takes a brief amount of time to complete. Issuing the next command before the current command is done results in a non-functioning program.

Taking all this info together, I have come up with the following code that does work. That is, I can now get the Arduino to change the name of the file it is logging to, on the fly.

#include 

NewSoftSerial logger(4,5);

void setFile(char fname[40])
{
  int wait = 200;
  Serial.println(fname);
  logger.println(26, BYTE);
  delay(wait);
  logger.print("new ");
  logger.println(fname);
  delay(wait);
  logger.print("append ");
  logger.println(fname);
  delay(wait);  
}

void setup() {
  Serial.begin(9600);
  logger.begin(9600);
  setFile("20100331.log");
}

void loop() { 
  unsigned long m = millis();
  unsigned long f = 0;
  char fn[40] = "";
  
  if (m % 5000 == 0) {
    f = m * (10000) + (6 * 100) + 3;
    setFile(ultoa(f, fn, 10)); 
    Serial.print("millis: ");
    Serial.println(m);
    logger.println(m);
  }
}

This code does a few things, most of which is to trigger the change of file name, and log something to the new file. This code is mostly self explanatory. Every 5 seconds, it uses the millis() function to build a string that we pass to the setFile() function. So lets focus on the "setFile()" function.

NOTE: The "logger" object is another serial port set up via NewSoftSerial. Feel free to use the hardware serial directly if you'd like - just remember to disconnect your TX/RX pins when you send code to the Arduino.

  • The setFile() function takes an character string parameter. This will be the name of the new file we are wanting. I've arbitrarily chosen a max string length of 40 characters (including the null terminator).
  • Next I set the "wait" variable. This was just me being lazy, as we need to wait a few times through the routine, and using the variable means I only have to change the wait period in one place.
  • We dump the passed file name to the Hardware Serial port so we can monitor what is happening.
  • Then we send the CTRL-Z character to put OpenLog into command mode.
  • Now we wait a moment to let the OpenLog process our request to enter command mode.
  • Then we build the request for the new file, and issue another wait.
  • Finally, we issue a request to append to the file we just created and wait again.

At this point, we should have a new file on our memory stick, and be logging to that file.

The important things here is the waiting, and the order of the commands. Notice two that we are using "logger.println()" to terminate a request. The println() function will automatically send the needed CR/LF for us. Or we can issue a print("\r\n") command followed by a wait.

I have not yet found the magic point for the delays. I know a delay of 50 ms is not sufficient, but 200 ms is. Somewhere in between those two ranges is the minimal value. For my needs, I do not need to be THAT precise. My app will not suffer waiting an extra 100 ms or so.

Armed with this information, I am intending on modifying the code from my last post to create new files using the date as the file name (i.e. 20100331.log, for March 31st 2010).

Being the novice electronics guy I am, I struggled with changing the file name from the Arduino. I did not find any pages that explicitly walked me through the process, so I really do hope this helps others in the similar situation. NOW that I have this info, I can see why saying "send CTRL-Z, followed by a new file request" sounds so straight forward to the more experienced crowd. But the knowledge barrier is often a challenge to overcome.

On to my next project.