[time-nuts] Recording mains frequency/phase [WAS: NoGPSsatellites]

Tom Van Baak tvb at LeapSecond.com
Mon Mar 2 04:57:58 EST 2015

> I'm going to have to build one of these.  Assume you have some sort of 
> circuit that converts low-voltage AC from a transformer secondary to a 
> pulse train, start a timer, and count x amount of pulses?

Hi Ben,

Any microcontroller will allow you to poll for or capture events. Many even have capture/timer capability in h/w. Using a continuously running multi-byte timer you just subtract the current time from the previous time to get time interval (period). The traditional method of starting or resetting a timer after each event is prone to accumulated timing errors. Making periodic snapshots of a continuous timer avoids this.

Note that timer wrap-around is transparent for binary counters, as long as your timer won't wrap twice between events. For example, a 16-bit 1 MHz timer is more than sufficient for measuring 60 Hz events (since 16667 < 65536) with 1 us resolution.

In pseudo-code:

    time_now = get_timer()
    interval = time_now - time_then
    time_then = time_now

Now, there are subtle issues with how interrupts and timers work, depending on the microcontroller, but the basic idea of measuring the precise interval between moderately rapid events (like 50/60 Hz cycles) is simple.


More information about the time-nuts mailing list