Showing posts with label LCDKeypad shield. Show all posts
Showing posts with label LCDKeypad shield. Show all posts

2013/07/29

LCD Display Date, Time, Weekday From DS1307 And Temperature From LM35

Date and time is useful for many applications. Most visitors to my blog search for rtc stuff. The following sketch show a simple application using a LCD shield, a RTC Sensor shield and a LM35 temperature sensor to display date, time, weekday, and environmental temperature. To use the sketch, you need to download the Time library.

The sketch is a new version of the sketches post on Jun. 3, 2012 and May. 3, 2013 using Time.h library and showing weekday.

Output



Sketch

 /*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * the sketch works on Arduino IDE 1.05
 * 1) modified by Befun Hung on Jul. 28, 2013 
 *    changing the sequence to year, month, day, hour, minute, second
 *    adding the day of week
 *    almost same function as sketch on May 1, 2012
 * 2) modified by Befun Hung on Jul. 29, 2013
 *    change display device to LCD Shield
 *    adding temperature readout from LM35
 *    the sketch does not contain delay() in the loop section 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

char *dayOfWeek[] = {"", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int potPin = 3; // change potPin value to 0, 1, 2 for A0, A1, A2 respectly
float temperature = 0;
int displayAtSecond;

void setup()  {
  lcd.begin(16,2);
  lcd.print("*cheaphousetek*");
  lcd.setCursor(0,1);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     lcd.print("Unable to sync");
  else
     lcd.print("Sync system time ");
  displayAtSecond = second();
  delay(2000);
  lcd.clear();
}

void loop()
{
  if (displayAtSecond != second()) { 
  digitalClockDisplay(); 
  displayAtSecond = second(); 
  }
}

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

void digitalClockDisplay(){
  // digital clock display of the time
  int span = 10;
  long aRead = 0;
  unsigned long temp;
  
  lcd.setCursor(0,0);
  lcd.print(year());
  lcd.print('-');
  if (month() < 10) {
    lcd.print('0');
  }
  lcd.print(month());
  lcd.print('-');
  if (day() < 10) {
    lcd.print('0');
  }
  lcd.print(day());
  lcd.print(' ');
  lcd.setCursor(11,0);
  lcd.print(dayOfWeek[weekday()]);
  lcd.setCursor(0,1);
  if (hour() < 10) {
    lcd.print('0');
  }
  lcd.print(hour());
  lcd.print(':');
  if (minute() < 10) {
    lcd.print('0');
  }
  lcd.print(minute());
  lcd.print(':');
  if (second() < 10) {
    lcd.print('0');
  }
  lcd.print(second());
  lcd.print(' ');
  for (int i=0;i<span;i++) {
    aRead = aRead + analogRead(potPin);
  }
  temperature = (aRead / span * 500.0 / 1024.0); // for other analog sensor change the constants
  printTenths(long (temperature * 10));
  lcd.setCursor(14,1);
  lcd.print(char(223));
  lcd.setCursor(15,1);
  lcd.print('C');
}

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