Go Down

Topic: Help Needed Building Clock (Read 4381 times) previous topic - next topic


I am looking for some help in building a clock. i have an unused 16x2 line LCD display in my car and thought i would make use of it by turning it into a clock, i have so far managed to get my arduino to display text and a count in seccons but have no idea how to go about making a clock so i was wondering if anyone would be able to help me. I am an electronics engineer so i have a very good understanding of teh hardware side of things but not on teh software/programing side of it. my setup is as follows.

Arduino nano v3.0, 16x2 character LCD display and DS1307 Real-time clock condule,  I have wired as follows to the Arduino

    LCD RS pin to digital pin 12
    LCD Enable pin to digital pin 11
    LCD D4 pin to digital pin 5
    LCD D5 pin to digital pin 4
    LCD D6 pin to digital pin 3
    LCD D7 pin to digital pin 2

Plus power and ground/backlight power and ground to the LCD

DS1307 SCL pin to pin A5
DS1307 SDA pin to pin A4

Any help anyone would be able to give me would be gratefuly received. i would like top display date and time on the display and have attached an immage of how i would liuke it to be displayed.

thanks in advance




There are examples all over the place for setting/getting time from a DS1307. The rest is just a matter of displaying the text in the right places.
The art of getting good answers lies in asking good questions.


Here is one:


Second video. My code runs on 16*2 displays too.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter


Looking at the site, this looks just the job, i could just not use the included display and connect to teh existing one, do you ship to the UK though?



I just happen to be playing around with RTC chips and I use a 16x2 LCD all the time for my testing.  I have a little breadboard that I have wired up with a Nano, 16x2 LCD and currently a PCF8563 RTC.  A couple days ago it was a DS1307.  I think next I will play with a DS1302.

I use a modified version of the Time library.  It has most of the functions that I need.  I added the strftime() function to make it easier to format time in different ways.

Here is a little sketch I wrote to check for clock drift.  I wanted to know how much the Arduino time drifted when compared to the DS1307.  I also check the time with time.gov.
Code: [Select]

/* The LCD is usually interfaced via 16 pins which are labelled as shown below:
    LCD Pin
    1. GND - Ground
    2. VDD - 3 - 5V
    3. VO  - Contrast
    4. RS  - Register Select - Command (0) or Character (1)
    5. RW  - Read/Write - Write (0) or Read (1)
    6. E   - Enable - Enable data transmit (0)
    7. DB0 - Data Bit 0
    8. DB1 - Data Bit 1
    9. DB2 - Data Bit 2
   10. DB3 - Data Bit 3
   11. DB4 - Data Bit 4 - used in 4 bit operation
   12. DB5 - Data Bit 5 - used in 4 bit operation
   13. DB6 - Data Bit 6 - used in 4 bit operation
   14. DB7 - Data Bit 7 - used in 4 bit operation
   15. BL1 - Backlight +
   16. BL2 - Backlight -
//   I2C
//   A4 - Data  (SDA)
//   A5 - Clock (SCL)
                         //Connections to Arduino
                         //LCD        Ardunino
                         // 1. GND    N/A
                         // 2. VDD    N/A
                         // 3. VO     N/A  (Tap off a 5K - 10K pot across VCC and Ground)
#define LCD_RS       12  // 4. RS     D12
                         // 5. RW     GND
#define LCD_ENABLE   11  // 6. E      D13
                         // 7. DB0    None
                         // 8. DB1    None
                         // 9. DB2    None
                         //10. DB3    None
#define LCD_DB4       4  //11. DB4    D4
#define LCD_DB5       6  //12. DB5    D6
#define LCD_DB6       7  //13. DB6    D7
#define LCD_DB7       8  //14. DB7    D8
#define LCD_Backlight 9  //15. BL1    Emitter of 2N3904, Collector to VCC, Base to D6 via 10K resistor
                         //16. BL2    GND

//   I2C
//   A4 - Data  (SDA)
//   A5 - Clock (SCL)
#include <Wire.h>
#include <Time.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
static time_t Start_t;
LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_DB4, LCD_DB5, LCD_DB6, LCD_DB7);
unsigned char LED_State;
void setup () {
//Serial.println("\nDate:" __DATE__ "\tTime:" __TIME__);

  lcd.begin(16, 2);
  pinMode(LCD_Backlight, OUTPUT); analogWrite(LCD_Backlight, 128); // Set the brightness of the backlight
  pinMode(13, OUTPUT);

  DS1307::SetFromCompileDateTime(__DATE__ __TIME__);
  time_t t = DS1307::getTime();

  Serial.println("\nFine tune the time.");
  Serial.println("Press (-) to decrease time by 1 second.");
  Serial.println("Press (+) to increase time by 1 second.");
  Serial.println("'X' when finished.");
  char c, szBuff[16];
  do {
    Start_t = secs();
    while (!Serial.available() &&
           Start_t+15 > secs()) {
      strftime(szBuff, sizeof(szBuff), "%m/%d %T");
      lcd.setCursor(0, 0); lcd.print(szBuff);
      Serial.print(szBuff); Serial.println("\t");
    c=Serial.read();  Serial.println((byte)c, HEX);
    switch (c) {
      case '+': adjustTime(1); break;
      case '-': adjustTime(-1); break;
    }  /* switch */
  } while (c!='x' && c!='X' && c!=-1);
  t = now();
  Start_t = t;

