27
\$\begingroup\$

I am learning with microcontrollers (mostly PIC16/18 and AVR) for some time, making various experimental projects. One of things that I found surprisingly challenging is maintaining decent clock accuracy.

Most of my projects have internal clocks at least to display uptime, but many of them (those that have display interface) also have an adjustable clock. I even created my own alarm clock for my bed table using a PIC16, but all of them are surprisingly inaccurate, despite using crystal oscillators (using internal RC clock is absolutely terrible.)

I implemented various interactive settings to configure drift to make the clock as accurate as possible, but I am still facing drift of about 10 seconds after two months compared to an NTP calibrated clock (in my phone.)

Basically this is my observation:

  • Internal RC clock - drifts by ~10 seconds within 24 hours
  • Crystal oscillator 8 MHz - using default interrupt delays calculated by MPLAB MCC drifts by ~10 seconds within few days
  • Crystal oscillator 8 MHz - using calibrated delays per each crystal individually drifts by ~10 seconds within a month
  • Crystal oscillator 16 MHz - using calibrated delays per each crystal individually drifts by ~10 seconds within two months

What also surprised me is that each crystal, even if the same frequency and from the same manufacturer is giving slightly different frequency, because each needs different calibration to give accurate time. Is that something common?

I am using cheap crystals you can buy at Farnell, such as this one.

I was reading about this and found that there are some "oven-controlled crystal oscillators" that are necessary for accurate time that maintain temperature of the crystal, but I find it hard to believe that these small ovens are installed in electronics like wristwatches.

My calibration mechanism allows me to adjust the "second interval" to the resolution of single clock cycle of the microcontroller, yet it still isn't accurate even with a 16 MHz crystal after several months.

How do wristwatch manufacturers solve this problem?

  • Are they using oven controlled crystals?

  • Are they calibrating each single wristwatch individually as each crystal has a slightly different PPM offset / frequency than other one?

Lots of electronics come with classic 24-hour clocks, yet they don't use NTP for syncing. How is this generally done?

\$\endgroup\$
12
  • 1
    \$\begingroup\$ The most precise clock is on that does not go at all: exactly correct 2 times per day... :) \$\endgroup\$
    – Solar Mike
    Commented Feb 24, 2023 at 9:16
  • 3
    \$\begingroup\$ Just to put it into numbers, 1 second in a year is about 32 parts per billion (ppb). Standard crystals are measured in parts per million whichis on the order of 1 second per week. Oven controlled oscillators can be sub-ppb. \$\endgroup\$ Commented Feb 24, 2023 at 9:22
  • \$\begingroup\$ You'll need to use oven controlled "OCXO" ones indeed. "How do digital watch manufacturers accomplish it?" Very likely they just use a TCXO at best and so the watch will drift over time, just like an analog watch. \$\endgroup\$
    – Lundin
    Commented Feb 24, 2023 at 9:30
  • 3
    \$\begingroup\$ Try this ... if the price doesn't scare you :) ... digikey.com/en/products/detail/abracon-llc/… \$\endgroup\$
    – Rodo
    Commented Feb 24, 2023 at 18:08
  • 2
    \$\begingroup\$ Your notion of "decent clock accuracy" is a bit ambitious. I remember when Bulova introduced the breakthrough Accutron watch, guaranteed to 2 seconds per month. The only consumer tech that could beat that was a synchronous motor, controlled by the electric grid, in the places where the grid frequency was precisely controlled (not everywhere!). \$\endgroup\$
    – John Doty
    Commented Feb 26, 2023 at 14:00

10 Answers 10

32
\$\begingroup\$

Have you tried comparing with what accuracy you actually get from a digital watch? I don't remember them delivering "1 second per year" accuracy.

The crystal you linked was 50ppm. The ones designed for time use seem to be 10-20ppm, but that's only somewhat more accurate, not a spectacular difference.

Temperature and calibration do matter. The trick with watches is not that they're oven controlled - you don't have the battery for that - it's that they tend to have a metal back, by which they're thermally bonded to a temperature controlled human. That keeps the crystal in maybe a 20-30C range. Not precise but better than nothing.

There is also in some countries a radio time signal, which can give you an accurate desk clock without having to use NTP or GPS.

Edit: fortunately there are watch afficionadoes who can tell us:

https://thetruthaboutwatches.com/2021/06/watch-accuracy-rolex-vs-casio/ : "Casio claims their battery powered watches achieve +/- 15 to 20 seconds accuracy per month"

