Microcontroller True RMS with noisy waveforms?

Guys -

In measuring True RMS voltage and current, waveforms from non-linear loads like rectifiers chop up the current and cause noise on the voltage. In me asuring with an A/D converter of a microcontroller, we need to figure out z ero crossing points in order to avoid truncation errors in the data to defi ne the start and stop of each cycle. That is challenging with a lot of har monics and noise that cause tons of false zero crossings.

Does anyone have any experience in doing something similar to this and how did you deal with the zero crossing noise?

Thanks!

Reply to
EnigmaPaul
Loading thread data ...

If you know the frequency (namely 50 or 60 Hz) you can sample for an integral number of cycles.

Or more generally, do about what a DVM does: take samples, square them, lowpass filter, and square root the filter output at the display rate.

--

John Larkin                  Highland Technology Inc 
www.highlandtechnology.com   jlarkin at highlandtechnology dot com    

Precision electronic instrumentation 
Picosecond-resolution Digital Delay and Pulse generators 
Custom timing and laser controllers 
Photonics and fiberoptic TTL data links 
VME  analog, thermocouple, LVDT, synchro, tachometer 
Multichannel arbitrary waveform generators
Reply to
John Larkin

Yes, harmonics make it harder to synchronize to some fundamental frequency.

No, you don't have to measure synchronously to some fundamental frequency.

No, looking for zero-crossing points is not the best way to synchronize to some fundamental frequency.

No, you don't have to synchronize at all. In fact, unless you know that you are looking at a signal that is dominated by a single tone at a constant frequency, you probably should not synchronize.

And your worst problems are going to be due to issues that you didn't even mention.

What exactly are you trying to measure? Power line voltage? Some other low-frequency power? Audio? Anything at all?

As JL said, sampling, squaring, low-pass filtering, and square-rooting is a good general technique. The amount of low-pass filtering you need is usually disappointingly large -- getting the RMS power of noise just takes a lot of time.

If you're making some sort of power line monitoring equipment, and if you don't mind if it doesn't work well when powered from gen-sets and 3rd- world grids, then sample the noise over 100ms. That's exactly 5 cycles of 60Hz, and 6 cycles of 50 Hz. Better yet, give it ramps on each end so that it has zeros at 50 and 60Hz, but filters the edges better.

If it's for power line measurement and you want to accurately measure gen sets and stuff, and for some reason you don't like Mr. Larkin's suggestion of just low-passing the squared wave, then get lots of samples per cycle (which you need to do anyway, to catch all the harmonics). Low- pass or band-pass filter so that you capture everything up to about 65Hz; you should get a clean-enough sine wave that you can phase lock to the power line. Then phase lock using a multiplying phase detector (and if you don't know what a multiplying phase detector is, ASK). DON'T use the filtered version for your measurement -- you're just using that for synchronization.

You didn't ask about how fast to sample. The answer is -- fast. You need to sample fast enough so that the squared version of your waveform does not suffer from significant aliasing. That means that if your frequency of interest in the non-squared signal is f_i, you need to sample fast enough to capture the signal at 2 * f_i that you'll be generating in the squaring operation. I'd probably use f_s = 5 * f_i as an absolute minimum sampling rate, and I wouldn't hesitate to go to ten times if my hardware would allow it.

--
Tim Wescott 
Control system and signal processing consulting 
www.wescottdesign.com
Reply to
Tim Wescott

s like rectifiers chop up the current and cause noise on the voltage. In measuring with an A/D converter of a microcontroller, we need to figure out zero crossing points in order to avoid truncation errors in the data to de fine the start and stop of each cycle. That is challenging with a lot of h armonics and noise that cause tons of false zero crossings. I am attemptin g to measure AC power line voltages and current.

w did you deal with the zero crossing noise?

Reply to
EnigmaPaul

.

Power Line Voltage.

Actually it specifically needs to work with gensets, and gensets that are b eing loaded by non-linear loads like VFDs.

-

There may be other reasons where synchronization is needed, such as to sync hronize the transfer between two sources, but that appears to be a separate problem. I probably need to also compute phase angle at some point as a f uture add-on so that needs to be considered.

s

Reply to
EnigmaPaul

The actual sampling rate does not have to be very high, if you can wait for the result for a few seconds.

