Finished: accurate powerline frequency meter


Senior Member
A few weeks back, I opened up the following thread, asking how to measure powerline frequencies with a resolution of two decimals.

This may seem fairly straightforward, and there are many ways to approach this problem. So in this initial post, I’m only focusing on the hardware. I’ll write a second post with the software later.
But first things first: to obtain a reliable counting period regardless of the actual software or hardware approach, the powerline’s zero crossings, which are the basis for any accurate counting scheme, must be solidly and accurately detected.
An added complication is that full line isolation is a must.

Traditionally, small powerline stepdown transformers have been used. It is actually an excellent choice, save for the fact that they are bulky and heavy, and with the price increase of the raw materials, a significant cost adder. Therefore, an optocoupler is a desirable solution.
But my first attempt failed miserably. There is a very large amount of noise at the zero crossings, which would cause random multiple pulsing.
The solution to ameliorate this issue is to use an optocoupler with a high CTR of at least 100, and an external base connection. The first characteristic is to reduce power dissipation in the primary side resistor, the second such that the gain can be tailored with an external network. The 4N35 nicely fits the bill.

In this particular instance R5 reduces the overall low frequency gain, while C1 forms a low pass filter around 700 Hz. The values were found empirically, start with these values but feel free to do a little experimenting yourself.
The output waveform will however, be significantly rounded off. Therefore, a schmitt trigger gate must be used prior to further processing. The resulting 120 Hz pulses are fed to a 74HC393 which divides it down and averages it by twenty, further removing any stray pulses. The resulting 83.3333 ms positive pulse (1/6 Hz = 166.666 ms, of which the positive pulse period is half that) will be counted by the 20X2 via the PULSIN command. All of this provides a total pulse jitter of only 8 us within 166.66 ms.

Which brings us to the main timebase. This was the main item I had to experiment to get right. For optimum frequency accuracy and stability, it must be an external resonator, which is internally multiplied by 4.
I had already evaluated that for maximum resolution, the pulse width being measured had to be as close as possible to the PULSIN timeout. In plain English slightly longer than 83.333ms. I’ll spare you all my trial and calculations, but it turns out that a 5 Mhz external source will provide a 131 ms timeout and a very convenient 2 us timing period. And I already had a high quality OCXO with that frequency.

For 60.00 Hz, that means PULSIN’s wordvariable will contain 41,667 counts. Other frequencies will vary with the reciprocal.

Since I only have left less than 48ms to perform all other tasks after PULSIN times out, I could not waste time waiting for the 83.3333 ms positive pulse to appear randomly. Rather I had to synchronize the signal with a pre-trigger a few milliseconds prior to the pulse’s leading edge. I achieved that by an AND combination of signals creating an interrupt. After all, the counter signals are already there and the 28X2 had plenty of unused pins.

The resulting 6 counts per second are further averaged and accumulated 180 times, for a total integrating period of 1/2 minute. This provides a very solid and accurate count which is saved on EEPROM.
Whenever the desired monitoring period is reached and the counting stops, the EEPROM’s values are downloaded in 15 minute blocks. Then utilizing the capabilities of an Excell spreadsheet, those values can be further accumulated to be plotted as a 15-minute block.

Why 15 minutes? Mains power delivery parameters (power factor, peak demand, etc) are continuously fluctuating and therefore utilities, at least in North America, use 15 minutes as control period.
The remaining circuitry is straightforward. A 24LC256 EEPROM, and an AXE033 serial display.

I’m attaching a 24-hour plot of the resulting project. It is in elapsed minutes from the start of the log, I may add a RTC later to have the proper timestamp.