https://www.hodinkee.com/articles/citizen-caliber-0100-eco-drive-movement-introducing : ah, this is probably what you're looking for. This is where the "plus/minus 1 second per year" comes from. It looks like they have special crystals and they've designed them for very low temperature coefficient and they have software calibration. It costs $2,900.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ OK, seeing how hard it is to keep accurate time, I think I will just hook ESP8266 to this and will keep syncing the PIC via NTP over wi-fi :) it was my naivity to think that in 2023 it's easy to hand-make a circuit that has accurate clock. \$\endgroup\$
    – Petr
    Commented Feb 24, 2023 at 10:25
  • \$\begingroup\$ @petr It is easy to make circuit with accurate frequency, but to get time you must integrate frequency and there is the problem. Frequency is still the easiest thing to measure. I dont know about anything else that we can measure to below 10 ppm accuracy with some cheap sensors. \$\endgroup\$
    – Rokta
    Commented Feb 24, 2023 at 17:49
  • 1
    \$\begingroup\$ As a point of reference, one of the clocks built by John Harrison 250 years ago was recently tested and found to be accurate to within less than 0.1ppm over a 100-day period. \$\endgroup\$
    – supercat
    Commented Feb 24, 2023 at 18:42
  • 4
    \$\begingroup\$ @Petr depends on your definition of hand made, and your budget. You can get relatively large rubidium oscillators for a couple hundred bucks with 0.1ppb scale accuracy, or pay another order of magnitude to get them in a 50x50x25 mm package \$\endgroup\$
    – llama
    Commented Feb 24, 2023 at 19:23
  • \$\begingroup\$ If you want a watch that beats 1sec/year, you don't need to spend much: if you live in the right place, just get a radio-controlled one (my G-Shock was under $100 about 20 years ago, and is still going strong). Otherwise a smartwatch, though they can have really bad RTCs (I've seen tens of seconds per day) if they're not paired over bluetooth with a phone that uses NTP or GPS. And of course smartwatches have bad battery life. \$\endgroup\$
    – Chris H
    Commented Feb 24, 2023 at 21:56
12
\$\begingroup\$

The 16 MHz crystal part in your link has 30 ppm initial tolerance and 50 ppm temperature stability. It has also specified aging of 3 ppm during first year and 1 ppm per year afterwards.

To put that into perspective, you are getting 10 seconds time deviation in 2 months, that is a tolerance of less than 2 ppm!

It is surprisingly accurate, as it could be 155 seconds off just from the 30 ppm initial tolerance.

So clearly these are crystals connected to MCU, and the crystals and the MCU circuitry are good enough for running the MCU, but they are not even intended for keeping precise wall clock time.

Also there are many thigs you dont say or know, like how well the actual crystal load capacitance matches to the value specified in the data sheet, at what temperature you are running the crystal, and even the amount of power level the crystal is driven with will affect the frequency and aging.

So the ability to keep time does not only depend on the crystal itself, it also depends on the surrounding circuitry and environment where the crystal needs to work in.

Wrist watches are completely different area than MCU clocks.

First of all, the circuitry and the crystal are designed primarily for timekeeping purposes, as you would expect.

The crystals are of different type, called tuning fork crystals, and they operate at different mode of vibration and power level than "normal" AT-cut MCU crystals.

Wrist watch oscillator ICs can be designed to measure and compensate the effects of temperature, but in most cases the watch will be at room or near body temperature anyway, so in a stable environment. The capacitive load on the crystal may be fine-tuned by manufacturing circuit board with specific wiring patterns/widths and even by configuring the oscillator to enable or disable on-chip capacitances. When a watch is produced, the oscillation may be further calibrated while measuring the oscillation frequency with a precise enough.

The calibration may also happen digitally, as you can easily just write a look-up table to map a range of values how much to adjust the rate of output pulse frequency based on say initial tolerance, varying temperature or assumed aging.

Having said that, these are mechanisms that may already be present in the MCU real-time clock peripheral, which are intended for timekeeping with a watch crystal, instead of the main crystal.

\$\endgroup\$
8
  • \$\begingroup\$ For my bed table alarm clock the temperature is very stable, it's around 20 - 22C, I actually happen to have a thermometer (DHT22) connected in that same circuit to same MCU. Maybe that's why it's so accurate. I didn't know that crystalls are suppose to be this inaccurate, I was surprised because there are many home appliances that have clock (even fridge, or oven) and I wouldn't expect those circuit to be so overly complex in order to keep correct time, I always thought keeping digital clock accurate is somewhat trivial with modern technology. \$\endgroup\$
    – Petr
    Commented Feb 24, 2023 at 10:21
  • \$\begingroup\$ They have a real time clock chip in them and well designed crystal circuit. You can buy RTC modules with built in crystal and thermal compensation too. \$\endgroup\$
    – Justme
    Commented Feb 24, 2023 at 10:35
  • 7
    \$\begingroup\$ Mains clocks can also use the mains for timekeeping: there's a precise number of cycles per day. \$\endgroup\$
    – pjc50
    Commented Feb 24, 2023 at 10:55
  • 3
    \$\begingroup\$ @pjc50 ... except when Serbia steals electricity for Kosovo and sets the entire European grid back 6 minutes! :) \$\endgroup\$ Commented Feb 24, 2023 at 20:26
  • 3
    \$\begingroup\$ Crystals are probably the cheapest component per ppm -- 32.768khz crystal with few tens of ppm costs 10c, the same accuracy in a resistor is 10000 to 100000 times more expensive... \$\endgroup\$
    – bobflux
    Commented Feb 25, 2023 at 21:26
