MainsFrequency: Difference between revisions
mNo edit summary |
|||
Line 63: | Line 63: | ||
== Software == | == Software == | ||
The Arduino source is available on [https://github.com/bertrik/mainsfreq the github page], but has not been tested on actual hardware yet. | |||
You can build it with platformio ('pio run'). | |||
The working principle is that we count the number of cycles in a 100 second period, this should nominally be 5000. | The working principle is that we count the number of cycles in a 100 second period, this should nominally be 5000. | ||
A cycle count is done every second and the result is put in a circular buffer of 100 bins. | A cycle count is done every second and the result is put in a circular buffer of 100 bins. | ||
The average of these 100 bins then provides the frequency over the past 100 seconds. | The average of these 100 bins then provides the frequency over the past 100 seconds. | ||
The circular buffer is initialized with a value of 50 for each bin. | |||
The accuracy of the frequency count depends on the accuracy of the crystal (among other things). | The accuracy of the frequency count depends on the accuracy of the crystal (among other things). |
Revision as of 19:52, 13 May 2018
Project MainsFrequency | |
---|---|
A simple mains frequency counter | |
Status | Initializing |
Contact | bertrik |
Last Update | 2018-05-13 |
Introduction
This page is about creating a simple frequency counter for mains power and publish the frequency 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, the period is 100 seconds.
To keep the measurement circuit relatively safe, only a small part of the electronics is actually connected to mains.
Status
Received/collected most of the parts:
- 100 nF X2 capacitors.
- piece of breadboard
- connectors (we have those at the space)
- got a polyfuse from User:benadski
- optocoupler (forgot the part number)
So I basically can now wire up the circuit, connect it to a Wemos D1 and start hacking!
Hardware
The plan is to use an ESP8266 because it can easily publish the measured value over wifi/MQTT.
I'm looking at various ways of actually getting the mains signal into the microcontroller. The simplest way appears to be an optocoupler in series with R, C and a diode anti-parallel over the optocoupler's LED.
- could be a transformer, like shown here. Safe but bulky.
- could be a capacitive/resistive dropper with an optocoupler, like the circuit in this post (note: this circuit is for 120V!)
could be this thing from aliexpress
Aliexpress circuit
The circuit as reverse engineered is shown on the right. Basically, the mains is rectified through a high-value high-power resistor and a bridge rectifier, then stabilized with a zener to 5.1V, which lights the green LED and activates the optocoupler to pull the output low when mains is present. The 47k pull-up makes the signal high when there is no mains power available.
So this circuit doesn't actually do what I want: the opto is activated all the time when 230V is present, it's not flashing at 50 Hz.
Circuit
The circuit on the right is the one I plan to use.
The capacitor drops most of the voltage. It should be an x-rated type, you can tell by the marking "X2". The resistor (1k or so) limits the inrush current. The diode (1N4148 probably) protects the optocoupler (4N27 or 4N35) from reverse voltage. The resistor on the right is a pull-up resistor, possibly not even needed if I use the built-in pull-up of the microcontroller.
The X-capacitor, even though it is designed for connection to mains voltage and referred to as a "safety capacitor", has a failure mode to most likely fail as a short. This means additional components are needed to make it actually safe to use in this circuit, like a fuse in series that blows when the capacitor fails. (maybe I should just use an Y-capacitor then, which has a failure mode of most likely failing open)
At 50 Hz, the capacitor has a reactance of about 32 kOhm, so the LED current should be something like 230V/32kOhm = 7 mA, well below the maximum of 60 mA mentioned in the data sheet. Possibly, a bleeder resistor could be put parallel to the capacitor, to make sure that the capacitor doesn't stay charged when the circuit is powered off. Power consumed in the resistor is R*I*I so about 50 mW, so we can probably use the very common 250 mW type there.
Software
The Arduino source is available on the github page, but has not been tested on actual hardware yet.
You can build it with platformio ('pio run').
The working principle is that we count the number of cycles in a 100 second period, this should nominally be 5000. A cycle count is done every second and the result is put in a circular buffer of 100 bins. The average of these 100 bins then provides the frequency over the past 100 seconds. The circular buffer is initialized with a value of 50 for each bin.
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 an time reference with at most 0.01 / 50 = 200 ppm frequency deviation. This is probably doable with the built-in crystal on a typical ESP8266 board (like a Wemos D1 mini).
References
Other interesting projects/documents:
- http://www.palebluedot.nl/jml/projects/arduino/43-measure-mains-power-frequency
- https://engmousaalkaabi.blogspot.nl/2016/11/ac-220v-frequency-counter-using-arduino.html
- Interesting App Note by Microchip about capacitive droppers
- https://a01.veron.nl/download/hamnieuws/2016/Ham-06-2016.pdf (see page 20, 220nf, 2M Ohm bleeder)