Pages: [1]   Go Down
Author Topic: How to keep time with a 16mhz crystal?  (Read 1143 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Sweden
Offline Offline
Sr. Member
****
Karma: 13
Posts: 257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

- 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
« Last Edit: February 13, 2013, 05:13:46 am by baselsw » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Using an Arduino UNO, how do you wire a 16mhz crystal to the Arduino?
The joy of open source
« Last Edit: February 13, 2013, 05:21:38 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 1
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 13, 2013, 05:23:15 am by dmor574 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Newbie
*
Karma: 1
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2395
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look for "RTC"

Mark
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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.

Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Experience, it's what you get when you were expecting something else.

SG
Offline Offline
God Member
*****
Karma: 11
Posts: 535
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"The UNO already has a 16MHz crystal on the card."
No, it has a ceramic resonator.
http://www.digikey.com/product-detail/en/CSTCE16M0V53-R0/490-1198-1-ND/584635
Older Duemilanove's have a 16 MHz crytal (the big can next to the chip).
http://www.digikey.com/product-detail/en/ATS16B/CTX1085-ND/2640031

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:
Code:
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 smiley-cool  (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.
« Last Edit: February 13, 2013, 01:55:47 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Experience, it's what you get when you were expecting something else.

Pages: [1]   Go Up
Jump to: