Home
Prox / RFID
Verichips
Ladder Logic
[interfacing] †
Tube Joints
Key Code From Photo
SolveSpace (3d CAD)
SketchFlat (2d CAD)
Photographs
Miscellany
Resume / Consulting
Contact Me

LDmicro Forum - ADC conversions in presence of mains noise

(you are viewing a thread; or go back to list of threads)

ADC conversions in presence of mains noise (by Ziggy)
Ihor,

Please comment on the following:

There is a requirement to read a DC voltage in presence of mains frequency ( either 50Hz or 60Hz ) noise.

One way to minimise the noise is to read the DC value over a number of periods of mains frequency and average it over the number of readings.

300 samples over one second period would be able to handle both 50Hz and60Hz mains noise.

How to achieve this disciplined sampling over one second?
Tue Jan 23 2018, 06:57:42
(no subject) (by Ziggy)
Bump!
Fri Feb 2 2018, 16:28:56
(no subject) (by MGP)
300 samples = 300 variables, that is not workable.
In ldmicro you can't make arrays like Val [0..299] to calculate the mean.

If you want to get rid of the noise you can consider the following attached program, which can be found in Samples.zip on this forum.
Sat Feb 3 2018, 04:23:12, download attachment adc-avg-scale.ld
(no subject) (by Ziggy)
MGP,

Thanks for the link.

I did not have an intention to set up a 300 element array. More of a running average approach.

HOWEVER....

I was looking for a hint on how to structure the program in order to achieve equidistant samples over the one second interval.

I guess setting the plc cycle time to 3.33miliseconds might do the trick.
Sat Feb 3 2018, 08:02:59
(no subject) (by MGP)
To calculate averages over a period you always need arrays no matter how short that period is.

I wanted to measure the average speed of a car over a period of 30min with a sensor on the wheels for a race (max 50km / h), but with Ldmicro you can not because you can not work with arrays and the 16bit math was another bigger problem.

So in a certain sense I am also asking, but I fear that it will not work.
Sat Feb 3 2018, 13:30:49
(no subject) (by Jonathan Westhues)
Let's say that we're sampling with the A/D every 10 ms. We want to compute the average of the last 25 samples, a period of 250 ms. If we want to compute that moving average every 10 ms, then we need the 25-sample array, pretty ugly in LDmicro. If we just want it every 250 ms, then we need just one variable, for our running sum.

The 16-bit math indeed requires care. Like, with a 10 bit A/D result, you can sum only (2^15-1)/(2^10-1) = 32 samples without risk of overflow. If you first divided your A/D result by 4 (reducing your resolution to 8 bits), then you could sum up to 128 samples.

There's theoretically no need to average over a full second to reject 50 Hz and 60 Hz noise--your noise averages to zero over one full period, so any integer multiple of 1/gcd(50, 60) = 0.1 s will do. Averaging longer of course helps with other kinds of noise.
Sat Feb 3 2018, 17:16:46
(no subject) (by Ziggy)
There is no need for an array 300 cells long.
It can all be done without recourse to an array.

The algorithm is quite simple:

Take the first sample and use it as a base value.
Take the next sample find the difference between it and base sample
Store the difference
repeat above and add the difference to the original difference.
Keep repeating till sufficient known number of samples reached.
Divide sum of differences by number of samples -1
add the result of division to the base sample

done... no array only a couple of variables
Mon Feb 5 2018, 06:39:27
(no subject) (by Ihor Nehrutsa)
to Ziggy

Without the array, you get the result after 300 measurements. Period of the measure = Tplc * length(array)

With the array, you get the result at every measurement, at every Tplc.

Both ways are right.
Mon Feb 5 2018, 08:10:25
(no subject) (by Jonathan Westhues)
Yeah. And summing the deltas (like Ziggy proposes above) is better than summing the absolute ADC result (like I assumed) if your noise is small--you can sum up to 32767/(your peak-to-peak noise) samples without overflow, which is more than 32767/1023.

In most applications involving averaging, it's easier to use a first-order IIR low-pass filter, sometimes called an exponentially weighted moving average. That gives you a result every Tplc with just one state variable. You can express that in LDmicro as something like Vout = (17*Vout + 2*Vin)/19, recomputed with a new Vin every ADC sample. Weighting Vout more makes the filter slower, and weighting Vin more makes it faster. We divide by the sum of the coefficients in the numerator to make the filter's DC gain one.

In this particular case, the simple average is better, though. You're trying to reject noise at a specific frequency, and the frequency response of the average has notches down to zero at k*(1/Taverage). A higher-order IIR filter could do that too, but that's a lot more complicated to implement.
Mon Feb 5 2018, 09:59:22
(no subject) (by Jonathan Westhues)
Oh, and the n-sample simple moving average computed every Tplc requires an n-element array, but it doesn't require n arithmetic operations every Tplc--you just add the newest element to your running sum, and subtract the oldest. You can do that relatively easily with the shift register op.
Mon Feb 5 2018, 10:08:10
(no subject) (by Ziggy)
My problem at the moment is how to ensure consistant and correctly timed sampling interval.

Should I rely on PLC loop ?
Should ADC call be at the start of the ladder or at the point in ladder where the ADC output is needed?
Mon Feb 5 2018, 16:42:22
(no subject) (by Jonathan Westhues)
Easiest to rely on the PLC loop. The timing will be most consistent if you put the ADC call at the beginning of the program (since variation in execution time of the stuff before will add jitter), though it probably doesn't matter.
Wed Feb 7 2018, 01:43:22
(no subject) (by Ziggy)
Jonathan,

Thank You.
Wed Feb 7 2018, 08:18:15
(no subject) (by Ziggy)
Ok ...

first layer of experimentation completed.

Next issue is : how does the read adc behave when multiple channels are invoked at the very top of the ladder when set up as free running elements ( no control or enabling rung elements ).
Mon Aug 27 2018, 02:06:49
Post a reply to this comment:
Your Name:
Your Email:
Subject:
(no HTML tags; use plain text, and hit Enter for a line break)
Attached file (if you want, 5 MB max):