The sampling pulse to the S&H circuit must be narrow (hundreds of microseconds) but the actual A/D sampling rate could be about the same (but not exactly the same) as the mains frequency. With a sampling pulse every 361 degrees of mains frequency, it would take about 7 seconds at 50 Hz and 6 seconds at 60 Hz to reconstruct a repetitive waveform with 1 degree resolution. Taking a few samples every mains cycle or reducing time resolution, would drop the averaging time considerably.

Think about how sampling oscilloscopes works.

Reply to
upsidedown

Most micros have a relatively fast A/D. Why not make multiple samples for each cycle, for a few cycles? And then take an RMS of these samples?

--
Mike Perkins 
Video Solutions Ltd 
www.videosolutions.ltd.uk
Reply to
Mike Perkins

You've got a lot of good reasons to synchronize, then.

My knee-jerk first candidate for synchronizing this would be to use a multiplying phase detector: generate a sine wave at the frequency that I think I'm looking at, and multiply that by the incoming signal. If the incoming signal is exactly 90 degrees off from my reference sine wave, then I have zero phase error. Then I'd steer the frequency of that generated sine wave using my measured phase error (in other words, I'd make a phase locked loop). The fact that it's a genset whose frequency may be varying complicates things, particularly if you want this to work with any genset, no matter how badly the frequency fluctuates.

I had said low-pass and then use a multiplying phase detector -- I was wrong. It's all the fault of trying to be sensible before I've consumed anything with caffeine that day. At any rate, if you run the phase detection over one cycle of your reference sine wave that'll automatically act like the filtering you want.

Zero crossing after filtering may be adequate, particularly if the sampling rate is high, but there's an uncertainty about exactly where the zero crossing occurred unless you interpolate the zero crossing between input samples. With the multiplying phase detector approach, you'll always nail the phase dead on as long as the frequency isn't varying too much. (Paradoxically, the problem of the indeterminate zero crossing is actually improved if the source has some phase jitter -- the jitter in the actual phase tends to randomize the quantization due to sampling, which makes it less of a problem).

Either of these techniques will probably work best if you're willing to vary the speed of your ADC sampling so that you always have an integer number of samples per cycle on the AC line. Varying your sampling rate like that is a pain in the behind, but it's often less so than trying to compensate for a whole measurement system that's centered around one cycle of some incoming AC.

--
Tim Wescott 
Control system and signal processing consulting 
www.wescottdesign.com
Reply to
Tim Wescott

That's true, at least if you're much more interested in repetitive phenomena than random noise. In fact I discuss that effect here:

formatting link
I was just assuming (or perhaps presuming) that the OP needed numbers in a snappy fashion.

--
Tim Wescott 
Control system and signal processing consulting 
www.wescottdesign.com
Reply to
Tim Wescott

Hi Guys -

As the OP, I realized that I should probably more clearly state my objectives so as to make it easier for you to focus your advice.

  1. I am measuring 3-Phase Volts and 3-Phase Amps
  2. It must yield a 'True RMS' measurement accurate to 1% of full scale for voltage (say 480V), and 2% of full scale current (5A).
  3. The current sensing will use standard 5A secondary Current Transformers across a precision shunt resistor.
  4. The processor available is a 32 bit ARM Cortex with built-in ADC (12Bit).
  5. I expect substantial harmonics and possibly conducted/radiated noise from non-linear loads such as VFDs. Measurement up to the 10th harmonic is desirable.
  6. I don't need especially speed response of the Volts and Amps values, substantial delay is acceptable up to perhaps a second or two.
  7. It must work with engine-driven generator outputs which may not be 'stiff' power supplies compared to the grid. However, usually the outputs will be reasonably well regulated with electronic governors and automatic voltage regulators.
  8. Although its not part of my initial design, I do anticipate adding phase angle to complete KW and PowerFactor and to possibly trigger in-phase synchronization of two AC sources.
  9. Although there is a fairly high power CPU available, lots of other activities will be happening at the same time such as network communications, supervisory functions, time delay logic, processing of sensors, etc. This is happening within the framwork of a basic pre-emptive RTOS (FreeRTOS).
  10. It is desirable to do as much of the RMS processing and filter in software so as to keep the hardware generic.

Hopefully this helps to focus the discussion.

Thanks for your input Tim, John, and Mike.

Paul

Reply to
EnigmaPaul

Since 6 pulse rectifiers (without smoothing) tend to have quite long conducting angles, the peak/average ratio is pretty moderate, compared to single phase rectifier with a large capacitor, thus less problems with the dynamic range.

The peak current handling (full scale) capacity needs to be many times the average ratings, if big single phase electronic loads without PFC are expected.

So at least 20-30 sample points are needed. To get those points during a single 60 Hz cycle, a sampling rate above

2000 Hz would be sufficient.

If the generator frequency is sufficiently stable during that one second (mechanical inertia), you could spread out those 20-30 sample points all over the 1 s period, thus less than one sample/cycle. Taking a few asynchronous samples every cycle and the measurement will also follow subsecond speed fluctuation. Some odd sampling frequency, such as 379 Hz should work with 50/60 Hz with some governor tolerances, without falling to sync.

While real power [kW] and power factor [0..1] make sense for any kind of waveform, the phase angle is relevant only for linear (RCL) loads, not for non-linear loads, such as the VFD input rectifiers.

I think that you should forget the issue of trying to measure phase angles at the moment and study other methods to measure the RMS current. The real power can be obtained by multiplying the voltage and current samples obtained out and averaging the result.

Doing a three lamp phase synchronizer in software ?? I still do not think that you need to know the phase angle.

Reply to
upsidedown

Yup.

He needs to be above Nyquist _after_ squaring. That means he needs to be able to capture up to the 20th harmonic. So, greater than 40 samples per cycle. I'd probably sample at no less than 80 samples per cycle, and even then I'd test to make sure that it was enough sampling to insure accuracy.

So 4800Hz, at least. Fortunately, that's probably not much for his processor.

I'm just contrary today, but if processor speed is an issue, capture a vector of samples from one or two cycles of the input wave, then process at leisure.

--
Tim Wescott 
Control system and signal processing consulting 
www.wescottdesign.com
Reply to
Tim Wescott

The Nyquist sampling rate doesn't apply to electric metering. A meter is gathering statistics on a waveform, not trying to reconstruct it.

I've sold meters (thousands of them, 16 CT channels each) that sampled voltage-current pairs at about 27 Hz. Worked fine.

--

John Larkin                  Highland Technology Inc 
www.highlandtechnology.com   jlarkin at highlandtechnology dot com    

Precision electronic instrumentation 
Picosecond-resolution Digital Delay and Pulse generators 
Custom timing and laser controllers 
Photonics and fiberoptic TTL data links 
VME  analog, thermocouple, LVDT, synchro, tachometer 
Multichannel arbitrary waveform generators
Reply to
John Larkin

I don't think that's the case, because otherwise Rayleigh's theorem wouldn't work for DFTs, and it does.

Squaring the samples does mean that the transform turns into its own autocorrelation, so that it's twice as wide and there is all sorts of aliasing happening.

The trick is that the average power measurement is taking place at the zero frequency sample, where (assuming the Nyquist criterion was satisfied at sampling time) the smearing doesn't quite reach.

Cheers

Phil Hobbs

--
Dr Philip C D Hobbs 
Principal Consultant 
ElectroOptical Innovations LLC 
Optics, Electro-optics, Photonics, Analog Electronics 

160 North State Road #203 
Briarcliff Manor NY 10510 USA 
+1 845 480 2058 

hobbs at electrooptical dot net 
http://electrooptical.net
Reply to
Phil Hobbs

You must be right, because I distinctly remember proving Parseval's (AKA Raylieig's) theorem, but I can't quite wrap my head around your answer just yet...

