How to measure Variations in Mains Frequency

With precious little surplus PV around at the moment, why not put your rig to an alternative use?  With nothing more than an Arduino & voltage sensor to hand, variations in mains frequency can very easily be recorded.

The attached sketch is based on my Mk2i Pv Router code.  The ADC operates in fixed timer mode with one voltage sample being taken every 1000uS.  That's 20 samples per mains cycle, or a thousand per second.  Or 15000 per 15-seconds, which is what the sketch records. 

Along with a timestamp, this number is sent to the Serial monitor at fixed intervals.  Comparable data from National Grid is updated every 15s so the output period at Line 180 has been set to this period (750 mains cycles).  The timestamp is updated independently ever minute.

Data is dispatched piecemeal to avoid any disturbance to the sampling process.  If the ADC's operation is ever compromised, a '$' character is sent to the Serial output.  No such characters have been seen during my first 24 hours of recording.

Having got some data, this can be conveniently displayed in a spreadsheet.  Copy and paste it into a .txt file and then import this data into Excel or similar.  Unfortunately, I've yet to get Excel to interpret the second column's data as "Time"; I therefore create an extra column (including seconds) for this purpose.  Another extra column displays the 'Y' values data in a normalised and inverted form.  This format can then be compared directly with published National data.

Frequency data for the last 60 mins can be screen-grabbed from http://www.nationalgrid.com/uk/Electricity/Data/Realtime/Frequency/Freq6... Equivalent local data can be displayed by adjusting the min and max limits of the graph's X-axis.  Time is treated as the decimal part of each 24h day, so 6am is 0.25.

This sketch is not intended to provide an absolute measurement of mains frequency, but it does seem to track variations pretty well.  It will be interesting to see whether results from a basic Arduino are any different than for an emonTx or alternative hardware platforms.

Warning: Allowing the laptop to hibernate is not good for one's data!

 

AttachmentSize
MainsFreqChecker.ino_.zip3.08 KB
MainsFreqResults.xls97 KB
localVar_3pm_18Jan.jpg30.23 KB
natVar_3pm_18Jan.png32.71 KB
PeterN's picture

Re: How to measure Variations in Mains Frequency

Great stuff Robin thanks for sharing. Tried your MainsFreqChecker sketch and it works great. Hint for other novices like me you need to use "Both NL &CR" choice in Arduino output COM screen (bottom right corner beside baud rate) to interact with sketch).

I would really like to measure Irish Grid Frequency and publish to a web site. I can usually manage to merge scripts ok e.g. adding temperature sketch to standard emontx sketch etc. However I tried merging this one with emontx and it's not as straight forward.  Anyone try this with rf (emonlib) ot emontx.

My aim is similar web page as Dynamic Demand. I also tried the following code at  http://jeelabs.org/2009/05/28/measuring-the-ac-line-frequency/ - only picture so I typed out compiles ok but does nothing ( could have missed something).

 

Cheers

Peter

volatile uint8_t count;
uint8_t first =1;
long lastTime;

ISR(ANALOG_COMP_vect) {
    ++count;
}

void setup() {
  Serial.begin (9600);
  Serial.println("\n[hertz]");
 
  ACSR = _BV(ACBG) | _BV(ACI) | _BV(ACIE);
  ADCSRA &= ~ bit(ADEN);
  ADCSRB |= bit(ACME);
  ADMUX =0;
}
void loop() {
cli();
if (count >= 100) {
long now = micros();
count -= 100;
sei();
if (lastTime !=0){
  long millihz =long(50e9 / (now -lastTime));
    Serial.print(millihz/1000);
    Serial.print('.');
    Serial.print((millihz/100) % 10, DEC);
    Serial.print((millihz/10) % 10, DEC);
    Serial.print((millihz) % 10, DEC);
  Serial.print(" Hz ");
if (millihz >= 50000)
    Serial.print('+');
Serial.print ((millihz - 50000) *  0.002);
Serial.println(" %");
}
lastTime = now;
}else
sei();
}
 

 

MartinR's picture

Re: How to measure Variations in Mains Frequency

My phase-locked loop sketch will do that for you. You just need to add the variable "frequency" to the TX payload as Brian D has done.

PeterN's picture

Re: How to measure Variations in Mains Frequency

Thanks Martin, have your sketch running and looks good. Will try send to Pi.

Cheers

Peter

calypso_rae's picture

Re: How to measure Variations in Mains Frequency

I'd forgotten all about this one!

chaveiro's picture

Re: How to measure Variations in Mains Frequency

See my post about EmonLibPro http://openenergymonitor.org/emon/node/2406

It does what you want and more.