Computing stuff tied to the physical world

AC measurement status

In AVR, Software on Oct 12, 2011 at 00:01

Before messing further with this AC current measurement stuff, let me summarize what my current setup is:

JC s Doodles page 17

Oh, and a debug LED and 3x AA battery pack, which provides 3.3 .. 3.9 V with rechargeable EneLoop batteries.

I don’t expect this to be the definitive circuit, but at least it’s now documented. The code I used on the ATtiny85 is now included as tiny50hz example sketch in the Ports library, eh, I mean JeeLib. Here are the main pieces:

Screen Shot 2011 10 07 at 00 32 58

Nothing fancy, though it took a fair bit of datasheet reading to get all the ADC details set up. This sketch compiles to 3158 bytes of code – lots of room left.

This project isn’t anywhere near finished:

  • I need to add a simple RC low-pass filter for the analog signal
  • readout on an LCD is nice, but a wireless link would be much more useful
  • haven’t thought about how to power this unit (nor added any power-saving code)
  • the ever-recurring question: what (safe!) enclosure to use for such a setup
  • and most important of all: do I really want a direct connection to AC mains?

To follow up on that last note: I think the exact same setup could be used with a current transformer w/ burden resistor. I ought to try that, to compare signal levels and to see how well it handles low-power sensing. The ATtiny’s differential inputs, the 20x programmable gain, and the different AREF options clearly add a lot of flexibility.


  1. I’m still a bit alarmed by the direct AC mains connections you use. I certainly don’t like those… But hey, it works!

    If you want to power this unit, I’d use a small transformer. They don’t cost much and they exist in small form factors suitable for PCB’s. In fact the KAKU system uses these to power itself.

    Still, I think you should look for an isolated method to measure current flow. Maybe even on an induction base? Something like this? ( Yes I know those are expensive, but hey, we can find something like it right? All kinds of off the shelf components exist for this job, so why don’t you use those?

    • I understand your concern – it worries me too, but my plan is to try this as a completely enclosed unit for use in an extension cord. At least here at JeeLabs.

      For larger wattages, I hope to find a better non-contact option with hall sensor or other electro-magnetic coupling.

      The i-Snail is neat, thanks for the pointer… but pricey :(

  2. What about this approach: For detection you can replace the LED with an optocoupler.

    • That consumes too much energy for larger loads (the resulting heat is what worries me most): 1000 W @ 220 V ≈ 4.5 A, and 4.5 A x 1.4 V = 6.3 W! Evidently, for smaller loads it gets proportionally better, but still.

  3. Did you consider using this kind of small bulb that is used in neon testers? The ones shaped as screwdrivers used to test the mains? Another idea is the bulb that sometimes sits in a wall mains switch as an indicator. If you are only after current detection maybe one of those would be enough and you would have nice isolation of your low-power circuit.

    • I don’t see how current detection could be implemented with these bulbs, which are designed for voltage detection.

  4. If you want to calculate the power consumed measuring the current is only half the job. You could state that the voltage is always 230Vac but you still need to take the COS(phi) into account. For the lightbulb (resistive only load) you can leave it out of your calculations though.

    • You’re right, of course. But my first goal is simpler: detecting whether an appliance is on or off. And if sensitive enough: whether a charger is actually charging or not. I’m hoping to get away with current sensing for that, i.e. triggering on a change in average current draw.

  5. I really don’t see the direct-to-mains connection problem as long as you follow some guidelines.

    In many cases (I opened up some of those measerument stuff to look what’s inside…) I see a distinct ‘line’ between the high voltage part (pcb) and the low voltage part (pcb) where the high and low voltage pcb are separated through simple opto-couplers or even specialized opto-couplers for the I2C bus for instance.

    Direct connection is also much cheaper!

    • You should have a look inside the mains ethernet adapters. The ones I have don’t just have a line, the board is almost cut in half by a 2mm slot machined through it. Just a couple of bits left at the ends, almost like snap-off tabs you get from a PCB manufacturer.

      Still worries me a bit though, as a teenager I built a capacitor based voltage multiplier for powering a helium neon laser tube, I was always amazed at how far the 2KV running voltage could jump across the board!

  6. Conrad has some enclosures you might be able to use. For example:

    As your circuit isn’t isolated from the mains you could use a circuit such as this one ( for the power supply. Just make sure to add a fuse, we do not want to see any pictures of a burned-down JeeLabs ;)

  7. Why don’t we crack open a standard power meter for home appliances and see what’s inside? It would make a great weblog post and we could reuse a lot for the principles in there!

    • Get yourself a cresta rce-1106 for €10 and have fun I would say ;-)

      The first thing you’re gonna find out is that this power meter is even cheaper than an empty outlet case (look for “stekkerbehuizing” @ Conrad).

      On the inside you might find a simple direct 220V connection, and a nice single energy measurement chip with built in 8051/8052 CPU, RTC and LCD display connections.

      See: (the 64-Lead chips are the ones with all the goodies).

      Price is around US$ 3 for this ‘big’ one. Simpler ones are a bit more than US$ 1, but require an additional CPU.

      If you check the more expensive ones (and better ones) such as the Energy Logger 3500, you’ll find a more expensive chip like the PS1000 from archmeter:


  8. Quote:”Why don’t we crack open a standard power meter …….”

    Because we know exactly what we will find inside! The electromechanical Ferraris counter exist since ‘a houndred years’ and there are numerous application notes for electronic solutions. The problem is that these devices are much to complicated (big, expensive, etc.) for a simple ON/OFF decision.

    • I think Jasper (Vliegendehuiskat) meant energy meters with LCD displays which plug into a wall socket.

      I do intend to explore those options as well. Got a couple of them lying around. But apart from perhaps being cheaper if you count the enclosure, these meters do more than I need (and what’s more important: each type will be different, so it might be hard for others to repeat the same experiment).

    • Quote:”I think Jasper (Vliegendehuiskat) meant energy meters with LCD displays which plug into a wall socket.”

      Yes, I understand and these were included in my (maybe a bit to general, sorry Jasper) answer. As Mars also stated, all these devices use special chips with more or less microcontroller power integrated or externally added. But this is overkill for your requirement.

      TSince quite sime time there is an AN for an AVR based energy meter (AVR465?) where you can also learn something without ‘Hacking’ a real unit :-)

  9. JC, I did not look into your code until now and I am wondering if the calculation is 100% correct. It might not give a ‘wrong’ result in the end, but it is at least a bit strange:

    values[next] = readAdc(); accum += last-values[next];

    adds the ‘old’ value to the sum and subtracts the new measurement result. I would prefer:

    accum -= last; accum += values[next];

    It would just be a bit more ‘logical’, or am I wrong? BR, Jörg.

    • Ooh, good catch. Basically, the code was using the wrong sign while adjusting the difference between previous and current. I’ve fixed the code. Not much change, though – I suspect that it all “averages” out because differences are centered around zero, so using the wrong sign ultimately cancels out anyway? Still, thanks for the correction.

      I’ve also switched to using 1.1V as ADC voltage reference. Slightly more jitter, but still a very clear jump in readings with 1W loads and more.

  10. Check out these guys using a Ohmite 13FR200E current sense resistor and a Avago Technologies HCPL-7520 linear optoisolator to get reliable current sensing at cents cost:

    • It’s not that simple, I’m afraid – that linear opto-coupler is fairly expensive. More importantly, the resistor is 0.2 Ω, which uses 2x as much power (220V runs at half the amps, but that means the resistor would in fact have to be made 0.4 Ω to measure the same wattage). Furthermore, the opto-coupler draws roughly 10 mA and it looks like it needs power on both sides of the optical barrier. I suspect that the input side has a built-in op-amp. All in all, my impression is that you need quite a bit of circuitry, when you include details such as power supplies. Still, it’s good to know about this option – thanks.

  11. Now that I see this schematic I start wondering why you don’t use one of your own analog plugs? There is gain in them and a higher resolution than an ADC in a Jeenode. At 240SPS you can get a resolution of 12 bits which is 3 times (2 bits) more detail, and you could use the gain to get another 8 times amplfication (3 bits). In total that’s 32 times amplification at almost 5x oversampling of the 50Hz. With the 4 available ports you could measure mains voltage AND current measurements twice. And with the voltage measurement you could do frequency-locking as well to be able to lower the sample speed and increase the resolution to 18 bits. Thats 8+3 == 11 bits (2048x) more detail than a Jeenode (for pure resitive loads). I bet you can use that to be able to detect less than 1 Watt usage.

    • Good points. I hadn’t realized that the sample rate might be enough after all. Note that 240 SPS is per channel, it’ll go down to 60 SPS over 4 channels. Note also that voltage need only be measured once, and that a standard analog pin would be sufficient for that. But you’re right, with proper timing it should be possible to measure one value in one cycle and another in the next.

      Still, for my initial low-end goal I think I can come up with a lower-cost solution. Stay tuned…

Comments are closed.