Arduino As A Clock

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
NoDelta
 
Posts: 25
Joined: Sun Sep 22, 2013 7:05 pm

Arduino As A Clock

Post by NoDelta »

Hello,
I`m completely new to arduino, circuits, programming, etc - but I went to the Make Fair in NYC a couple weeks ago and left feeling inspired to build my own binary clock.
I picked up the Arduino starter kit from this site and have successfully been through the older set of tutorials (the newer ones require a shift-register and that's in the mail).
The big question that I have is that my clock isn't accurate. I've been losing ~1 second every hour. I had read online that these boards are good for 1-2 seconds per day, so I`m well above that.
Here's my code:

Code: Select all

#include <Time.h>                       //Need this to count the seconds.
int s1  = 0;
int s10 = 0;
int m1  = 0;
int m10 = 0;
int hours = 0;
int h1  = 0;
int h10 = 0;
int timecheck;

void setup() {
  Serial.begin(9600);                   //Set up serial communication at 9600bps
  setTime(12,0,0,1,1,11);
  timecheck = second(); }

void loop(){
//////////     time keeping     //////////
  if (timecheck != second()) {
    s1++;
    timecheck = second();
    if (s1 == 10)  {
      s1=0;
      s10++;    }
    if (s10 == 6 )  {
      s10 = 0;
      m1++;    }
    if (m1 == 10)  {
      m1 = 0;
      m10++;    }
    if(m10 == 6)  {
      m10 = 0;
      hours++;    }
    if(hours<10)  {
      h1 = hours;
      h10 = 0;    }
    else {
      if(hours<12)  {
        h1 = hours-10;
        h10 = 1;      }
      else {
        hours=0;
      }}

// The next bits print to Serial, and I assume they're not relevant to this issue

  }

//////////     seconds ones display     //////////

  if (s1 == 1 || s1 == 3 || s1 == 5 || s1 == 7 || s1 == 9)  {
    digitalWrite(8,HIGH);  }
  else { digitalWrite(8,LOW);  }

//similar if's for the other LED lights to display seconds as 0-9
//once i get the shift registers there will be lots more similar code
}
So - is there a more accurate way to track the time?
I thought about just using the hour(), minute(), second(), etc that load with Time.h, but it seems like that would make the LED display more complicated.

Thanks in advance!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino As A Clock

Post by adafruit_support_bill »

The Arduino Uno uses a resonator for the processor clock. These are somewhat less accurate than a crystal. I suspect that the 1-2 seconds/day figure applied to earlier Arduino models which used a crystal.

To get a more accurate time-base you could attach a Real Time Clock module:
http://www.adafruit.com/products/264
http://www.adafruit.com/products/255
The DS1307 chip is the same one used in the Monochron and BulbDial clock kits. The Chronodot is a temperature-compensated version of that chip that is even more accurate.

The other advantage of the RTC modules is that they have built-in battery backup so you won't lose time on a power failure.

NoDelta
 
Posts: 25
Joined: Sun Sep 22, 2013 7:05 pm

Re: Arduino As A Clock

Post by NoDelta »

Thanks for the quick response.
Do you know what it is about the difference between the resonator and the crystal that makes such a big difference?
I mean, if the resonator is consistently off by 1 second every hour, this would be easy enough to program into the code.
Based on the 35 hours I've left it running thus far - I`m seeing a 1.16s/hr difference.
Accounting for a 1 second/hr difference would cut my error down to 4 sec/day, which seems pretty reasonable to me. Is a resonator variable enough that this isn't a viable solution?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino As A Clock

Post by adafruit_support_bill »

The resonator frequency will vary somewhat with temperature. If it is running in a reasonably stable environment, you should be able to improve the long-term accuracy with periodic adjustments in software.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”