Reading AC Voltages with a Microcontroller

This article shows an op-amp based circuit to transform a high-voltage AC waveform into something that a micro-controller can measure directly.

DISCLAIMER: This article is provided as-is, with no guarantee that the circuit will work in your situation, or that it will be safe. If you’re going to attempt to measure mains voltages, do so at your own risk – if you don’t understand the math or circuit diagram, then you probably shouldn’t be working with mains anyway. I am not responsible for your mistakes. You have been warned.

Background

A few years ago, I was involved in a project that needed to measure mains voltages in New Zealand, which has a 230 V, 50 Hz supply. My circuit needed to be as cheap as possible, and didn’t need to be electrically isolated from the supply. I needed to measure the voltage using a microcontroller that had an input range of 0 to 5 volts, and needed to measure voltages from -375 V to 375 V (or 265 VRMS).

To convert this large, alternating voltage range to something compatible with the micro, I’ve seen several approaches on the web. Most do something along the lines of rectifying the AC voltage before feeding it into the micro. While that may be fine for some, it creates ambiguity between positive and negative voltages. I needed something better. It would be great to have a circuit that multiplies the input voltage by a constant, then adds half the supply voltage of the micro. It turns out you can do that with a single op-amp, and three resistors (five if you don’t have a handy half-supply reference). Here’s how:

The Circuit

If you’ve used op-amps before, this should look reasonably familiar – it is pretty much the standard inverting amplifier. However, the key differences are that the non-inverting input is tied to half of Vcc instead of ground (so if you have a 5 V microcontroller, tie this to 2.5 V), and the extra resistor pulling the inverting input up to Vcc – this resistor should be exactly the same value as the other resistor called Ri.

The operation that this circuit performs is:

This is precisely what we want. Say we would like to measure mains voltage in New Zealand (remembering the disclaimer above), with a range of -375 V to 375 V, and the microcontroller can accept voltages from 0 V to 5 V. By choosing Ri to be 150 kΩ and Rf to be 1 kΩ, the gain from the formula is 1/150. IMPORTANT: unless something is wrong, the voltage at the inverting input will be 2.5 V (since both inputs will be the same, unless the op-amp is saturated). That means that the resistor Ri will need to be large enough that very little current flows when Vin is large.

Here’s a quick table for Ri = 150 kΩ, Rf = 1 kΩ, Vcc = 5 V.

Vin (Volts) Vout (Volts)
-375 5.0
0 2.5
375 0.0

As you can see, an input waveform centred around 0 V is nicely converted to a much smaller waveform, centred around 2.5 V – just right for a microcontroller.

Proof

From Kirchoff’s Current Law, the current into the inverting input node is, remembering that no current flows into the op-amp:

This is simply the voltage across each resistor connected to the node, divided by that resistor’s resistance.

Simplifying:

And we get what we expect.

Notes

Remember that an op-amp is not perfect. For best results, get a rail-to-rail one (that can output voltages near the supply rails), and make sure you’re not driving too much current through Ri. Most importantly, do not connect anything to mains unless you know what you’re doing! Don’t blame me if you blow something up.

5 thoughts on “Reading AC Voltages with a Microcontroller

  1. I was doing the same project and went through your circuit also. The only difference is that i am again inverting first stage Opamp output (Inverting the output from first stage ) . DUAL OPAMP i am using.

    The problem that i am facing is that what should i do with the secondary neutral. It may be wired with ground of OPAMP ?? plus negative supply of OPAMP should be connected to ground(GND) or negative supply (-VCC) ?

    I have done the same task with AC to DC rectification but errors are so many. So i am trying to do the same with OPAMP.

    Regards,
    Ahmad

    • Hi Ahmad,

      I’m not sure of your particular application, but the input must be referenced to the same ground as the rest of the circuit. If this means connecting your secondary neutral to ground, then that might be OK. I’d be reluctant to make suggestions though, due to the potential danger of getting something wrong.

      The op-amp negative supply can be connected to either ground or -VCC, since the output is not going to be negative in normal cases. It was simpler in my application not to include a negative supply at all.