--
Tim Wescott 
Control system and signal processing consulting 
www.wescottdesign.com
Reply to
Tim Wescott

That works using the sub-sampling technique, which may or may not work on a genset with a wavering frequency output.

--

Tim Wescott 
Wescott Design Services 
http://www.wescottdesign.com
Reply to
Tim Wescott

Good comments guys.

I am reviewing an existing software design from another engineer so I needed to spend some time trying to understand what is currently being done. This design works but has problems with current sensing not working or working inaccurately when presented with non-linear VFD loads.

The basic sequence is as follows. Each RMS calculation is tored in a ring buffer in FIFO fashion:

Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS Voltage (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS current (Ph A, B, and C) Wait 200ms while doing other systems tasks

Calculate average RMS Voltage (Ph A, B, and C)

The averages of the above 5 calculations is taken and presented to the display screen and monitoring system.

Each RMS calculation consists of:

1) Each 1/2 waveform being sampled at 21us between each sample. 2) Slope detection and zero crossing detection is used to start and stop the sampling and to identify "tops" and "bottoms" of the waveforms. 3) The tops and bottoms are sampled in an alternating fashion for each RMS calculation above. Hence, we might sample a top and then approx 200ms later start sampling a bottom. 4) When two tops and two bottoms are sampled (equivalent to 2 cycles), then these are used to calculuate RMS and is fed to the ring buffer above. 5) The microcontroller has a hardware feature for detecting zero crossings which is used at the basis of the zero crossing detection using interrupts. We also do some additional checks to verify monotonicity on the result of the zero crossing to try to verify that its legitimate and not "noise". 6) There does not appear to be any low pass filtering in the digital domain. There is some hardware filtering; a second order filter that starts to roll off quickly after the 10th harmonic (assuming 60hz fundamental).

