Real Time Clock -- Keeping Time with a Microcontroller

Often a micro controller project needs to know what time ( perhaps including date ) it is, say for a data logging project, or even more directly a clock. This article deals with methods of keeping time.

Real Time Clock Modules

This is a module includes a highly precise oscillator ( often based on a clock crystal ), suitable counters to count the oscillations, and an interface for the micro controller. There is a good overview at Wikipedia http://en.wikipedia.org/wiki/Real-time_clock and an easily available one can be found at Sparkfun https://www.sparkfun.com/products/99 Often these devices are very low power making them practical for battery powered projects, or for mains powered projects with battery powered backup. Real time clocks information is easy to find on the web, this article will largely focus on other methods.

Just Use the Micro controller Clock

Micro controllers all have clocks for timing their own processing. You can use software and the counter timers ( on most chips ) to determine the time. You will also have to write software to manage the clock. Accuracy of the oscillator is important here, and generally crystals are the most accurate. A .01 percent accurate crystal will be off by about 10 seconds a day. This may be accurate enough or not. With more complex software you can add correction routines to the clock and increase the accuracy of the clock pretty much as much as you want, until you are limited by the stability of oscillator, one again a .01 percent stability is about 10 seconds a day. Probably the most important factor for stability is temperature. So if you temperature is more or less constant stability may be good. Aging of the crystal can also effect the stability. You can improve this accuracy by feeding the temperature to an error correction routine, or you can use a temperature controlled “oven” to improve the stability. ( Some of these techniques can also be used with real time clock modules or may be built into them ).

Use the Power Line

The power line ( mains power ) runs at 60 Hz and over the long term is very stable. If you are using a plug in power supply then this is an easy option.

Overview: This assumes that you are using a transformer based power supply. This will have a low voltage AC output. Some of this you will rectify, filter, and probably regulate. Use a second tap on the transform and a diode for half wave rectification. You do not filter this, instead put a resistor in series with it and a diode to the regulated micro controller power. This will give you a 60 Hz wave clipped at 0 volts and the micro controller voltage. This signal is safe to feed into a micro controller input pin.

There are several ways to process this signal, here is one:

Set the input pin to input and feed it to a counter. When the counter gets to a count of 60 ( some counters count up, some down, do what is appropriate for you processor ). it should trigger an interrupt and and count the second.