Zero-Crossing Detector for Mains Frequency Monitoring
Jeremy Elson, June 2023; updated June 2024
Contents
Overview
Buying Hardware
Filtering
Sample Data
Use Cases<br>Time Errors in Grid-Powered Clocks
Audio and Video Forensics
A home experiment showing we’re all one grid
Schematics and Design Notes
Overview
I find it interesting to measure the frequency of the mains power<br>coming out of my wall. Though it’s nominally 60Hz in North America, it varies<br>slightly but continuously, indicating the instantaneous balance between the<br>grid’s power generation and load. These variations can be interesting to watch,<br>for reasons I describe below.
Mains power is 120 volts here, which is not to be trifled with. I wanted a way<br>to safely generate a 3.3 volt square wave that indicates the mains voltage<br>polarity. The low-voltage square wave is easy to feed into counters and<br>timestampers. I’ve seen various solutions for this on the Internet, but all of<br>them seemed a little janky or inconvenient; I wanted to solve the problem<br>properly.
My strategy is to first convert the 120VAC down to a safe 12VAC using an<br>off-the-shelf AC wall wart. I built a circuit board that takes 12VAC from a<br>5.5mm barrel jack, standard for most wall warts, and emits a 3.3VDC square wave<br>on an SMA jack, useful for plugging into counters. The 12VAC input is both used<br>to power the detector and to provide the signal that’s being measured. The<br>output always reads either ground or 3.3V, depending on the input’s polarity.
Buying Hardware
If you’d like a zero-crossing detector of your own, they’re now available for sale:
Buy On Amazon
A 5-12VAC power adapter with a 2.5mm/5.5mm barrel adapter is required but not included. You might have one in your junk drawer. If not, they’re widely available, for example:
Jameco
Amazon
Filtering
The board has two types of hysteresis to keep the output stable and glitch-free.<br>First, there’s a simple low-pass RC filter on the input (components R2 and C4 on<br>the Rev E Schematic) with a cutoff frequency at approximately the mains<br>frequency of 50/60Hz. The comparator, an<br>MCP6561, also has built-in<br>hysteresis. I’ve collected tens of millions of samples without a single glitch.
The RC filter phase-shifts the output by about 500\(\mu\)s. If you need more<br>precise phase, depopulate C4 and the output response time will be under 100ns.
An example waveform is below, along with a zoomed-in view showing the<br>transition. The input is blue; the output is yellow.
Sample Data
I hooked up my zero-crossing detector to an AC wall wart in my Seattle home to<br>measure the grid frequency of the Western<br>Interconnection.<br>The output pulses were measured by a timestamper I made using a microcontroller<br>(an<br>STM32G431)<br>attached to a GPS-disciplined<br>oscillator. The<br>timestamper writes its current clock value to its serial port every time it sees<br>a rising edge from the zero-crossing detector. These timestamps start from an<br>arbitrary epoch—the timestamper’s boot time—but have very good accuracy<br>relative to each other. The timestamper’s resolution is about 6ns, and the GPSDO<br>I use to drive its clock has a frequency accuracy of better than<br>1E-10<br>over tau=1s.
The serial data is read by a Python process running on Linux on a Raspberry Pi.<br>The Pi’s system clock is NTP-synchronized to a stratum-0 NTP server in my lab.<br>The serial data reader on the Pi gives each pulse an approximate UTC timestamp.<br>Due to USB and scheduling delays between the timestamper’s output and the RPi<br>Python process, these timestamps have an error of up to about 100ms. Therefore,<br>the frequency indicated is correct to better than 1E-10, but the UTC time at<br>which that frequency occurred could be wrong by up to 100ms.
Three days of data are summarized in the graph below. The graph shows 10-minute<br>averages, but the underlying data, available<br>here,<br>has a timestamp for every AC pulse, over 16.1 million samples. The source code<br>for generating the graphs is available in this Jupyter<br>Notebook.
Use Cases
Why measure the frequency of wall power? Three reasons motivated me: to try to<br>catch a “time error correction” event; to experiment with forensic methods for<br>timestamping audio and video recordings; and for a fun evening of<br>experimentation with another friend on the Western Interconnection.
Time Errors in Grid-Powered Clocks
For over a hundred years, the power grid had a secondary use: to distribute<br>accurate time. More precisely, the 60Hz sine wave that comes out of every power<br>outlet can be used as a stable frequency reference. Henry Ellis Warren was<br>perhaps the first to have this idea; around 1912, he designed a clock called the<br>Telechron that used the mains both to<br>power the clock and as an oscillator. The idea was that once you set the<br>clock to the right time, it would keep good time on its own without having to be<br>set again.
Warren discovered that his early prototypes told time poorly—because the grid<br>itself was not at the proper frequency! His solution was to call the...