2012/07/07

Freeduino/Arduino + Datalogger Shield + LCD Keypad Shield + Sensors Connecting To A1, A2 And A3

Dataloggers can be easily used to record sensor readings from analog inputs. With following sketch, anyone can easily record data read from analog input by just stacking Freeduino/Arduino, Datalogger Shield and LCD Keypad Shield and connecting analog ins from A1 to A3.

The data read is raw data, you have to adjust the sketch to do the conversion according the sensor specification. For example, LM35 sensor need to calculate the value of (raw data * 500 / 1023) .

With the data file, you can use data visualization software , such as Gnu Plot, to display data in graph.

Pins Used

cheaphousetek Datalogger Shield ---
RTC: A4 and A5
mSD: D10, D11, D12 and D13

cheaphousetek LCD Keypad Shield ---
LCD: D8, D9, D4, D5, D6 and D7
Keypad: A0

Sensors(LM35): A1, A2 and A3

Output Of LCD Keypad Shield



Contents Of Output File


Sketch

// File name: DataloggerFileNameMMDDHHMMLCD.pde
// The sketch works on Arduino IDE 0022

#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

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 = "", dataString;    // 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);
  };
  lcd.begin(16, 2);
  // 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 A1-A3 and append to the string
  delay(195);
  for (int analogPin = 1; analogPin <4; analogPin++) 
    {
    delay(200);
    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);
    }
  lcd.clear();
  lcd.setCursor(0,0);
  dataString = String(data_string);
  lcd.print(dataString.substring(5, 19)); 
  lcd.setCursor(0,1);
  lcd.print(dataString.substring(20));
  //Serial.println(data_string);
   
  // delay(195);
}