Monday, May 16, 2016

Designing a Mains Frequency Monitor, Part 1

This is the first in a series of posts about designing a mains frequency monitor using the Microchip PIC 16F1619 microcontroller. Today we will talk about the features of this device that make it a good choice for such a project. We will also start to piece together our circuit, program, and test a prototype on the breadboard.

Prototype of a mains frequency monitor using the PIC 16F1619.


Many people assume that the mains frequency is exactly 50/60Hz, but in fact that is rarely true. Accuracy and stability of the mains frequency is an interesting topic to research. No two points on the grid will have exactly the same characteristics, so collecting data at your location can reveal some interesting findings. This is especially true if you have (or are interested in building) a mains-referenced clock. There are many industrial applications as well for such a project.

Numerous circuits and methods have been published for monitoring the mains frequency. Recently, I came across the PIC 16F1619 microcontroller and thought it would be a great device to design my own solution around. It is a very inexpensive chip that comes in a DIP package, making it breadboard-friendly for prototyping. This PIC has an interesting set of features including a Zero Crossing Detector (ZCD), a Signal Measurement Timer (SMT), and an Angular Timer. It also includes several 16-bit timers, configurable logic cells, a PID math accelerator, a hardware USART, and lots more. Check out the datasheet for this device to learn more.

Pinout of the 16F1619 in a 20-pin PDIP/SOIC/SSOP package.

Zero Crossing Detector

The first peripheral we will examine in more detail is the ZCD. It allows the detection of both the positive- and negative-going zero crossings of the input signal. Additionally, the output of the ZCD can be used as an input to another peripheral such as the SMT for gating and windowing. Externally, it requires only a single series resistor to limit the current into the module. Additional pull-up and pull-down resistors can be added to correct the offset on the comparator reference voltage or intentionally set an offset from true 0V. Check out this Technical Brief to learn more about the capabilities and setup of the module.

Block diagram of the ZCD module.

Scope capture of an AC input signal (blue) and ZCD output signal (yellow).
The output is also accessible in an internal register for triggering other peripherals.

Signal Measurement Timer

The SMT is a flexible 24-bit timer with many options for clocking, windowing, and gating. Check out the Technical Brief to learn more about it. We can use it to measure the relatively long cycles of the mains signal at full system clock speed and still not overrun the timer. Combined with the ZCD it gives us a means for measuring the mains frequency to a suitable level of precision. With the core functionality needed for our project handled by integrated peripherals, we save our code space for processing the data and feeding it out to another device for monitoring and logging.

Block diagram of the SMT module.

Angular Timer

Another really cool peripheral in the 16F1619 is the Angular Timer. It locks onto the input signal and allows the instantaneous phase angle to be determined at any point during the cycle. It can also be used for missed pulse detection. This peripheral relies on several registers and configuration bits, but once set up it is easy to use. You simply read out the value of the phase clock during a cycle to calculate the angle. This peripheral has many potential uses for mains applications and motor control. Microchip has a webpage on it with more information, documentation, and a video.

Designing the Circuit and Firmware

With the above three peripherals in mind, I started working on my circuit design for the project and building a prototype on the breadboard. I added a 20MHz TCXO to generate a stable, accurate clock for the microcontroller and timers. For the mains input, I used a 12V AC wall adapter. This goes into a half-wave rectifier and then a voltage regulator to generate VDD. The mains signal to be measured is tapped off before the rectifier, bringing it into the ZCD with a common ground reference. I might explore upgrading to a center-tapped full-wave rectifier in the future.

Below is a partial schematic for the circuit thus far.

The beginnings of a mains frequency monitor circuit.

The firmware came together quickly in MPLAB X IDE. I set up the various peripherals and put the SMT into Windowed Measure mode. The window is triggered by the internal ZCD output bit. When a positive-going zero crossing is detected, the SMT beginning counting at the full 20MHz system clock speed. At the end of the cycle the count is latched into a register and then stored. Once measurements for 60 cycles are accumulated, the average frequency during the second is calculated and printed via USART to an LCD display.

I didn't actually need the Angular Timer in my project, but it's such a cool peripheral I wanted to find some way to include it. I decided to use it to verify that the negative-going zero crossing occurs at the correct phase angle. That angle is of course 180 degrees (assuming a symmetric waveform). The ZCD generates an interrupt at the zero crossing and the program checks the count in the phase timer. If the phase angle is not correct, an error flag is set. Currently I just use this to set a status LED, but there is a lot more that could be done with it. In the future I might use it to detect erroneous measurements and automatically remove them before they get ingested into my data.

This hardware and firmware setup also gives us the basis for a mains-referenced clock, where the time is incremented every 60 cycles. That doesn't actually provide very good timekeeping (over short intervals) due to the instability/innacuracy of the mains frequency. For now it's an interesting addition to the project though. We will revisit this later and implement a much better solution for our clock.

The prototype on the breadboard works well, and you can see a picture of it at the top of this article. My initial testing against proper frequency counters shows that the design so far meets my expectations.

Lots More To Do

The project is coming along well, but we have a lot more to do before it is done. In Part 2, we will finish the firmware, finalize the schematic, and design a PCB for it. We will also need to do a lot of testing to verify the performance of the counter.

I have uploaded the program in its current state to a new Github repo. Please check it out if you are interested in seeing what the code looks like. This repo will also house the PCB design files and other documentation in the future.

If you have feedback and suggestions on this project, please post them in the comments below. A post on Part 2 of the project will be coming in a couple of weeks.

Thanks for reading!

- Dan W.


  1. Thanks for another interesting project, Dan. I had already stocked a few PIC16F1619 in the hopes of doing this very application. I knew I would eventually out grow my PICkit 2 programmer ;-) Looking forward to Part 2.

  2. Consistency and persistency are the key factors to prove you in the field. This blog actually give us an idea of how to be a consistent and persistent in the field. DigitEMB