Michael Gold's picture

Re: Software


Great work on this project thus far! Your version of the of the Powermax optoisolator-based design is very accessible for a relative newbie like me (who works on software by day and is a hardware hobbyist only at night).

I ordered all the parts from (substituting parts that I had on hand like the powerstrip that are specific to the US grid) and I assembled the circuit on a solderless breadboard as per the schematics and the instructions. (If its of interest I can upload the list of part numbers I ordered from my US-based supplier). The build went smoothly, and I think I have a working power monitor circuit.

That said, on the software side of things, I'm encountering a couple of issues:

1) The Arduino sketches seem to be functioning, and I am getting serial data, however the frequency calculations seem to be off. Using my 'device under test' (a 100-watt incandescent light bulb - soon to be an antique we hope) I am getting readings like 89.xx, 102.xx etc. My first thought was perhaps the issue can be attributed to the difference in frequency (60Hz on this side of the pond). I looked at the at the Arduino code and didn't see anything specific to the 50Hz used in the UK that I would have needed to convert to 60Hz. On the other hand, I did see a call to delayMicroseconds() (set to 669), and changing that value seems to affect the frequency reading. What is the 669 microsecond delay for?

2)In the Java viewers I am not able to zoom - the X and Y buttons are not functioning when I click them.
for reference:
#java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)

Next I think I'm going to try to get everything working with one of my xBee modules...

Overall, really nice work!

Inhisding's picture

Re: Software

Great blogpost, great looking weblog, added it to my favorites.

TrystanLea's picture

Re: Software

I've had another look at the delayMicroseconds line. The reason it is there is to slow down the sample rate so that 1000 samples cover 1s of time.The reason it effects the value for frequency when its decreased is down to noise in the voltage waveform.

If we have a look at the frequency calculation part:

The first line tries to find the point at which the voltage waveform crosses the zero line. The program calculates frequency by counting the time between each time the voltage crosses 0 (going from - to +) it then takes an average by repeating the step several times.

if (fpVLastValue < 0 && instV >= 0)
         vPeriod = millis() - vLastZeroMsec;

         if (vPeriod > 0) {
            vPeriodSum += vPeriod;
         vLastZeroMsec = millis();
      fpVLastValue = instV;

The main problem is that "if (fpVLastValue < 0 && instV >= 0) = true" does not always happen once a cycle. There is a certain amount of noise in the voltage waveform and if we look at the point at which the voltage crosses zero in detail we will see that it actually dips back down over the zero line and back up again. The program cant distinguish between these small oscillations and the mains ac frequency cycle and so the frequency will be larger than the correct frequency.

But the program only sees these small oscillations when it looks at the crossing point in detail, a delay value of around 669 microseconds will skip over these small oscillations and so the program will only register the larger mains frequency variations.

There are therefore three ways we can minimise the error caused by these small oscillations

1) Have a large amplitude voltage waveform. If you can use the power sampler program to take a picture of the voltage waveform that would help identify if the voltage waveform is too week or noisy. What value resistors are you using for the voltage divider?

2) Skip the small oscillations by having a sufficiently high delayMicroseconds. Although this will also lower the accuracy of the power measurements.

3) I could look in to a better method of measuring the frequency altogether that does not rely on such a good quality waveform.

Let me know if I can clarify any of the above.
Hope that helps


Michael Gold's picture

Re: Software

It does help. Thanks!

My samples ( http:[email protected]/sets/72157621968166756/ ) are based on voltage divider where RA is 2.2K and RB is 2.2M. There is a fair amount of oscillation, which would explain the frequency changes, and would also explain the change in frequency with the addition/removal of devices-under-test.

In another project I used a low-pass filter to smooth the inbound data. (Accelerometer data from iPhone or Wiimote to control a R/C car) Would a low-pass filter be useful here?

Maybe the bigger question is this: Given the oscillation, is zero the ideal threshold by which to measure frequency?

