# Computing stuff tied to the physical world

## Direct 220V hookup

In Hardware on Sep 19, 2011 at 00:01

This next hookup is the most tricky one – measuring current draw via a direct connection to AC mains:

No more transformers, no more galvanic isolation. The 0.1 Ω resistor in series with the appliance generates 0.1 VAC of output for each 1A passed through it. The resistor handles up to 2W, which means it’ll be ok up to about 1000 W: the current will be 1000 / 220 ≈ 4.5 A, and the power generated as heat will be 0.45 V x 4.5 A ≈ 2 W.

Not that I intend to go that far. My main load test is still a 100 W light bulb.

The rest of the circuit is the same as before: one side connected to 1/2 VCC, the other end connected to an AIO pin. I’m using the same test sketch as before, sending out one reading per second.

So how does it perform? Not good, so far. Here’s the sketch running with no AC connected:

``````    reading:RF12-868.5.17:oneLong:value = 18070
``````

That’s 13 .. 39 mV, and it’s all over the map. These readings are with no load but AC connected:

``````    reading:RF12-868.5.17:oneLong:value = 625570
``````

Hm – 630 mV. Doesn’t make sense. Turning it off (still connected) gives this:

``````    reading:RF12-868.5.17:oneLong:value = 31128
``````

And with the 100 W light bulb:

``````    reading:RF12-868.5.17:oneLong:value = 474201
``````

Now it’s 470 mV. I’m lost…

I understand how a very high-impedance circuit can pick up a signal even when not connected. But the above levels make no sense at all. On an open line, one could explain how noise pickup would completely throw off the ADC, especially when no load is plugged in.

But in this case, the input impedance towards the AIO pin is 0.1 Ω + 22 kΩ. As a test, I even removed the voltage divider, and connected the 0.1 Ω resistor directly between GND and AIO, well… with a 100 Ω resistor in series to avoid damaging the I/O pin. The only difference was that the off-state now reads as 4 mV, as expected. But load or no load, with AC connected, the reading still jumps to around 0.7 V.

Doesn’t look like the ATmega is broken, since it sends out packets and reacts to changes on its analog input pin.

I’ll look into filtering. I’ll also consider hooking up my scope, but this comes with a considerable risk – all those wires plugged into all sorts of things and a USB-powered scope which could easily fry my computer!

Perhaps there’s a bug in the sketch. A simple multi-meter hooked up across the 0.1 Ω resistor indicates levels between 4 and 12 mVAC – which seems about right.

1. careful! this is extremly dangerous, as live AC power is available at the ADC… this can kill someone who unfortunately touches the MCU or any part of the circuit.

by the way, noise is a big problem here, you should try set the sampling frequency as low as ~ 200Hz. (theoretically 2*50=100Hz would be fine – nyquist freq.) you have to use a lowpass RC filter too!!!

• sorry, the nyquist-thing above is true only if you have no upper harmonics in the current – resistive loads, ac motors, transformers – but in case of switchmode power supplies it won’t work…

• Thanks for the link, but for now I’m exploring lower-cost options, i.e. no active circuit. Again, my goal is not measurement but (adjustable) threshold detection – a yes/no outcome would be enough for me.

2. From the ATMega datasheet: “The ADC is optimized for analog signals with an output impedance of approximately 10 kΩ or less. If such a source is used, the sampling time will be negligible. If a source with higher impedance is used, the sampling time will depend on how long time the source needs to charge the S/H capacitor, with can vary widely. The user is recommended to only use low impedance sources with slowly varying signals, since this minimizes the required charge transfer to the S/H capacitor. Signal components higher than the Nyquist frequency (fADC/2) should not be present for either kind of channels, to avoid distortion from unpredictable signal convolution. The user is advised to remove high frequency components with a low-pass filter before applying the signals as inputs to the ADC.”

With the circuit above, I’m not sure what the impedance is the A/D input sees – very hard to determine without accurately modeling the AC and DC characteristics of the 220 supply…

I think buffering the input signal might help – at least you can control what the A/D input ‘sees’.

Also, you might want to look into the “ADC Noise Canceler” part of the datasheet.

3. The impedance for the A/D converter might indeed be a problem. In my opinion it is 22 KOhm, I don’t see how the mains circuit would have an influence on that.

I would rather buffer the artificial zero (where the two 22 kOhm resistors meet), although the result would be almost the same.

About using a scope: JCW, why don’t you use the JeeNode as a scope. Just send the sampled data to the pc and display it there. I did that yesterday night with some lines of tcl/tk. I will send you a copy of the code.

