# How to measure AC line frequency from mains

## Main Question or Discussion Point

Hi all,

I would like to ask for suggestions on how to measure the frequency from my main supply which is 240V? As i know the frequency varies very little, hence i would like to measure it as accurate as possible.

The reason for this is because I'm doing a smart meter project with Arduino Mega 2560. I've researched and I saw people using LM2917 or comparator or AN795. However, I am not so sure on how to connect them from the main supply. As i know, I need to step down the voltage from 240V to 6V. I have no ideas after that.

Suggestion are much appreciated.
Thanks!

Related Electrical Engineering News on Phys.org
mfb
Mentor
Why do you need such a precise measurement?
A transformer can give you low voltage with the same frequency, add something to digitize the voltage and compare its frequency with clock cycles of your microcontroller.

meBigGuy
Gold Member
You seem to be having difficulty with getting pulses from the 6V AC. Look up zero crossing detector on google.

Once you have pulses you can do any of several things, depending on the accuracy you need. You can count pulses for a fixed period and do division, or you can use the TC9400 and an A/D converter as explained in AN795.

The simplest is to count pulses, and just make the period as long as you need for the accuracy you want. But, sometimes you want faster results and there are tricky ways to do that. But, more accuracy always requires more time for any given method.

If you decide to use the TC9400 I think you just need a voltage divider (check the data sheet)

Last edited:
AlephZero
Homework Helper
Depending on your country, you may be able to get the mains frequency measurements in real time, more accurately than you can easily measure them yourself. Note that the frequency is adjusted so its long term average is "exactly" 50 or 60 Hz depending on your country, and at any time the cumulative deviation from the average is unlikely to be more than a few seconds either way.

See http://en.wikipedia.org/wiki/Utility_frequency#Long-term_stability_and_clock_synchronization

For example http://www.mainsfrequency.com/ shows the real time data for Europe.

If i convert the AC signal to a square wave, then i can use Arduino to capture the rising pulse in order to count the frequency. Is this way correct and accurate? I have run the simulation and the result is as attached. Please correct me if I'm wrong.

#### Attachments

• 20.5 KB Views: 751
• 45.6 KB Views: 703
mfb
Mentor
That is certainly possible. I don't know the resolution you can get, but the edges look good.

meBigGuy
Gold Member
The 741 is not really a good choice to use as a comparator since the voltage out goes from -V to +V. You will find that digital circuits downstream do not want to see voltages lower than ground. (typically Vss - 0.3).

Use a comparator which, with a pullup usually, will give a voltage between 0 and +V (or whatever is on the pullup).

Accuracy will depend on how long you count. Count for any abitrary 1 second and you get 60 +- 1. Count for 1 minute and you get 3600 +- 1.

mfb
Mentor
The 741 is not really a good choice to use as a comparator since the voltage out goes from -V to +V. You will find that digital circuits downstream do not want to see voltages lower than ground. (typically Vss - 0.3).
Add a resistor and a diode...

Accuracy will depend on how long you count. Count for any abitrary 1 second and you get 60 +- 1. Count for 1 minute and you get 3600 +- 1.
If you measure the time of the edges, you can get a better resolution.

meBigGuy
Gold Member
mfb is right in both cases. Technically the diode is out of spec at -0.7 volts or so, but it generally isn't an issue.

The 741 will have very slow edges compared to a comparator, but that might not be an issue for you. Could result in some jitter.

mfb is right in both cases. Technically the diode is out of spec at -0.7 volts or so, but it generally isn't an issue.

The 741 will have very slow edges compared to a comparator, but that might not be an issue for you. Could result in some jitter.
Will it help more if i use a LM193 Comparator?

I have corrected my schematic where i put a ground on V- instead of -5V. With this, there will not be anymore negative voltage. Does this help to avoid my Arduino from being damaged?

vk6kro
It is possible to measure the mains frequency and get very accurate results, but it may take more than an Arduino to do it.

You would divide the mains frequency by 100 to give a 0.5 Hz symmetrical (50% duty cycle) square wave. This gives an approximately 1 second pulse which can be used to control a logic gate.

After that, you can count the number of pulses of a known frequency input (say 1 MHz ) are passed through by the logic gate.

Suppose you got a reading of 997654 pulses. This would mean the gating pulse was actually 0.997654 seconds and the mains period was 50 times this.
So the frequency would be 50.11757 Hz.

Depending on the speed of the Arduino, you may be able to apply this technique at a lower frequency and still get good results.

meBigGuy
Gold Member
See http://letsmakerobots.com/node/28278 for how to use the Arduino timers. This is definitely the correct approach.

The arduino runs at 16MHz and has an input capture register that can record a timestamp for an external event and notify you to go read it. You can measure for as long as you want and then average, or produce a running average, or whatever.

Timer Input Capture:
When a timer input capture interrupt occurs, the input capture flag bit ICFx will be set in the interrupt flag register TIFRx. When the input capture interrupt enable bit ICIEx in the interrupt mask register TIMSKx is set, the timer input capture interrupt service routine ISR(TIMERx_CAPT_vect) will be called.

Put a blocking diode between the output and R4.