I have found a few hardware approaches for frequency measurement. For example, I recently came across an implementation (http://www.phzmonitor.com/) that connects an AC to AC transformer adapter to an op amp. From the incoming sine wave, the op amp creates a square wave, and a PIC microcontroller then counts the number of clock cycles between the leading edges of the square wave.

I recently breadboarded something somewhat along those lines, AC to AC transformer to a diode (half wave rectifier) to a voltage divider, then to an analog pin on the Arduino. Using the comparator built into the Atmel chip on the Arduino, the sketch compares the sampled voltage to the Atmel's bandgap voltage, and measures frequency based on how long it takes in milliseconds between 120 transitions of the sampled voltage above/below the bandgap voltage (120 transitions for 60Hz). The sketch is based on this: http://news.jeelabs.org/2009/05/28/measuring-the-ac-line-frequency/ .

I get what seems to be decent results using this approach, but the results are different from the square-wave leading-edge approach used above (they have a live stream of grid frequencies which I used to compare results).

Not sure which of the two approaches is best, (or if neither are). In both of the cases, though, the oscillation seems to be factored out because of the comparison to a voltage reference.

By the way, the optoisolators work as advertised. In miswiring my voltage divider I just fried them (and not my microcontroller, or myself). Quite a spark indeed. Always a good reminder to treat mains electricity with care.

Now, to order more optoisolators...


TrystanLea's picture

Re: Software

Ive had another look at the blowing optoisolators. Ive created a page about it here in case it comes in useful.

Looking at your method of frequency measurment, I dont quite understand what your doing when your comparing the sampled voltage to the Atmel's bandgap voltage. I havent used the more low level comands as used in the jeelabs example so I cant quite see whats happening. Do you have a code sample of it working on the Arduino?



TrystanLea's picture

Re: Software

Thanks for the screens, Its great to see the program working for you! Nice window theme too!

The waveforms don't look right though. Are they from before or after the spark incident? I have also blown two hcpl's and the output look very similar especially: http:[email protected]/3797316264/in/set-721576219681... were the waveform oscillates erratically as the main sinusoidal waveform crosses zero.

Im wondering if there is a problem with the circuit design that allows a maximum rating to be exceeded some ware...

The reason I say this is that I recently changed my high side power supply from a cheap 5V volt adapter which was actually 6V and so I had to use a 5V 100mA voltage regulator to keep it at 5V, to a computer power supply which gave a clean 5V output. The hcpl's blew soon after and I wonder weather the voltage regulator was acting as some sort of current limiter to the supply...

It could also be possible to add small resistors to the inputs of the hcpl's to limit the input currents

Im away now until Monday, I will have a closer look at it then.

The method you found for frequency measurement looks great, it would be good to try it.



TrystanLea's picture

Re: Software

Hello Michael

Thanks for your post! Its really great to hear that your building it.

I put the delay in to have an output from the Arduino once a second. I will have another look at the program though because it think it might be possible to get rid of it completely.

Have you managed to get an output of the current and voltage waveform for your setup? If I could have a look at that, that would help me to work out what's happening with the frequency.

Is view panning/moving working for you? and do you have a mouse wheel?

I also realise that the description of the electronics wasn't clear about the need for two power supplies one for the high side and another for the low side, I've updated the isolation and electronics pages with details on it.

Are you using the Xbee's with the Arduino shield or Adafuit adapter or another? Good luck with connecting it up, it would be great to have wireless communication! Id be really interested in hearing how it goes. I have two xbees waiting here, ready for when I get time to play with them.



Michael Gold's picture

Re: Software


I uploaded a handful of sample waveforms to flickr:

http:[email protected]/sets/72157621968166756/

I included a baseline (no loads), as well as waveforms for my laptop, the incandescent lightbulb, a CFL, and combinations of my laptop plus the bulbs.

Viewing and panning were fine; but since I was on a laptop with a trackpad, scroll-wheel movement was not an option. After reading your reply, I connected a USB mouse with a scroll wheel, and I was able to move the wheel and adjust the zoom (quite fluidly, actually).

The original schematic was great. Clear and elegant. I was able to discern the high/low sides without a problem. I used a 5V powersupply as well as the 5V from the Arduino.

The xBees I have are on Adafruit breakout boards, which I have been using with a Arduino Duemilanove and a Boarduino thus far. I'll begin by using the microcontrollers, but I would eventually like to leverage the built-in AIO sampling pins on the xBee such that I can embed the radio and sensors into a power outlet without the need for a microcontroller. The xBee would be pre-configured with AT commands to wake up every X seconds and send a sample to the xBee PAN co-ordinator. The coordinator server would then collect/aggregate the data. I think I'll have to remove an IC from one of the adafruit breakout boards in order to use the xBee AIO pins (same way that ladyada implemented xBee radio for the Tweet-a-watt).

I have some vacation time coming later in the month, perhaps thats when I'll work on that...(plus I planned to tinker with both a hall effect sensor IC and a hall-effect current transducer that came in the parts order that contained the optoisolators and current-sense resistor. Your addition of the non-invasive CT method could not have been better timed!)


chales fernendes's picture

Re: Software

online marketing

Thank you for the information,it helped me solve my problem..