How to keep time with a 16mhz crystal?

I've been researching methods to keep accurate time on arduino. I found that external hardware does this best using either crystal/oscillator or IC. I'll split this question into 2:

  • Using an Arduino UNO, how do you wire a 16mhz crystal to the Arduino? How do you program the Arduino to receive the data from the crystal? What kind of data does the crystal spew out?

  • I would still like to know the answer to the previous question, but what are other methods of keeping time relatively accurately. I also have a 555 timer IC, but this seems like overkill.

Most tutorials were either for the Atmega by itself, or just not very explanatory as is. Any thorough tutorial on wiring hardware and basic programming would help. It's probably very simple, but only when you know how to do it.

Thanks for your time, DM.

A 16mHz crystal is probably unavailable. It would oscillate once every 62.5 seconds.
Perhaps you meant 16MHz.

dmor574:

  • I would still like to know the answer to the previous question, but what are other methods of keeping time relatively accurately. I also have a 555 timer IC, but this seems like overkill.

555 timer, well, wouldn't be my first choice.. Well it depends on how accurate you want it to be and the time "range", ms, ns etc. RTC chip is the best choice in most cases.. Here is a link for everything you need to know..

http://playground.arduino.cc/Main/DS1302

The UNO already has a 16MHz crystal on the card. See the entry in the products section of the sit. You should also read the datasheet for the ATMega's. Crystals do not "What kind of data does the crystal spew out". They don't give out data at all.

Run the chip at 8Mhz no external parts required (as I recall), again see the datasheet.

No programming required.

Mark

Using an Arduino UNO, how do you wire a 16mhz crystal to the Arduino?

The joy of open source

Just to clarify, this would be used for a clock (regular hours/mins/seconds/year clock). The crystal I have says: "TXC 16.OET 17".

Are you saying the board can use its crystal to keep accurate time? Do you suggest any libraries for the programming aspect?

Thanks for all the replies so far, DM.

Why do you want to wire a 16MHz crystal to a device that already has one?

During my research, I read that the Arduino can't keep accurate time for a clock, losing seconds or minutes each day and that a solution was to add another crystal.

Look for "RTC"

Mark

I believe the Uno has a resonator, not a crystal.

I read that the Arduino can't keep accurate time for a clock, losing seconds or minutes each day

You need to define some figures here. A resonator will have an error amount (say in ppm). So will a crystal. So will a 555 timer circuit. So will a real-time clock. Some RTCs are temperature-compensated, and thus more accurate.

What is the acceptable error amount? Don't say "zero" because you won't get that unless you own an atomic clock facility, and even that won't have a zero error amount.

If you need to feed some fish at 6 am, then the on-board resonator will be fine, they won't care if they are fed at 6:02 am.

One thing you can do is work out if the millis() figure is out by a fixed amount. For example, if it loses 10 seconds a day, just compensate for that in your calculations.

I believe that the chronometers that sailors used to use were accurate, not because they showed the right time, but because any error was a fixed error. That is, the clock might be 5 seconds slow each day, but it was always 5 seconds slow. So the navigator would multiply the number of days since the clock was last calibrated, add/subtract that from the time shown, and have a pretty accurate idea of the current time.

Probably the most accurate time-keeping would be to spend $50 or so on a GPS module, and have it return the time-of-day, which it gets from the satellites, which I believe is pretty accurate.

dmor574:
During my research, I read that the Arduino can't keep accurate time for a clock, losing seconds or minutes each day and that a solution was to add another crystal.

Well, how accurate do you need it to be? Do you want to set the time every time you reset your micro? Do you want date information too? If you want accurate, convenient time then use an external RTC like a Dallas 1307 or something. If you just want to accurately know when seconds or minutes go by, then you can use the internal timers and the crystal that already comes with the board. It is likely within 50 parts per million so would be accurate to about 5 seconds per day or better. A 32.768 kHz crystal can be used to clock an internal timer as well and it would be accurate to about 1 second per day. To accomplish this, you'll have to "interfere" with the way the Arduino sets up the Timers and what not which would likely have repercussions such as not being able to use PWM on all the pins or something like that. I'd just go with the external RTC and a battery backup if you want convenience and accuracy.

If you want more accurate external RTC, you can get the DS3231 about 2 to 3 ppm per year
you can get it for less then US$6

"The UNO already has a 16MHz crystal on the card."
No, it has a ceramic resonator.

Older Duemilanove's have a 16 MHz crytal (the big can next to the chip).

Using micros( ) to keep track of seconds going by, and updating previousMillis by a fixed amount every time 1 second has elapsed, I only see 1 second of drift a day on crystal equipped boards:

void loop(){
currentTime = micros();
if ( currentTime - previousTime) >= interval){
preciousTime = previousTime + interval;  // all are unigned long datatypes, interval = 1,000,000 uS
seconds = seconds +1;
if (seconds == 10){
   seconds = 0;
   secondsTens = secondsTens +1;
   if (secondsTens = 10){
       secondsTens = 0;
       minutes = minutes +1; 
       etc.

Track seconds, 1/10s of seconds, 1/100s of seconds,
go up to hours, days, etc. Just keep track of when to roll things over.
For example, rolling over the hours when hoursTens = 3 for a 24 hour clock will throw your starting time off 8) (need to watch for 2 and 5 instead, not 3 & 0)
This method is also nice because each digit is maintained seperately for send to a shift register or MAX7219 for display for example.
I have tracked time this way against the official US time source
http://www.time.gov/
with 1 second drift observed.

Wow, I thought that was a crystal from looking at it. I also have been tinkering with Capture Interrupts and timing a DS1307 square wave output. I consistently get readings that are farrrrrr inside the .5% spec of the resonator. I noticed it drifted a bit the first few minutes after powering it on, but settled in to give me 99998x uS long ticks from the DS1307. This is close to crystal accuracy from a resonator. Combined with the DS1307 crystal the total accuracy is still within 20ppm; a far cry from the 5000ppm they spec the resonator at.