LCD clock

Hi all, I am just learning how to work with the arduino. Available to me is a sainsmart 20x4 LCD2004. I am able to make it display simple text ect. but I am currently attempting to make it display time. I have obtained the Time library, but I am VERY confused as to how to start or where to start. Could anyone guide me in some direction. Thanks in advance

Try something like this. I think it works

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <LiquidCrystal_I2C.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

LiquidCrystal_I2C lcd(0x27,20,4);  

void setup(){
  Wire.begin();
  Serial.begin(9600);
  lcd.init();  
    delay(1000);
 lcd.clear();
      lcd.backlight();  //Backlight ON if under program control
      lcd.setCursor(0,0); 
  // Print a message to the LCD.
 lcd.print("Today it is");
 
}

void loop(){
  printDate();
  delay(1000);

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  lcd.setCursor(10,1);
    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      lcd.print("MONDAY    ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUESDAY    ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WEDNESDAY    ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THURSDAY    ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRIDAY    ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SATURDAY    ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUNDAY     ");
       Serial.print("SUN  ");
      break;
  }

  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

   lcd.setCursor(10,2);
  
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
  lcd.print("    ");
  
  lcd.setCursor(0,3);
  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("         ");

}

if you have the Time.h library with that LCD for I2C library, well you can use something like this:

void printTimeToLCD()
{
//Display time on auxilliary LCD
  lcd.setCursor(0,2);// first column, second row
  lcd.print("Time: ");
  if (hour() < 10) lcd.print("0"); //leading zero
  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("   ");
  lcd.setCursor(0,3);// first column, third row
  lcd.print("Date: ");
  if (month() < 10) lcd.print("0");
  lcd.print(month());
  lcd.print("/");
  if (day() < 10) lcd.print("0");
  lcd.print(day());
  lcd.print("/");
  lcd.print(year());
  lcd.print("    "); //cleanup that can actually go, I used it to get rid of the remnants of the last print
}

It positions the ‘cursor’ on the display, and ‘prints’

I clipped it from a clock project i just finished. This is an auxiliary display for troubleshooting and setting. the first two rows were used for displaying other stuff

To Nick_Pyner

I really appreciate your help with the code.

The LCD is printing the correct day as in Sun-Sat, however the day and time are not changing.

For example the LCD prints the day as: 165/165/16 and current time as 45:165:165

Any suggestions?

I happened to have the code to hand but I missed sending the vital link.

http://bildr.org/2011/03/ds1307-arduino/

which includes the time setting procedure.

I believe the 165s suggest the clock module is improperly connected, or a flat battery. If everything is kosher, you should get a time, even if it is just the time in Shanghai. Some clock modules are very particular about the battery and the power available.

Nick_Pyner

You mention the use of some sort of battery, does this mean that I am suppose to be using some sort of RTC?

My original question was geared towards simply using the LCD and ardunio uno and somehow have the arduino get my computer time and display it on the LCD, but if this is not possible, I do have an RTC available (see attached picture).

The RTC was my second question, I was wondering if it is possible to connect the RTC (it has five different pins) to arduino using a breadboard or something since I do not have the plug shield?

From reading online I have learned that the RTC has 5 pins (P, D, G, +, A, I) and that it can run on 5 or 3.3 V but I am not quite sure where I would connect the A4 and A5 from the arduino.

Hope this makes sense and I appreciate your help a lot. Newbie here

165/165/16 means you have a bad connection.

mscisner:
You mention the use of some sort of battery, does this mean that I am suppose to be using some sort of RTC?

Well, yes, and I assumed you were. I now realise that the 165s didn't mean bad conenction, they mean non-possession.

My original question was geared towards simply using the LCD and ardunio uno and somehow have the arduino get my computer time and display it on the LCD, but if this is not possible, I do have an RTC available (see attached picture).

It is possible but a rather futile exercise, particularly as you now have an RTC. Still, Arduino does have an onboard timer and you can do it by organising your own counts and using

delay(1000);

but don't bother.

I am not quite sure where I would connect the A4 and A5 from the arduino.

That's understandable. I assume it is one, but I have never seen an RTC module like that. Only four connections are actually need, and any manufacturer who intended it for Arduino, and knew what they were doing, would have the SDA and SCL pins clearly marked. Maybe JeeLabs can tell you what this is about.

Thanks for the link to the full RTC sketch, I have figured out how to connect the RTC Plug I have and will work on it ASAP.

I had a couple weeks delay due to some other work I had going on.