2012/05/01

Freeduino/Arduino Data Logger Shield Creating File Name Combining Date And Time With Timestamp

Created data file name: MMDDhhmm.txt where M means month, D means day , h means hour and m means minute respectively. Once the reset on Arduino is pressed, a new logger file is created.

An alternative is to use a DS1307 RTC shield (such as RTC Sensor Shield) and a SD shield.

Datalogger Shield



Pins Used

Analog pins 4 and 5 are used for I2C protocol implemented by Maxim DS1307.
Digital pins 10, 11, 12 and 13 are used for SPI protocol to save data file to micro SD card.

RTClib.h download web page

http://www.ladyada.net/make/logshield/download.html

Sketch

// File Name: DataloggerFileNameMMDDHHMM.pde
// The sketch works on Arduino IDE 0022
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

File file;                                // test file
const uint8_t SD_CS = 10; // SD chip select
RTC_DS1307 RTC;            // define the Real Time Clock object
String file_name = "";          // file name should not prefix with "prefix_word"
char fn[] = "MMDDHHMM.txt";
int i=0;
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Wire.begin();
  if (!RTC.begin()) {
    Serial.println("RTC failed");
    while(1);
  };
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);

  // following line sets the RTC to the date & time this sketch was compiled
  // RTC.adjust(DateTime(__DATE__, __TIME__));
  DateTime now = RTC.now();

  if (now.month() < 10) {
      file_name = String(file_name + '0' + String(now.month(), DEC));
    }
    else {
    file_name = String(file_name +String(now.month(), DEC));
    }
  if (now.day() < 10) {
      file_name = String(file_name + '0' + String(now.day(), DEC));
    }
    else {
    file_name = String(file_name +String(now.day(), DEC));
    }
  if (now.hour() < 10) {
      file_name = String(file_name + '0' + String(now.hour(), DEC));
    }
    else {
    file_name = String(file_name +String(now.hour(), DEC));
    }
  if (now.minute() < 10) {
      file_name = String(file_name + '0' + String(now.minute(), DEC));
    }
    else {
    file_name = String(file_name +String(now.minute(), DEC));
    }

  file_name = String(file_name + ".txt");
  // Serial.println(file_name);
  // Serial.println(file_name.length());

  for (i=0;i<=file_name.length();i++) {
    fn[i] = file_name.charAt(i);
    // Serial.print(file_name.charAt(i));
  }
  // Serial.println(fn);

  if (!SD.begin(SD_CS)) {
    Serial.println("SD failed");
    while(1);
  }
  Serial.print("File Name: ");
  Serial.println(fn);
  //file = SD.open(fn, FILE_WRITE);
  //file.close();
  // Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {
  DateTime now = RTC.now();

  String data_string = "";
  data_string = String(now.year(), DEC);
  data_string += "/";
  if (now.month() < 10) {
      data_string = String(data_string + '0' + String(now.month(), DEC));
    }
    else {
    data_string = String(data_string +String(now.month(), DEC));
    }
  data_string += "/";
  if (now.day() < 10) {
      data_string = String(data_string + '0' + String(now.day(), DEC));
    }
    else {
    data_string = String(data_string +String(now.day(), DEC));
    }
  data_string += " ";
  if (now.hour() < 10) {
      data_string = String(data_string + '0' + String(now.hour(), DEC));
    }
    else {
    data_string = String(data_string +String(now.hour(), DEC));
    }
  data_string += ":";
  if (now.minute() < 10) {
      data_string = String(data_string + '0' + String(now.minute(), DEC));
    }
    else {
    data_string = String(data_string +String(now.minute(), DEC));
    }
  data_string += ":";  
  if (now.second() < 10) {
      data_string = String(data_string + '0' + String(now.second(), DEC));
    }
    else {
    data_string = String(data_string +String(now.second(), DEC));
    }
  data_string += ",";

  //read sensor value from A0-A3 and append to the string
  for (int analogPin = 0; analogPin < 4; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < 3) {
      data_string += ",";
      }
    }

  file = SD.open(fn, FILE_WRITE);
  if (file) {
    file.println(data_string);
    file.close();
    Serial.println(data_string);
    }
    else {
      Serial.print("error opening ");
      Serial.println(fn);
    }
  //Serial.println(data_string);
 
  delay(995);
}

6 comments:

  1. Great post!! nice work
    bit i have a doubt, how can i create a new log file when the day changes?? i mena, one new log file every day...

    ReplyDelete
    Replies
    1. You can reboot at 00:00:00 everyday using software_Reboot as sketch posted on Oct. 29, 2014.

      Delete
  2. Hi, I am unable to get the thermistor to read correct temperature values. There is a lot of variation. Is there anything wrong with my Datalogger Shield v1.0?

    ReplyDelete
  3. You have to confirm the resistance of resistor you use, analog in varies according to the resistance of thermistor you use.

    ReplyDelete
    Replies
    1. But don't I plug the thermistor directly in the A0 SVG ports? I think I am doing something wrong.

      Delete
  4. To read analog signal, you have to learn what potentiometer is. The thermistor serially connected with resistor is just same as a potentiometer or a voltage divider.

    ReplyDelete