Go Down

Topic: How to display time on 16x2 LCD (Read 11862 times) previous topic - next topic

hcm2000

Oct 30, 2014, 08:46 pm Last Edit: Oct 30, 2014, 08:48 pm by hcm2000
I know how to display text on LCDs, but I can't figure out how to display time. Can somebody give me the code for displaying time on an LCD or walk me through how to program it?

floresta

This is really a programming question as opposed to an LCD question.

Are you planning to use some sort of RTC (Real Time Clock) chip or are you planning to have the Arduino do all of the work?

Don

MarkiT

#2
Oct 30, 2014, 10:37 pm Last Edit: Oct 30, 2014, 10:38 pm by MarkiT
Don't know if this helps, but it uses an RTC (DS1307 or similar), Uno and a 16x2 LCD display.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
     lcd.begin(16, 2);
   
  // pinMode(8,OUTPUT);
 
  if (! RTC.isrunning()) {
   Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
  //RTC.adjust(DateTime(__DATE__, __TIME__)); // Use ONCE then comment out and reload
  //RTC.adjust(DateTime(2014,10,25,21,10,0)); // Set time manually ONCE as above
  }
}
 
void loop () {
    DateTime now = RTC.now();
    lcd.setCursor(0, 0);
    //lcd.print("Digital Clock");
    lcd.setCursor(0, 0);
    lcd.print(now.day(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.year(), DEC);
    lcd.print(' ');
    lcd.setCursor(0, 2);
     if (now.hour()<10)
    lcd.print('0');
    lcd.print(now.hour(), DEC);
    lcd.print(':');
     if (now.minute()<10)
    lcd.print('0');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    if (now.second()<10)
    lcd.print('0');
    lcd.print(now.second(), DEC);
    lcd.setCursor(12, 0);
   int dayofweek = now.dayOfWeek();
   switch(dayofweek){
     case 1:
     lcd.print("Mon");
     break;
     case 2:
     lcd.print("Tue");
     break;
     case 3:
     lcd.print("Wed");
     break;
     case 4:
     lcd.print("Thu");
     break;
     case 5:
     lcd.print("Fri");
     break;
     case 6:
     lcd.print("Sat");
     break;
     case 0:
     lcd.print("Sun");
     break;
    delay(1000);
}
 
}

hcm2000

I was planning on having the Arduino do all of the work. No RTC

floresta

Quote
Don't know if this helps, but it uses an RTC (DS1307 or similar), Uno and a 16x2 LCD display.
(1) You should have waited for his response to my question.

(2) If you are going to post code then you should use 'code' tags.

(3) If you don't know about code tags then you probably should read this thread:
     How to use this forum

Don

floresta

Quote
I was planning on having the Arduino do all of the work. No RTC
Well it will involve a lot of work for results that will be a lot poorer than those you get from an RTC chip.   Then you still will have to display those results using essentially the same techniques that I assume the previously posted code demonstrates.

The Arduino can inherently keep track of milliseconds so all you have to do is count them.  After 1000 of them you can chalk up one second, 60000 for one minute, 3,600,000 for one hour, etc.

Don


Cactusface

Hi,
      The DS1307 is a cheap and simple chip to use. There's a dedicated library, it has battery backup, have you thought what a power cut will do to your clock, reprogram it all again!!  You can even buy a cheap DS1307 module from China for about £1.50 in real money, or $2USD post paid. It is controlled by the i2c bus so just 4 wires and 2 of them are power, includes backup battery CR2032. This has got to be the easy way.............

Hope it helps, Regards.

Mel.
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

MAS3

Hi hcm2000.

Is you problem in creating a clock, or is it in displaying it ?
You told you know how to display texts.
If the problem is in displaying it, you need to find out how to display values instead of text, and the above code has some samples of that.
Also think about what variables you have in a time display (it's most like not just a single variable).

If you have some half working code, show it so people can help you improve it.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

bperrybap

I was planning on having the Arduino do all of the work. No RTC
If you are going to do that, then I HIGHLY suggest that you use the Time library.
http://playground.arduino.cc/Code/Time
It will do all the time keeping in the background for you and there API functions and examples of how
to access the various date/time elements.

Another added value to using the Time library is that it supports syncing with an RTC
so if you decide that the accuracy of time is not good enough due to the inaccuracy
of the Arduino clock circuity, or that you need time recovery after a loss of power,
you can then add an RTC that
can be used to periodically update the DateTime inside the Time library.
(The Time library does all this for you)

This is good because you only write your mainline code to the Time library then if/when you add the RTC
nothing has to change other than a tiny bit of code in your setup() function to initialize the RTC
and Time library to use the RTC. All the mainline code will continue to use the Time library
just as before.

Also, if using the Time library you can use the TimeZone library to get timezone and daylight savings
change support.
https://github.com/JChristensen/Timezone


For an RTC, I'd recommend using a DS3231 or DS3232 based module as it is much more accurate and
also includes a temperature sensor. The price for this is about the same as the DS1307 type modules.
https://github.com/JChristensen/DS3232RTC


One note of caution related to many of these RTC modules is that they have bugs in the hardware.
On many of them you have to cut a few traces to ensure that modules works correctly and do
not attempt to recharge the non-rechargeable battery.


--- bill

Go Up