2013/08/15

Analog Sensor Data Logger


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

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

This is the new version of the post on May 01, 2012 with the difference of using Time.h library.

Pin Used

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

Time.h Download

http://playground.arduino.cc//Code/Time

Sketch

// File Name: AnalogDataLoggerTimeLibrary.ino
// The sketch works on Arduino IDE 1.05
#include <SD.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#define analogSensorStart 3
#define analogSensorEnd 3

File file; // test file
const uint8_t SD_CS = 10; // SD chip select
String file_name = ""; // file name should not prefix with "prefix_word"
char fn[] = "MMDDHHMM.CSV";
int i=0;
int displayAtSecond = 0;
time_t t;

//------------------------------------------------------------------------------
// call back for file timestamps 
void dateTime(uint16_t* date, uint16_t* time) {
  time_t timeStamp = now();

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

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(hour(timeStamp), minute(timeStamp), second(timeStamp));

//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Wire.begin();
  setSyncProvider(RTC.get); // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
    else
    Serial.println("RTC has set the system time");
    
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);

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

  file_name = String(file_name + ".CSV");

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

  if (!SD.begin(SD_CS)) {
    Serial.println("SD failed");
    while(1);
  }
  Serial.print("File Name: ");
  Serial.println(fn);
}
//------------------------------------------------------------------------------
void loop()
{
  t = now();
  if (displayAtSecond != second(t)) {
    analogSensorDataLogger();
    displayAtSecond = second(t);
  }
}

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

  //read sensor value from A0-A3 and append to the string
  for (int analogPin = analogSensorStart; analogPin <= analogSensorEnd; analogPin++)
    {
    int sensor = analogRead(analogPin);
    data_string += String(sensor);
    if (analogPin < analogSensorEnd) {
      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);
    }
}

No comments:

Post a Comment