Showing posts with label temperature sensor. Show all posts
Showing posts with label temperature sensor. Show all posts

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);
}

2012/06/03

Freeduino/Arduino + Real Time Clock Sensor Shield + LCD Keypad Shield + LM35 Temperature Sensor


Pins Used

cheaphousetek RTC Sensor Shield ---
Analog pins 4 and 5 are used for I2C protocol implemented by Maxim DS1307.

cheaphousetek LCD Keypad Shield ---
Digital pins 8, 9, 7, 6, 5 and 4 are used by LiquidCrystal.h to communicate with LCD Keypad.

LM35 Temperature Sensor ---
Analog pin 3 is used read temperature data from LM35 temperature sensor.

To save data to micro SD, follow the link below.

http://cheaphousetek.blogspot.tw/2012/07/freeduinoarduino-datalogger-shield-lcd.html

There is a new version using Time.h library and showing weekday.

RTClib.h download web page

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



LCD Output

Sketch


// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// 2010-02-04 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
// $Id: ds1307.pde 4773 2010-02-04 14:09:18Z jcw $

// Added LCD display and LM35 temperature function by Befun Hung 2011-10-13
// File Name: LCDDS1307RTClibLM35A3.pde
// The sketch works on Arduino IDE 0022

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

RTC_DS1307 RTC;
int potPin = 3;
float temperature = 0;

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    lcd.begin(16, 2);
    // lcd.print("*cheaphousetek*");
    
    // following line sets the RTC to the date & time this sketch was compiled
    // RTC.adjust(DateTime(__DATE__, __TIME__));
}

void printTenths(long value) {
  // prints a value of 123 as 12.3
  Serial.print(value / 10);
  Serial.print('.');
  Serial.print(value % 10);
  Serial.println();
  lcd.setCursor(10, 1);
  lcd.print(value / 10);
  lcd.setCursor(12, 1);
  lcd.print('.');
  lcd.setCursor(13, 1);
  lcd.print(value % 10);
}

void loop () {
    DateTime now = RTC.now();
    int span = 10;
    long aRead = 0;
    unsigned long tmp;
      
    tmp = now.year();
    Serial.print(now.year(), DEC);
    lcd.setCursor(0, 0);
    lcd.print(tmp);
    Serial.print('/');
    lcd.setCursor(4, 0);
    lcd.print("-");
    
    tmp = now.month();
    if (now.month() < 10) {
      Serial.print('0');
      Serial.print(now.month(), DEC);
      lcd.setCursor(5, 0);
      lcd.print('0');
      lcd.setCursor(6, 0);
      lcd.print(tmp);
    }
    else {
      Serial.print(now.month(), DEC);
      lcd.setCursor(5, 0);
      lcd.print(tmp);
    }
    Serial.print('/');
    lcd.setCursor(7, 0);
    lcd.print('-');
    
    tmp = now.day();
    if (now.day() < 10) {
      Serial.print('0');
      Serial.print(now.day());
      lcd.setCursor(8, 0);
      lcd.print('0');
      lcd.setCursor(9, 0);
      lcd.print(tmp);
    }
    else {
      Serial.print(now.day(), DEC);
      lcd.setCursor(8, 0);
      lcd.print(tmp);
    }
    Serial.print(' ');
    
    tmp = now.hour();
    if (now.hour() < 10) {
      Serial.print('0');
      Serial.print(now.hour(), DEC);
      lcd.setCursor(0, 1);
      lcd.print('0');
      lcd.setCursor(1, 1);
      lcd.print(tmp);
    }
    else {
      Serial.print(now.hour(), DEC);
      lcd.setCursor(0, 1);
      lcd.print(tmp);
    }
    Serial.print(':');
    lcd.setCursor(2, 1);
    lcd.print(':');
    
    tmp = now.minute();
    if (now.minute() < 10) {
      Serial.print('0');
      Serial.print(now.minute(), DEC);
      lcd.setCursor(3, 1);
      lcd.print('0');
      lcd.setCursor(4, 1);
      lcd.print(tmp);
    }
    else {
      Serial.print(now.minute(), DEC);
      lcd.setCursor(3, 1);
      lcd.print(tmp);
    }
    Serial.print(':');
    lcd.setCursor(5, 1);
    lcd.print(':');
    
    tmp = now.second();
    if (now.second() < 10) {
      Serial.print('0');
      Serial.print(now.second(), DEC);
      lcd.setCursor(6, 1);
      lcd.print('0');
      lcd.setCursor(7, 1);
      lcd.print(tmp);
    }
    else {
      Serial.print(now.second(), DEC);
      lcd.setCursor(6, 1);
      lcd.print(tmp);
    }
    Serial.print (' ');
    lcd.setCursor(8, 1);
    lcd.print(' ');
    // Serial.println();
    
    for (int i=0;i<span;i++) {
      aRead = aRead + analogRead(potPin);
      // Serial.print(aRead);
      // Serial.print(' ');
    }
    // aRead = aRead / span;
    temperature = (aRead / span * 500.0 / 1024.0);
    // Serial.print(aRead);
    // Serial.print(' ');
    // Serial.print(temperature);
    // Serial.print(' ');
    printTenths(long (temperature * 10));
    lcd.setCursor(14, 1);
    lcd.print(char(223));
    lcd.setCursor(15, 1);
    lcd.print('C');
        
    delay(968);
    
    // Serial.print(" since 2000 = ");
    // Serial.print(now.get());
    // Serial.print("s = ");
    // Serial.print(now.get() / 86400L);
    // Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    // DateTime future (now.get() + 7 * 86400L + 30);
    
    // Serial.print(" now + 7d + 30s: ");
    // Serial.print(future.year(), DEC);
    // Serial.print('/');
    // Serial.print(future.month(), DEC);
    // Serial.print('/');
    // Serial.print(future.day(), DEC);
    // Serial.print(' ');
    // Serial.print(future.hour(), DEC);
    // Serial.print(':');
    // Serial.print(future.minute(), DEC);
    // Serial.print(':');
    // Serial.print(future.second(), DEC);
    // Serial.println();
    
    // Serial.println();
    // delay(3000);
}