So I finally came across an interesting topic that warrants going through the registration procedure to be able to contribute :).
Hello, everyone.
Needs a power quality analyzer that monitors 24/7 dfor weeks.
These cost 1000s$.
It can actually be DIY'ed much cheaper, as long as you are not after officially certified measurements and just want something for personal use to satisfy your curiosity and possibly decide whether there is an actual reason to contact your power company's customer support.
This is exactly what happened in my case. Initially I wanted to simply log mains voltage values over time and plot a graph, which I did, and it was quite easy to do using what could be grabbed from my computer's UPS over the RS232 connection, but its data update rate was really slow, the readings were a few volts off, and it worked only when the computer was turned on, so I decided to implement a dedicated 24/7 ADC-based data acquisition and logging system.
It required a lot of reading, learning and simulating to implement (and I'm still not sure I did everything properly, but it works). Schematics is really simple and it basically follows the typical application circuits from the datasheets.
(it's a simplified diagram: passives etc. aren't shown)
Fuctional parts are as follows:
1) Input: a divider made of 5W resistors, them being 5W to both meet the necessary voltage rating and avoid any noticeable heating that would cause an uneven temperature drift. It's not easy and possibly doesn't make much sense to obtain proper low-tempco resistors of the necessary values with proper voltage rating where I live, so I just throw in some thicker ones and call it a day.
The values shown allow to register input values up to about +/-650 V peak before hitting the max values at the ADC output.
The symmetrical arrangement of the 2x240K resistors (instead of using a single 480K) allows to avoid direct contact between any of the mains wires, be it line or neutral, and the low-voltage side (but this still doesn't provide proper isolation, which requires separate attention).
2) Input protection 1: a low-value fast-acting fuse in series and an appropriately valued varistor after the fuse across the mains input. At the varistor's value shown, the fuse is expected to blow before the ADC readings reach their maximum, which is probably not good (we lose resolution for nothing), but safe.
3) Input protection 2: an 11V rated bidirectional TVS diode across the outputs of the 240K resistors to blow the input fuse should any of the 240K resistors fail short. The 11V value seems to be the first at which the diode has a steep enough voltage vs current curve "shoulder" and draws negligible, if any, current at the working input voltage, thus not affecting the readings. At the same time, reaching 11V across the inputs will still not be enough to damage the op amp, see below.
4) A decent op amp that provides sufficient output current and slew rate. Defining "sufficient" is difficult for me, I just found what works experimentally, in my case it's a nice RRIO quad-channel AD8608 that allows to use a single 5V supply to power everything, rather than having to make dedicated power rails for the op amp. There's no strict requirement for a RR op amp, however, and something like a good old TL084 can be used, provided that it's powered in such a way that the input/output voltage swing specs are met. But then it's necessary to make sure that the ADC inputs aren't overdriven which happens automatically with a 5V-powered op amp.
Another advantage of this AD8608 is that its inputs are allowed to receive voltage higher than VCC+0.6V if the input current stays below 5 mA, which is the case, unless the input protection components (or the circuit designer) fail. It may be possible with other ICs that have integrated input clamping diodes, but in this case the 5 mA limit is explicitly specified in the datasheet.
Two channels are used to buffer the differential input signal, the other two buffer the common mode and ADC Vref references. For these I used MCP1525 and MCP1541, respectively.
5) ADC: SPI-compatible MCP3304 (MCP3302 will work as well -- whatever is cheaper/easier to get) working in fully-differential mode and providing a 13-bit output value from -4096 to +4095. MCP3202/3204/3208 will also work, but it cannot into fully-differential mode and signed output, providing lower effective resolution.
6) Galvanic isolation between the ADC and the SPI digital data receiver: ADuM2401BRWZ. A nice little IC working no questions asked at the ADC's max clock of 2MHz and beyond. Beware, the "AR[W]Z" version works up to 1MHz only, so need to get the "B". Zero hassle compared to optical isolation, which is still possible, but wastes more power and is fiddly in getting it right (which I however did successfully) in terms of choosing proper high-speed optocouplers and resistor values for them, not to mention the number of components and board space used, considering one optocoupler plus the passives per each SPI pin (4 total).
7) Digital SPI signal receiver and processor: Orange Pi Zero LTS single-board computer, which has been collecting dust in a drawer for several years. Nice little thing. Rather slow (~20ksps max) with the WiringOP library, but with DMA access to GPIO registers* it can easily reach the 2.1MHz/100ksps speed that the MCP3304 can work at, and do all the necessary math in real time.
Providing uninterruptible power for it is a separate topic. There are multiple possibilities.
(*in case anyone's interested, I published a
basic proof-of-concept implementation.)
8) (not shown on the schematics) Power supply for the op amp, ADC, voltage references and the "unsafe" side of the ADuM2401: a 5V-5V isolated DC-DC converter from AliExpress.
Nice and cheap thing, comes in a variety of input and output voltages, can be used to make dual supply rails etc., really useful to have a handful of them in the components bin. Input is powered from the OPi 5V rail, output goes to a MIC5205YM5 LDO regulator set for 5.1V output, thanks to the fact that my DC-DC converter gives about 5.2V on the output when input is 5V. It's really better to use a 5V-9V model and use a plain old jelly bean LM317 instead, but all I had at the time was the 5V in, 5V out model.
The "safe" side of the ADuM2401 is powered from the 3.3V rail of the SBC, which is the logic level of its GPIO pins.
At this point we have the original (save for the effects of the input resistors' parasitic capacitance and inductance) input waveform sampled 100000 times per second which is more than enough for the 50/60 Hz mains voltage.
Simple math calculations (but we have to implement zero-crossing detection to process complete periods, which, however, is easy enough on the software side) will yield us real-time timestamp, RMS, DC offset, frequency and crest factor values, which can be used as a measure of distortion of the input signal relative to the ideal sine wave at the measured amplitude. The raw values received from the ADC chip will need their DC offset and gain corrected, which is easily done on the software side. Calibration is done with any TRMS multimeter you trust. It only remains then to implement some graphing of the results which is usually the least pleasant part of the job.
This is likely as much as any hobbyist would ever want to have. Well, I also wanted to perform FFT on my samples to properly calculate THD in real time, but unfortunately this is where I hit my lack of knowledge and fail to understand how to use the FFT libraries to do this. So I ended up simply calculating the crest factor deviation as a measure of quality of the input voltage.
Finally, it's dirt cheap, all parts combined probably under $30 or maybe $20, which includes the Orange Pi SBC. Far below any factory-made data logger.