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

2013/07/28

Display Date and Time In Arduino Serial Monitor Using Time.h

First, Download Time.h from the following link.
http://playground.arduino.cc/uploads/Code/Time.zip

Read usage of Time.h library.
http://playground.arduino.cc/Code/time

The function is almost same as the sketch post on May 1, 2012, except adding displaying the day of week.

Pins Used

Analog pins 4.and 5 are used for I2C protocol implemented by Maxim DS1307.

Serial Monitor Output


Sketch

/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * the sketch works on Arduino IDE 1.05
 * modified by Befun Horng 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
 */

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
char *dayOfWeek[] = {"", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup()  {
  Serial.begin(9600);
  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");    
}

void loop()
{
   digitalClockDisplay();
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(year());
  Serial.print("-");
  if (month() < 10) {
    Serial.print('0');
  }
  Serial.print(month());
  Serial.print("-");
  if (day() < 10) {
    Serial.print('0');
  }
  Serial.print(day());
  Serial.print(" ");
  Serial.print(dayOfWeek[weekday()]);
  Serial.print(' ');
  if (hour() < 10) {
    Serial.print('0');
  }
  Serial.print(hour());
  Serial.print(':');
  if (minute() < 10) {
    Serial.print('0');
  }
  Serial.print(minute());
  Serial.print(':');
  if (second() < 10) {
    Serial.print('0');
  }
  Serial.print(second());
  Serial.println();
}