The beauty of clock crystals

A project log for Kronoino : An AVR powered Digital Wrist Watch

A digital watch using the ATmega 328P and seven-segment displays

Brainy.BaboonBrainy.Baboon 09/23/2020 at 12:160 Comments

Any clock needs to extract from the imperfect, random world around it, a precise and periodic signal. There are many ways to do it. One of the most convenient ways of having a periodic phenomenon is to use an oscillating system. Unsurprisingly, most timekeeping devices, from grandfather clocks to atomic clocks use periodic oscillations as a time base.  From pendulums to springs to fancy grains of sand to atoms we humans have tried everything. One of our most successful attempts has been at harnessing, what I call, the physics of piezo-electricity. One of the applications of this magical phenomenon is in making crystal oscillators. Piezoelectric crystals, when hit, will oscillate at their natural resonant frequency, like every other object, but they also produce a voltage (or current) every time they are deformed. They can also be made to deform by applying a voltage. With some additional circuitry it is possible to take some of the electricity produced by the crystal and feed it back into the crystal( we make up for the loss by amplifying the current using a transistor). There we have it! An oscillating system that goes on forever. Steve Mould has got a really good video explaining crystal oscillators.

When using an AVR chip we have the option of using the internal Resistor-Capacitor(and op-amp) oscillator as the time base but this is not reliable over extended periods. We will be using the 32,768 Hz (or 32kHz) crystal oscillator as the time base. The choice is motivated by many reasons. For one, it is ubiquitous, from digital watches, battery-powered wall clocks to laptops, these are everywhere. Another advantage is that they are low power and do not require loading capacitors for stability.  Unlike the battery-powered clocks which use a series of flip-flops to bring down the 32,768 Hz to 1Hz, we are going to use the AVRs timers to generate an interrupt every once in a while and update the seconds counter accordingly. As the timer counts using the external clock signal coming from the crystal we have got ourselves an accurate time base. Just how accurate?

Assuming my crystal has an accuracy of 100 ppm which translates as 100/1,000,000 (hundred over a million) which is equal to 0.0001. 

As there are 86,400 seconds in a day, our error will be +/-(0.0001 x 86,400)= +/- 8.64 sec at room temperature.

 We can use the AVRs Timer2 which can use an external clock signal. We will use the Timer1 (which will use the internal R-C oscillator at 8MHz) for the delay utility.

 TIMER2 is set up in void setup() to overflow every 8 secs. 

  TCCR2A = 0x00;
  TCCR2B = 0x00; 
  TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); 
  ASSR = (1<<AS2); 

This generates an interrupt and we update the counters accordingly:-

  seconds += 8;
  minutes += seconds / 60; 
  seconds %= 60; 
  hours += minutes /60;
  minutes %= 60;