I know the devil is in the details and I've probably left out some important ones that I have yet to discover but I am wondering if anyone has any general comments or critique on this overall approach?

It strikes me that:

a) The sampling rate seems unnecessarily high. Its sort of like we can vastly oversampling with LONG gaps between sampling runs. b) Splitting the waveforms into tops and bottoms seems complicated and subject to error in the presence of harmonics. i.e. how do we know we are on a positive slope of the fundamental and not the positive slope of a double dip bump due to harmonics? c) There could be things changing in between the long gaps of sampling runs. For example the frequency shifts between the time we sampled the top and later sampled a bottom. d) the zero crossing detection in the microcontroller is a bit of a black box for me. Its not clear to me these are designed to work on noisy waveforms.

Thanks!

Reply to
EnigmaPaul

I fogot to mention that it is called the "Analog Watchdog".

Reply to
EnigmaPaul

The best way to meter a variable frequency is to pass the sample data (E*I products for power, E^2 or I^2 for RMS) through a lowpass filter, to get a tapered windowing function. Sample rate should be fairly high to avoid wobbles in the filtered data, but the amplitude of higher harmonics is usually low, and (if the line voltage is basically sinusoidal) there's no power in the current harmonics.

This is like the recent discussion about measuring the realtime efficiency of a switching regulator. A rectangular averaging window makes ugly power values unless the window is synchronized to the waveform, but a smarter windowing function (easily implemented by an IIR filter) smooths things much better.

A high sample rate avoids folding harmonics back into the filter passband, which is a Nyquist sort of statement. There might be an argument for nonperiodic sampling.

AC line measurements are usually so messy that a little measurement-to-measurement noise is expected, so if the sample rate and filtering aren't extreme, nobody will notice. Where you get verified is on energy measurement, and that's averaged over minutes.

--

John Larkin                  Highland Technology Inc 
www.highlandtechnology.com   jlarkin at highlandtechnology dot com    

Precision electronic instrumentation 
Picosecond-resolution Digital Delay and Pulse generators 
Custom timing and laser controllers 
Photonics and fiberoptic TTL data links 
VME  analog, thermocouple, LVDT, synchro, tachometer 
Multichannel arbitrary waveform generators
Reply to
John Larkin

Excuse me, but that's insane.

Periodically and continuously do this, probably in an ISR:

Sample voltage:current pairs as close in time as possible; call the samples E and I

Autozero the samples.

Compute P = E*I, lowpass filter to get realtime power

Integrate power to get energy

Compute E^2, lowpass filter. Square root whenever you want RMS voltage results.

Ditto current

Compute volt-amps from RMS voltage and current

Do that for each CT in the system.

--

John Larkin                  Highland Technology Inc 
www.highlandtechnology.com   jlarkin at highlandtechnology dot com    

Precision electronic instrumentation 
Picosecond-resolution Digital Delay and Pulse generators 
Custom timing and laser controllers 
Photonics and fiberoptic TTL data links 
VME  analog, thermocouple, LVDT, synchro, tachometer 
Multichannel arbitrary waveform generators
Reply to
John Larkin

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.