• PS1: With my simple JeeNode scope I did some measurements with the Talema current sensor (wich is basically a transfomer). With a ordinary lightbulb, the signal was perfectly sine, but a energy saving lamp showed something that looked entirely different…

Thereafter, I quickly gave up the idea of a precise power measurement.

• Instead of buffering with an op amp, you can just use smaller resistors for the voltage divider… (on the left side of your schematic)

• The impedance the ADC sees is 11k (22k in parallel with 22k), this is fine. I suspect there’s a lot of high frequency noise present and adding a capacitor across the ADC input to low-pass filter out high frequencies would help.

There is also the capacitive pickup from the mains to consider – assume there is 30pF of stray capacitance to a 220VAC signal at 50Hz – that’s about 2uA of pickup which across 11k yields 23mV at the ADC inputs.

4. PS2: JCW, some thoughts about safety:

Use the shunt resistor on the neutral line, or use an isolating transformer for the whole ac setup.

OTOH, for this experiments you do not really need 230 V. You could just as well measure a low voltage ac current, result would be the same. In addition, on a low voltage setup it would also be easier to vary the load.

• Indeed, same though occurred to me – for testing, I could just do all this on the low-voltage secondary side of a transformer. No messing with high voltage until the hookup and logic have been worked out.

5. For an AC connection without filtering and with no load connected your sketch is giving you a figure related to the ‘noise power’ in your home electrical system and measurement setup.

When you add this noise to your measured voltage signal it’s average will be zero (typically Gaussian distribution) but with no load you treat the noise as your signal and measure it directly.

When you switch off (with zero load) the measurement drops significantly as you no longer have a direct connection to the rest of your house mains system.

Paul

• Ok – I think I understand, although I’m still surprised since I’m measuring across a 0.1 Ω resistor, i.e. a very low impedance load which I thought would get rid of all low-energy noise.

6. I guess filtering will be the solution, mains is an awful source of whatever mess. I would rectifie the voltage over the shunt and put a RC-behind it. This will give you a stable voltage linked to the peak-currents. You’ll have to divide by 1.41 to have real current. Just my 2 cents.

7. You are measuring an AC voltage, the atmega will measure the actual voltage at a random point in the sinewave, or maybe worse: measure the voltage while is changes (which is the nature of AC). Joost is right. You need a diode, a capacitor and possibly a resistor to discharge the cap. Then the atmega will measure the peak value of the AC voltage over the 0,1 ohm resistor.

• Note that I’m measuring > 1000 x per second and averaging in software (see the sketch of a few days ago). A diode won’t work at these millivolt levels and since this is AC, I can’t use a cap without diode, the swings would be filtered out as ripple voltage. The ADC has a sample-and-hold circuit, which should work as long as the input isn’t too high impedance (22 kΩ may be too high, I’ll try lower values for the voltage divider).

8. fyi – An interesting product to monitor mains current: http://www.theenergydetective.com/about/howtedworks

9. I think you’re not really measuring across the .1Ω resistor, because of the 22kΩ between the other end of the shunt and the ATmega’s ground. Instead, the ADC input has a low-impedance connection to your mains wiring through the shunt, which is giving you the noise.

You could use an op-amp to either buffer the voltage divider’s output or (probably better) to create a precision rectifier, which can then work across the shunt: http://sound.westhost.com/appnotes/an001.htm

Unfortunately, the ATmega328’s ADC doesn’t support differential input mode.

• I do not agree with this! The mains ground is separated ftom to JeeNode ground which is floating.

Current always flows in loops, i.e. the current that flows away from the plus pole of the low voltage circuit has to go back to the minus pole. It simply cannot divert into the mains circuit.

It would be different if the JeeNode was grounded, but this is not the case.

10. You might have a common mode voltage problem, where ground loops can cause strange measurements. Try drawing the whole circuit including the power supply for the node (I can’t make out from the fragment you show how the power gets to the circuit), and include any parasitic capacitance that might exist between the AC lines and the a/d converter input or grounds. Then, analyze the entire circuit, including the power supply and see if there could be any voltages where you don’t expect them.

11. JC, I think you should have at least some capacitance between the voltage divider and GND. A small one (5nF Y capacitor) would help to AC-couple GND (your reference voltage for the ADC) to the line. A bigger one (uF) would stabilize the ‘neutral’ voltage of Vcc/2.

Imagine that the ‘line’ voltage (where your shunt is placed) changes it potential with 50Hz whereas your GND is free floating. This causes arbitrary voltages on your ADC input.

BR, Jörg.