# AC voltage measurement with a microcontroller

I want to build a circuit that will eventually calculate power, power factor and thd for a light switch or a wall outlet. But I'm having some problems with the voltage measurement circuit. simulate this circuit – Schematic created using CircuitLab

The effective voltage is 230 +-10V.

1. Do I need to use a transformer? As far as I understand the only advantage I'm gaining from it is that it will protect me if I touch the circuit before R1 (the circuit won't be accessible anyway).
2. What is the correct equation for C1?
3. Is there something I am missing?
• Have a look at openenergymonitor.org where they've published hardware and software designs. AC power measurement is tricky and your DC approach here will not work. And yes, you do need a transformer. – Transistor Jun 20 '16 at 11:02
• 1N4148 are not Schottky diodes. – Olin Lathrop Jun 20 '16 at 11:14
• There was an answer earlier about rectifying the AC voltage and omitting the capacitor and the second divider, but was discarded because rectified sine wave wouldn't give the effective voltage value. I don't think that's a problem in this case because the instantaneous values are read by the microcontroller and half of the waveforms can corrected later in software. – Vasil Kalchev Jun 20 '16 at 13:13
• OpenenErgyMonitor seem to use a similar circuit – Vasil Kalchev Jun 20 '16 at 17:40

Do I need to use a transformer?

No you don't. You do need isolation between the power line and anything you could touch. A transformer somewhere in the chain is one way to achieve that, but not the only way.

I would probably not use a transformer. In this case it is useful to have the thing that is doing the measuring tied directly to the reference of the signal it is measuring. In your case that means tying the ground of the microcontroller to the neutral line.

Anything connected to either AC line, hot or neutral, must be considered live and dangerous. In this case it means that the micro is at unsafe voltages and must be isolated from anything you can touch when the product is properly sealed. I would probably use opto-isolators between the micro and the rest of the circuit. Digital signals can be carried across a isolation barrier much better than analog signals. By not isolating the analog signal, you get better measurements. The digital values can then be sent across isolation without corruption.

What is the correct equation for C1?

The best way to look at that is to consider the impedance of C1 relative to what it is driving. In your circuit, there are 1 kΩ resitors to 5 V and ground, so the right side of the cap is driving a 500 Ω load. The left side of the cap is driven by a 400 Ω source. That means the cap will cause attenuation by half when its impedance is 900 Ω.

Let's say you want the cap impedance to not exceed 1/100 of that at 50 Hz so that it has little effect on the mix of harmonics. The equation to get the capacitance that has a specific impedance at a specific frequency is:

C = 1 / (2 Π f R)

When f is in Hz and R in Ohms, then C is in Farads. 9 Ω at 50 Hz therefore requires 355 µF. However, that large value is due to the excessively low impedance the cap must drive. There is no need for R3 and R4 to be as low as you have them. They can easily be 10x higher.

• Thank you for the explanation about the capacitor. I like the idea about using optoisolators, I read about using a "highly linear" optoisolator or using an ADC before the uC, but this seems like the most compact and economic method. "Everything connected to AC is considered dangerous", is this because of resistive parts malfunction? – Vasil Kalchev Jun 20 '16 at 12:54

Do I need to use a transformer?

Yes you do. If you need to ask then you are not sufficiently experienced to develop the circuit that doesn't use a transformer. I'd use a transformer and I am reasonably experienced.

Other problem - you mains voltage is not developed with respect to ground - it is relative to neutral and neutral may or may not be grounded. This makes it sure that a direct galvanic connection to live wires is dangerous.

So, use a transformer and that inevitably solves the problem of C1 (which you have incorrectly drawn as an electrolytic) because you can bias the transformer secondary to half the reference voltage of your ADC and therefore measure the full swing.

If you want to measure power you need to multiply voltage and current waveforms together. If you want to "badly estimate" power you can convert each to dc and take a guess at the power factor of the load and probably be a mile out.

• The microcontroller will be measuring the instantaneous values for the current and voltage. The current circuit should be able to measure the full swing because it is offseted to half the AREF. Correct me if I am wrong but I think that the only advantage to using a transformer is that the circuit before R1 will be safer to touch. link to a question about that – Vasil Kalchev Jun 20 '16 at 12:12