9
\$\begingroup\$

Regular digital watches don't tend to maintain time to that sort of accuracy. A few minutes a year of drift is reasonable. A lot of embedded clocks don't display the seconds, so drift is less noticeable.

You can maintain accuracy with an external timing source. There are a few classic ways:

The MSF signal

A radio transmitter is Cumbria is constantly broadcasting an accurate time signal. It uses a very low frequency carrier that covers a lot of Western Europe. You used a UK subdomain for the farnell link, which suggests you're in range. It's a very simple on-off keyed broadcast of only sixty bits at one bit per second. I think you could build a receiver without too much trouble. If you didn't want to decode it, you could also synchronise to the minute marker of each broadcast to correct drift.

Electric time

This method is used by mains powered clocks (rare nowadays) and those mechanical plug in timers. Usually it involves a tiny synchronous motor that spins at the grid frequency. Although the grid frequency isn't that precise, in the UK, the National Grid maintains an accurate average frequency, so while it can drift it always corrects.

\$\endgroup\$
9
\$\begingroup\$

There are approximately 31 million seconds in a year, so you want better than 0.03 ppm accuracy. Some temperature-compensated crystal oscillators can manage that.

Temperature controlled oscillators get even better accuracy, but temperature control uses too much electricity for a wristwatch. Temperature compensation works by measuring the temperature and then guessing how much that will perturb the clock rate and compensate the time reading accordingly.

"Smart" watches use the Internet or GSM time, so they don't need precision oscillators.

Radio-controlled watches use radio signals to re-synchronise them.

Atomic watches are radio-controlled watches with misleading marketing.

Stand-alone digital watches generally aren't that accurate, and normal consumer grade watches are more like 10 parts per million.

I own a kitset digital clock that's based around a DS32315N TCXO/RTC IC. It seems to have drifted about 1.5 minutes over the last three years.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ "31 million seconds in a year" --> How about π*10^7? - Correct to 0.5% \$\endgroup\$ Commented Feb 25, 2023 at 3:36
  • \$\begingroup\$ it's closer to sqrt(10^15) \$\endgroup\$ Commented Feb 25, 2023 at 3:52
  • 1
    \$\begingroup\$ @chux-ReinstateMonica exactly. I like it written as "pi seconds are a nanocentury" \$\endgroup\$ Commented Feb 25, 2023 at 8:27
6
\$\begingroup\$

Wow…the fact that you ask such a question is symptomatic of how ubiquitous really accurate time standards have become. The only reason you are able to know how “inaccurate” your circuit is because you are able to compare it to sources which (eventually) rely on huge and expensive atomic clocks. (As others have stated, these are becoming almost affordable!)

ALL electronic components are subject to manufacturing tolerances and drift with temperature, age, and operating conditions. Yes, manufacturing processes and materials have improved, but to look at the calendar and say “It’s 2023, where are the near-zero tolerance parts?” Is to ignore reality.

A crystal is basically a xylophone bar with electrodes on it. The frequency is literally dependent on its mechanical dimensions, the mass loading of the plated contacts, and to a small degree the reactance of the circuit where it lives.

OK, so ends my lecture, on to my version of an answer:

I built a sub-ppm microcontroller (PIC) based reference oscillator for my frequency counter. I used a cheap 10 MHz crystal, trimmed with a mechanical trim cap and several variactors in parallel. I did not use the microcontroller’s oscillator circuit, but a JFET based circuit driving the crystal with minimal signal to reduce aging drift. The entire circuit is wrapped in a layer of thin packing foam.

The microcontroller monitors temperature with a thermistor and drives the variactors according to calibration data stored in eeprom. The microcontroller drives a 7 bit R-2R ladder DAC with the LSB pwm’d for 14bit effective resolution. The oscillator and DAC used thier own regulated supply, also within the insulated container.

The point of the insulation isn’t to maintain constant temperature, but to keep everything at equal temperature and slow down temperature changes so the calibration data can compensate the net effect of the many error sources.

I was fortunate that my counter needed a 10 MHz reference clock, as this facilitated calibration:

Calibration was done by zero-beating the oscillator against the WWV carrier signal. By watching the S-meter on the receiver, it is possible to get this down to less than 1/2 Hz error, which is 1/20 ppm at 10 MHz. Propagation between Ft.Collins and my home modulates the WWV signal on a slightly longer scale, so that is as about as good as I can do. In practice, the frequency varies by up to almost 2 Hz with temperature variations, so I call it good to 1/2 ppm. I check it every year or two, but haven’t felt the need to re-calibrate in the decade plus since I made the thing.

This is the level of trouble I went to to achieve the equivalent of a clock accurate to about 15 seconds per year. You are somehow expecting over an order of magnitude more precision, just because it’s 2023? Good luck with that!

For a clock, rather than a reference oscillator, you don’t need to self-adjust the actual crystal frequency. Just measure the temperature and apply time correction factors in software per your calibration data. Of course, to obtain that data, you will pretty much need to do something at least similar to my approach, as each data point would require months for the sort of accuracy you seek unless you have a reliable way to check the crystal frequency. You can use a frequency counter of dubious calibration to obtain your temperature calibration, then apply one overall calibration factor based on observed time drift to correct for the error introduced by the counter. Oh yes: make sure your counter doesn’t load the oscillator circuit.,

\$\endgroup\$
5
\$\begingroup\$

The first thing you need to be aware are crystal tolerances, which are usually in the PPM range. The lower the PPM is, the less random drift your time will have. Note that this is a relative number, so having a higher clock won't really help if your crystal has a big drift.

The second problem is thermal dependency. Crystals are tuned forks. If the temperature changes, the frequency may also change. Sometimes this can be compensated in software, so a temperature sensor might help you with that. However, beware of self-heating if you want to use an internal temperature sensor in a MCU: if your usage is not ultra-low-power, self-heating might become a problem. Thankfully most watches are ULP due to battery constraints, but desktop clocks might not be.

Third, some MCUs have an internal register to adjust the tuning of the crystal oscillator. Most typically, a simpler MCU like the AVR only has a register for the calibration of the internal RC oscillator. However, some more advanced ones, like the STM32 can have both an oscillator for running programs and another for timekeeping like an RTC (real-time clock). This way it is possible to have a cheaper, higher frequency clock for most things, while keeping time with a more precise crystal. Sometimes it is also possible to use the internal oscillator to calibrate the RTC crystal, as described in ST's Application Note 2604.

Fourth, joining everything: it is absolutely critical that you don't keep time in software-only. You have to use a hardware timer with an interrupt that doesn't mess with the timer value, or you will most likely have jitter problems. If you want enough precision, you may have to actually calibrate the RTC clock for multiple temperatures, keep the values in a table and adjust the CAL[6:0] bits and the prescaler (if using the STM32 or similar).

Some more technical details may be found in the aforementioned AN2604.

\$\endgroup\$
4
\$\begingroup\$

All great answers above, but also want to point out that there are some amazing new digital TXCO chips available now including the RV-3032 that keeps +/-1.5ppm at normal indoor temps using only 160nA(!).

https://www.microcrystal.com/en/products/real-time-clock-rtc-modules/rv-3032-c7/

Not 1 second per year, but well below 1 minute per year and very, very low power and simple to use with a microcontroller.

\$\endgroup\$
3
\$\begingroup\$

For $2k-$3k you can get a chip-scale atomic clock ("CSAC"). That's about the only "plug and forget" way of getting the stability you look for. Unless, that is, you want to learn everything there is to know about making your own crystal resonators, wrapping oscillators around them, characterizing their performance, and finally using mathematical models to calibrate out frequency drift. It's more fun to play with crystals if you got the time for it and are willing to set up a lab where experiments can be made (annealing, tempco tests, long-term stability tests, etc.).

I haven't tried it, but perhaps using an ensemble of resonators, with widely varying tempcos, could allow for cross-compensation, with suitably advanced modeling techniques. I'm sure I'd like to know, but I don't hold my breath and probably would not attempt it other than out of curiosity.

\$\endgroup\$
2
\$\begingroup\$

When possible use a GPS time synchronization. Most units can output a very accurate pulse every second. Use software to adjust the division setting. You add a bit of short time accuracy by investing in a oven controlled crystal oscillator.

\$\endgroup\$
1
\$\begingroup\$

How do wristwatch manufacturers solve this problem?

They rely on the wrist of a live human (which is at 37 °C) to keep the crystal at a steady temperature, though slightly cooler.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That only solves the temperature stability, not initial tolerance. \$\endgroup\$
    – Justme
    Commented Mar 1, 2023 at 0:32
  • \$\begingroup\$ You answered the first part (Do they use an oven? Yes, the human arm). Answer to the second part (Are they calibrating each single wristwatch individually) is? \$\endgroup\$ Commented Mar 1, 2023 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.