Go Down

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

pcoe149

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

Phil.

                                                                                                                                                                                       

PaulS

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.

liudr

Here is one:

http://liudr.wordpress.com/shields/phi-2-shield/

Second video. My code runs on 16*2 displays too.

pcoe149

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?

Phil.

RandallR

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.begin(9600);
//Serial.println("\nDate:" __DATE__ "\tTime:" __TIME__);
  Wire.begin();

  lcd.begin(16, 2);
  lcd.clear();
  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();
  setTime(t);

  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()) {
      //DisplayDateTime();
      strftime(szBuff, sizeof(szBuff), "%m/%d %T");
      lcd.setCursor(0, 0); lcd.print(szBuff);
      Serial.print(szBuff); Serial.println("\t");
      delay(200);
    }
    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();
  DS1307::setTime(t);
  setSecs(t);
  Start_t = t;
}

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

  DS1307::getTime(DS1307_tm);
  DS1307_t = makeTime(DS1307_tm);

  Arduino_t = secs();

  DisplayDateTime();

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

  digitalWrite(13, (LED_State=!LED_State));   // Toggle
  delay(1000);
}

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


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.

liudr


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?

Phil.


Phil,

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

https://shop.ruggedcircuits.com/index.php?main_page=product_info&cPath=4&products_id=40

https://shop.ruggedcircuits.com/index.php?main_page=product_info&cPath=4&products_id=39

pcoe149

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>
RTC_DS1307 RTC;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
 
lcd.begin(16, 2);
RTC.begin();
Wire.begin();

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('-');
lcd.print(now.month(), DEC);
lcd.print('-');
lcd.print(now.year(), DEC);
lcd.setCursor(4, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
delay(1000);
}

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

if(now.day < 10)
lcd.print('0');

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.

thanks,

Phil.

CrossRoads

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

lcd.print(now.day(), DEC);
vs
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.

pcoe149

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.

bperrybap

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)
lcd.write('0');
lcd.print((int)hr);
lcd.write(':');
if(min < 10)
lcd.write('0');
lcd.print((int)min);
lcd.write(':');
if(sec < 10)
lcd.write('0');
lcd.print((int)sec);
}


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

liudr

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


Tutorial of sprintf:
http://liudr.wordpress.com/2012/01/16/sprintf/

pcoe149

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.

Thanks,

Phil.

liudr

Read reply #7. You forgot parentheses.

pcoe149

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)
lcd.print('MAY');

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,

Phil.

AWOL

Quote
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