Accurately Track Your Mains Frequency

Depending upon where in the world you live, AC mains frequency is either 50Hz or 60Hz, and that frequency is maintained accurately enough over time that it can be used as a time reference for a clock. Oddly it’s rarely exactly that figure though, instead it varies slightly with load on the network and the operators will adjust it to keep a constant frequency over a longer period. These small variations in frequency can easily be measured, and [jp3141] has created a circuit that does exactly that.

It’s a surprisingly straightforward device, in which a Teensy takes its power supply from a very conventional if now a little old-school mains transformer, rectifier, and regulator. A sample of the AC from the transformer passes through a low-pass filer and a clamp, and thence to the Teensy where it is fed into one of the on-board comparators from which its period is measured using one of the timers. Even then the on-board crystal isn’t considered accurate enough, so it is in turn disciplined by a 1 pulse per second (PPS) signal from a GPS receiver.

The Teensy then reports its readings over a serial line every five seconds to a Raspberry Pi, which collates and graphs the data. In case you are wondering what the effect of mains frequency variations might be, we once covered the story of how an entire continent lost six minutes.

Ask Hackaday: Is Your Clock Tied To Mains Frequency?

Earlier in March we heard about a quirk of the interconnected continental European electricity grid which caused clocks to lose about six minutes so far this year. This was due to a slight dip in the mains frequency. That dip didn’t put anything out of commission, but clocks that are designed to accumulate the total zero-crossings of the power grid frequency of 50 Hz don’t keep accurate time when that frequency is, say 49.985 Hz for an extended period of time.

An interesting set of conversations popped up from that topic. There were several claims that modern alarm clocks, and most devices connected to mains, no longer get their clock timing from mains frequency. I’ve looked into this a bit which I’ll go into below. But what we really want to know is: are your alarm clocks and other devices keeping time with the grid or with something else?

Continue reading “Ask Hackaday: Is Your Clock Tied To Mains Frequency?”

Mains Clocking A Microcontroller

[Lujji] is playing around with the STM8 microcontroller. In reviewing the official documentation for this chip, he read the external clock can be a sine wave, a triangle wave, or a square wave with a 50% duty cycle. The minimum CPU frequency is 0 Hz. [Lujji] doesn’t have a signal generator, and presumably, he’s all out of crystals. He does have mains AC, though, so why not clock a microcontroller with wall power?

Using mains power as a frequency standard is a concept a hundred years old. Synchronous motors turn at a rate proportional to the mains frequency, and this has been used in clocks for decades. If you’re really clever, you can clock digital circuits with mains AC, but we’ve never seen someone replace a tiny crystal in a microcontroller circuit with mains power.

After an experiment to prove the concept, [Lujji] went on to construct a circuit that wasn’t as dumb as connecting the microcontroller directly to a wall socket. The direct approach didn’t work that well anyway — the STM8 didn’t like low frequency clocks with slow edges. [Lujji] needed a clock with cleaner edges, and a 555 configured as a comparator fit the bill.

The completed circuit sends mains power through an optocoupler to drive a 555 configured as a comparator. The output is a clean 50Hz clock that is connected to the OSCIN pin on an STM8. This is now a chip running at 50Hz, and yes, it works. [Lujji] set up a circuit to write ‘Hello World’ on an old Nokia LCD. That took about three minutes. It works, though, even though it’s completely useless. Maybe this can be applied to some novel timekeeping similar to that one-instruction-per-day clock we looked earlier in the year.