void loop() {
  tmElements_t DS1307_tm;
  time_t DS1307_t, Arduino_t;

  DS1307_t = makeTime(DS1307_tm);

  Arduino_t = secs();


  lcd.setCursor(0, 1);  lcd.print("                ");
  lcd.setCursor(0, 1);
  int iLapsed = DS1307_t - Start_t;
  int iDiff = DS1307_t - Arduino_t;
  if (iDiff != 0) {
    lcd.print(", D:");  lcd.print(iLapsed / iDiff);

  digitalWrite(13, (LED_State=!LED_State));   // Toggle

void DisplayDateTime(void) {
  char szBuffer[16];
  strftime(szBuffer, sizeof(szBuffer), "%m/%d %T");
  lcd.setCursor(0, 0);

I also added the 'secs()' function to the Arduino system.  I proposed to the powers that be that it would be a good addition to go along with the millis() and micros() functions.  I also added a way to set the seconds so you could count the number of seconds from a known starting point like 1970/1/1.  I was testing that as well.

If you are interested, I can send the code to strftime() but it is quite large.

Let me know if you have any questions about the code.  I may have an EE question for you.


Looking at the site, this looks just the job, i could just not use the included display and connect to teh existing one, do you ship to the UK though?



Yes I ship to UK. Here are two links to 16X2 and 20X4 shields that ruggedcircuits.com sells for me:


Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter


Forstly thanks for all your replies, i have got so far with the project but come to a halt again,

i can display the date and time but when the numbers are between 1 and 9 the display only shows a single digit i.e. '5' not '05' my script is as follows so far.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);

if (! RTC.isrunning()) {
lcd.print("RTC NOT RUNNING!");
RTC.adjust(DateTime(__DATE__, __TIME__));

void loop () {
DateTime now = RTC.now();

lcd.setCursor(4, 0);
lcd.print(now.day(), DEC);
lcd.print(now.month(), DEC);
lcd.print(now.year(), DEC);
lcd.setCursor(4, 1);
lcd.print(now.hour(), DEC);
lcd.print(now.minute(), DEC);
lcd.print(now.second(), DEC);

I thought it would be a simple case of adding the function

if(now.day < 10)

for example for the day but when i do so all i get is an error saying

"invalid use of member (did you forget the '&' ?)

can anyone help me on this one please?

i did get this up and running properly yesterday but then lost my sketch (Lesson learned to back things up and save regularly)

This is the one final bit i would like to get resolved before it goes ion teh car.




You left out the () after now.day perhaps?

lcd.print(now.day(), DEC);
if(now.day < 10)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


That Didnt seem to work, what i am trying to do firstly is precede the day of the week or month with a 0 if it is between 1 - 9. later i plan to youse the function to replace the month with the name of the month if i can get this to work.


Here is function that I use to print a time:
Code: [Select]
void lcdPrintTime(uint8_t hr, uint8_t min, uint8_t sec)
if(hr < 10)
if(min < 10)
if(sec < 10)

I'll set the cursor position, then call this function.
It doesn't do the date but you get the idea.
I'd do a different function for the date.
That way you can simply set the position for the date, print the date
then set the position for the time and print the time.
You can move locations or swap lines easily that way.

--- bill


Do this
Code: [Select]
char buffer[12];
sprintf(buffer, "%02d-%02d-%4d", day, month, year);

Tutorial of sprintf:
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter


Thanks for all your replies guys but i have tried all your sugestions and still cant get it working right - the anoyance is i did have it working before i lost the sketch, i am happy with the basic code i have i just want to be able to use the iff function correctly and want to know what i am doing wrong for it to be saying have you forgot the & symbol?

Any other sugestions would be great.




Read reply #7. You forgot parentheses.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter


Thanks for your reply, im all up adnd running now - thankfully, last question, to substitude the month from a number too a word i have been using the expression:

if(now.month() == 5)

Obviosly just for MAY but when i upload it just displays a load of numbers no letters, am i doing something wrong here?

Thanks again,



am i doing something wrong here?

Yes, I think you probably are.

Of course, if I could see your code, I'd probably be able to say for sure.

You should, of course, use double  quotes, not single, for "MAY"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up