Difference between revisions of "MainsFrequency"

From RevSpace
Jump to navigation Jump to search
(Hardware)
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
   }}
 
   }}
  
 +
== Introduction ==
 +
[[File:power_dip_peak.png|800px|top|dips and peaks on the hour]]
  
== Introduction ==
+
This page is about a simple circuit for measuring the frequency of grid power, publishing it over MQTT.
This page is about a simple frequency counter for mains power publishing the frequency over MQTT.
 
  
 
It's based on the Arduino platform, using an ESP8266 to do the wifi/network/MQTT stuff.
 
It's based on the Arduino platform, using an ESP8266 to do the wifi/network/MQTT stuff.
 
 
The frequency measurement principle is to count the number of mains cycles in a fixed period.
 
The frequency measurement principle is to count the number of mains cycles in a fixed period.
To get a resolution of 0.01 Hz, the period is 100 seconds.
+
To get a resolution of 0.01 Hz, we count for approximately 5000 cycles at 100 Hz, so the period is 50 seconds.
 
 
 
To keep the measurement circuit relatively safe, only a part of the electronics is actually connected to mains
 
To keep the measurement circuit relatively safe, only a part of the electronics is actually connected to mains
 
and the low-voltage side is isolated with an optocoupler.
 
and the low-voltage side is isolated with an optocoupler.
  
 
== Status ==
 
== Status ==
 +
[[File:blaasop.jpg|thumb|right|oops]]
 
It works!
 
It works!
  
 
It blew itself up after being moved to a different casing, but was fixed!
 
It blew itself up after being moved to a different casing, but was fixed!
  
See [https://revspace.nl/grafiekjes/d/3deykmVmz/power?orgId=1 Graph of the AC main frequency as measured at RevSpace]
+
See [https://revspace.nl/grafiekjes/d/3deykmVmz/power?orgId=1 Live view of the AC main frequency as measured at RevSpace]
  
 
Compare it with:
 
Compare it with:
Line 32: Line 32:
  
 
Possible future enhancements:
 
Possible future enhancements:
* Reduce the averaging period from 100 to 50 seconds, to make the reading more dynamic without losing resolution
 
 
* Display the frequency on a nice little OLED screen (or something similar)
 
* Display the frequency on a nice little OLED screen (or something similar)
  
Line 40: Line 39:
 
The microcontroller is an ESP8266 because it can easily publish the measured value over wifi/MQTT.
 
The microcontroller is an ESP8266 because it can easily publish the measured value over wifi/MQTT.
  
The circuit to sense the 50 Hz is [https://nl.aliexpress.com/item/ding/32828199766.html this mains module] sold on Aliexpress, with a small modification.
+
The circuit to sense the frequency is [https://nl.aliexpress.com/item/ding/32828199766.html this mains module] sold on Aliexpress, with a small modification.
 
The modification is that the smoothing capacitor has been removed, resulting in a 100 Hz signal going into the optocoupler (a pulse during each zero crossing).
 
The modification is that the smoothing capacitor has been removed, resulting in a 100 Hz signal going into the optocoupler (a pulse during each zero crossing).
  
Line 56: Line 55:
  
 
An interrupt is generated for each falling edge of the signal coming out of the optocoupler. This happens during the zero crossing, so twice each mains cycle, 100 times per second.
 
An interrupt is generated for each falling edge of the signal coming out of the optocoupler. This happens during the zero crossing, so twice each mains cycle, 100 times per second.
The working principle is that we count the number of interrupts in the past 100 second period, this should nominally be 10000.
+
The working principle is that we count the number of interrupts in the past 50 second period, this should nominally be 5000.
An interrupt count is done every second and the result is put in a circular buffer of 100 bins.
+
An interrupt count is done every second and the result is put in a circular buffer of 50 bins (one for each second).
The sum of these 100 bins divided by 200 then provides the average frequency over the past 100 seconds.
+
The sum of these 50 bins divided by 100 then provides the average frequency over the past 50 seconds.
The circular buffer is initialized with the nominal value of 100 for each bin.
 
  
 
A simple filter suppresses spurious pulses: when an interrupt occurs, the counter is increased only when more than 8 ms has passed since the previous zero crossing.
 
A simple filter suppresses spurious pulses: when an interrupt occurs, the counter is increased only when more than 8 ms has passed since the previous zero crossing.

Revision as of 13:14, 26 May 2018

Project MainsFrequency
Netfrequentiemeter.jpg
A simple mains frequency counter
Status Completed
Contact bertrik, Peetz0r
Last Update 2018-05-26

Introduction

dips and peaks on the hour

This page is about a simple circuit for measuring the frequency of grid power, publishing it over MQTT.

It's based on the Arduino platform, using an ESP8266 to do the wifi/network/MQTT stuff. The frequency measurement principle is to count the number of mains cycles in a fixed period. To get a resolution of 0.01 Hz, we count for approximately 5000 cycles at 100 Hz, so the period is 50 seconds. To keep the measurement circuit relatively safe, only a part of the electronics is actually connected to mains and the low-voltage side is isolated with an optocoupler.

Status

oops

It works!

It blew itself up after being moved to a different casing, but was fixed!

See Live view of the AC main frequency as measured at RevSpace

Compare it with:

Possible future enhancements:

  • Display the frequency on a nice little OLED screen (or something similar)

Hardware

the mains sensing module from Aliexpress
reverse engineered schematic of mains sensing module

The microcontroller is an ESP8266 because it can easily publish the measured value over wifi/MQTT.

The circuit to sense the frequency is this mains module sold on Aliexpress, with a small modification. The modification is that the smoothing capacitor has been removed, resulting in a 100 Hz signal going into the optocoupler (a pulse during each zero crossing).

How things are wired:

  • Used the modified Aliexpress circuit.
  • Used a nodemcu v3 for the ESP8266 (this was what was available at the hacker space)
  • Mains power going into the mains detection module (left part of the schematic)
  • ESP8266 pin 3.3V: connected to the pull-up on the mains module output (top in schematic - green wire)
  • ESP8266 pin D5: connected to the optocoupler output on the mains module output (middle in schematic - brown wire)
  • ESP8266 pin GND: connected to the GND pin on the mains module output (bottom in schematic - yellow wire)
  • put in a plastic enclosure with warning labels

Software

The Arduino source is available on the github page.

An interrupt is generated for each falling edge of the signal coming out of the optocoupler. This happens during the zero crossing, so twice each mains cycle, 100 times per second. The working principle is that we count the number of interrupts in the past 50 second period, this should nominally be 5000. An interrupt count is done every second and the result is put in a circular buffer of 50 bins (one for each second). The sum of these 50 bins divided by 100 then provides the average frequency over the past 50 seconds.

A simple filter suppresses spurious pulses: when an interrupt occurs, the counter is increased only when more than 8 ms has passed since the previous zero crossing. Without this filter, the frequency was typically about 2 Hz too high.

The accuracy of the frequency count depends on the accuracy of the crystal (among other things). To get 0.01 Hz error at 50 Hz, we need a time reference with at most 0.01 / 50 = 200 ppm frequency deviation. This is doable with the crystal on a typical ESP8266 board (which has an accuracy of 25 ppm or so).

You can build the software with platformio ('pio run'), it uses libraries WifiManager and PubSubClient.

References

Other interesting projects/documents: