# Computing stuff tied to the physical world

## One piece of the puzzle

In Software on Sep 22, 2011 at 00:01

The measurement anomalies of the recent experiments are intriguing. I don’t want to just tweak things (well, up to a point) – I also want to explain what’s happening! Several insights came through the comments (keep ’em coming!).

Let me summarize the measurement algorithm: I measure the < 1 VAC peak-to-peak voltage ≈ 5000 times per second, and keep track of a slow-moving average (by smoothing with a factor of 1/500-th). Then I calculate the arithmetic mean of the absolute difference between each measured value and that average. Results are reported exactly once every second.

Some notes:

• noise should cancel out for both the average and the mean values, when the signal is large
• noise will not cancel out if it is larger than the fluctuation, due to the absolute function

There’s also another problem:

I’m not synchronizing the averaging period to the power-line frequency, nor taking zero crossings into account.

This matters, and would explain up to some 2% variation in the measurement. Here’s why:

• each 1-second sampling period measures about 50 cycles
• let’s assume it’s 49-cycles-and-a bit
• the extra “bit” will be residuals at both ends
• the 49 cycles will be fine, averaged from zero-crossing to zero-crossing
• but the ends may “average” to anything between 0 and 100% of the true average for entire cycles
• so 1 of the 50 cycles may be completely off, i.e. up to 2% error (always too low)

So it looks like there are two measurement issues to deal with: noise and the lack of zero-crossing syncing.

It doesn’t quite explain the extreme values I’ve been seeing so far, but for now I’ll assume that an excessive amount of noise is being picked up in my crude breadboarded circuit and all the little dangling wires.

Shielding, digital noise filtering, and syncing to zero-crossings… quite a few things to try out!

1. I can’t help thinking that there was still some progress which you could make with the isolated method (that’s what I’m playing with in the garage at the mo). Not only does it reduce the risk of an ouch but once you have your isolated AC voltage you have the change to rectify and smooth it before it meets the ADC port.

That’s my plan anyway!

2. To keep things as simple as possible: As we (still) have quite stable 50Hz here in Middle Europe, it is not necessary to care about zero crossings if you just make sure that you sample always whole numbers of fullwaves (or even halfwaves, if there are no half-wave rectified loads). So sampling for n*20ms is ok, too. And why care about 2% error if you make 11% error by using arithmetic average instead of RMS? If your goal is exact measuring values for all kind of loads and line frequencies than you have to: – sync to zero crossings and measure full waves – sample in an interrupt with exact sample timing – take a ‘high enough’ number of samples (preferably ~>100) – use RMS calculation – …?

BR, Jörg.

3. measering my power consumption, i had the same problems. In many power grids power line communication (PLC) is used, so there ist not only the 50 Hz frequency and its harmonics (due nonlinear loads)but many different HF frequencies in the whole band from several khz up to 30 Mhz. May be a low-pass will help.

4. Since your signal is a continuous sine(ish) wave where the RMS value for 1/4 wave should be the same as that of a full wave the error from imperfect synching shouldn’t be more significant than 0.5% ?

• Heh – I knew someone would bring it up! Here’s my visual explanation: there are four π/2 segments, all the same shape, each one the same average (if it were a pure sine wave). There are two segments which can be incomplete. Those segment would not introduce any error if they were rectangular, i.e. a square wave, but they are sine segments.

So indeed, the error will remain under 0.5%: the difference between a sine and a square wave, amortized over ± 50 cycles. Exercise for the overzealous reader: when is the relative error maximal?

Which all goes to show that I’ve got bigger fish to fry!

• Yes, but this is a rather theoretical caclulation. In many cases current is not sine, not even sine(ish). And still, real ‘synching’ is not needed, it is only necessary to make the measuring period n*10ms long.

Or do you operate your houses with diesel generators (which can be way off 50Hz?

BR, Jörg.

5. Agreed. Just wanted to explain that part a bit more.

6. Btw: It seems possible to measure all switches from one location: http://www.technologyreview.com/computing/22947/