AC voltage measurement with a microcontroller - Page 1
Forum section: electronics / beginners

author: 09d08
author_rank: Contributor
timestamp: August 23, 2018, 07:34:05 pm
content: Hi,I need to measure AC voltages with an ATmega328 for an inverter I'm trying to build. Is a step down transformer necessary in this case since I won't be measuring mains? I was thinking of using a voltage divider, peak detection circuit and a zener for protection but I would like to get some feedback before trying out things that have anything to do with high voltage AC. What is the correct way of doing this?

author: rstofer
author_rank: Super Contributor
timestamp: August 23, 2018, 07:39:52 pm
content: What voltage?Can you convert the AC voltage to DC with a bridge rectifier and then store the DC value on a capacitor? You would then reference one side of the rectifier/capcaitor to logic ground. A transformer might help here to get the voltage into the realm of reasonable. The capacitor should charge to the peak voltage of the DC as long as there is no load on it. The ADC wouldn't load it very much.Now that you have DC, can use a resistor voltage divider or any other scheme to get the value into range for the ADC. If you want to offset the voltage (read from 10-20V instead of 0-20V (so you can get better precision at 15V), read Chapter 4 of "Op Amps For Everyone".

author: 09d08
author_rank: Contributor
timestamp: August 23, 2018, 08:53:25 pm
content: The maximum voltage would be 220VAC. I guess I should be doing this sort of stuff with a clear head, I should have thought of using a bridge rectifier. I have a small 12V:220V transformer too but I would like to keep the circuit small so I won't use it. Anyway, thanks for your help and mentioning that great book. Here's also the schematic of the inverter I mentioned for those who are interested.

author: Kleinstein
author_rank: Super Contributor
timestamp: August 23, 2018, 09:02:57 pm
content: A transformer would offer isolation, that might be needed for safety reasons.A normal small 230V/12 V transformer is not really suitable for measurement. Especially the small one (e.g. < 5 VA) are quite nonlinear due to the magnetization current. If at all they may be ok up to about 150 V.

author: 09d08
author_rank: Contributor
timestamp: August 23, 2018, 09:34:58 pm
content: Quote from: Kleinstein on August 23, 2018, 09:02:57 pmA transformer would definitely be safer due to isolation as you said but I doubt the bridge rectifier and voltage divider approach will cause problems. What do you think?

author: bson
author_rank: Unknown
timestamp: August 23, 2018, 10:22:06 pm
content: You can also use a divider network and a capacitive isolator, likehttps://www.silabs.com/products/isolation/current-sensors/si8920-isolated-amplifier, which offers 5kV (rms) isolation. Their use case for that particular part is to measure a current shunt, but you can just as well use it to measure a voltage. I've prototyped a lead-acid bank monitor with it, with one per battery interfaced with an op amp to an MSP430i2021 acting as an intelligent I2C device, and it works fine (not the greatest SNR for precision use, so not a precision device). There's an evaluation kit for it at the bottom of that page ($30). Physically smaller and easier to assemble on a board than a transformer. Measuring AC with a µC is very simple, and with a similar setup you could trivially have a configurable cycle (Vrms is always over cycle), optional calibration data, and provide other readings like zero crossing count, min, max, and DC offset, an on timer, off timer for the last outage (AC side ~0V), and a timer since the last off-to-on transition (time since last outage).

author: rstofer
author_rank: Super Contributor
timestamp: August 23, 2018, 10:27:39 pm
content: Well, 220Vrms * 1.4 = 308Vpp and I just don't want to deal with those kinds of voltages. Especially on a PCB containing any kind of electronics. Bottom line: There is no way I would use that approach.I would look for a small 'potential' transformer because they do not add distortion. I'm not sure where to look for such a thing because the ones I used were 12kV->120V, large and expensive. How do they do it in a Kill-A-Watt?

author: 09d08
author_rank: Contributor
timestamp: August 23, 2018, 10:39:51 pm
content: Quote from: bson on August 23, 2018, 10:22:06 pmThat would really be a nice solution but finding an isolation amplifier IC in my country is nearly impossible as local suppliers usually don't have such parts and buying stuff from sites like digikey is a hassle here.Quote from: rstofer on August 23, 2018, 10:27:39 pmI agree that doing this without isolation certainly isn't good practice but there's not much one can do when acquiring parts is a challenge itself. I'll try to find a small potential transformer that suits my application. I hope there actually are ones that aren't huge because using a transformer that is physically bigger than the main one for measurement would be rather weird.

author: MasterT
author_rank: Frequent Contributor
timestamp: August 23, 2018, 10:48:57 pm
content: I would use resistive divider, since galvanic isolation is not required. Just use appropriately sized resistors, common 0.125W IIRC for 100V, so 3 in series should be connected. Or even put dividers on a small proto board , that may be installed separately from uCPU. I don't see transformer output has common ground connection, you will need one.

author: Kleinstein
author_rank: Super Contributor
timestamp: August 24, 2018, 08:49:08 am
content: It depends on the application and the case if isolation is required. It also matters where the supply for the µC is coming from and what to do with the results.For which type of divider is more suitable it also depends on the required accuracy.Ring core type transformers from something like 5 VA can give reasonable accurate output to input voltage, especially if used at a lower than nominal voltage. So 2 such transformers in series might be an option. There were also special low standby loss transformers in the 3 VA range that might work.An alternative would be a series resistor and current transformer - it should be reasonable linear, but might use a little more power. This is kind of a modified resistive divider with the transformer used for current instead of voltage.One could also use opto-couplers in a linear mode, though with limited accuracy.If there is spare computation time at the µC, one could used the ADC to sample the AC waveform and do RMS calculation numerical instead of rectification. It sounds like a little overkill, but is just a little more program and saves an the HW side.

author: b_force
author_rank: Super Contributor
timestamp: August 24, 2018, 10:35:26 am
content: About isolation, it depends on the approach.You can first isolate the measured signal or you can also get a interface isolation IC.That means that you don't isolate the signal but the isolation is behind the ADC or ucontroller going to the rest of the system.

author: 09d08
author_rank: Contributor
timestamp: August 24, 2018, 11:16:24 am
content: I think I'll use a voltage to frequency converter like an LM331 with an optoisolator. A zener supply for the LM331 should suffice and provide isolation at a reasonable price. I'll draw a schematic when I get home.

author: RoadRunner
author_rank: Frequent Contributor
timestamp: August 24, 2018, 08:50:55 pm
content: One possible Way to have get 12V from your inverter transformer and use circuit something like i did for fun few year back with MSP430https://www.circuitvalley.com/2015/01/true-rms-meter-microcontroller-mains.html

author: JS
author_rank: Frequent Contributor
timestamp: August 25, 2018, 04:24:54 am
content: Getting the clean AC to the µC is the hard part, then, once you get the AC to the µC ADC with the proper offset you should just sample it and do the math there, you don't even need to porvide enough bandwidth for the signal, I successfully build an audio vumeter with an arduino (9kSa/s max?) with decent response well over 20kHz.As said a transformer would be nice but then you have the problem of the linearity of the transformer. Other way would be to make a capacitive divider and a transformer to provide isolation, but then you need to characterize the caps, the transformer and the interaction between them.JS