## rms current measurement using attiny88

9 posts / 0 new
Author
Message

i am using adc for current signal measurement. controller frequency is 8MHz and i initialise adc as

1] prescaler-> 64

5]auto trigger source -> free running mode.

i have considered 200ms time span samples, sqaure it , average it and then take square root

why for fixed current input adc count vary 2 to 3 count?

First, where is your complete schematic?   What filtering are you applying to the signal & also for the AVcc  & Aref pins?

Out of 1000 counts 2 or 3 is not much, what are you signal levels with respect to the ADC reference?

Also what is the signal freq you want to measure?

You probably don't need to start off using ADC interrupts.

Do you need to take the square root?   Only if you need to show the result, otherwise use the avg squared value.

There are many postings on the Freaks regarding RMS

When in the dark remember-the future looks brighter than ever.

I actually think a deviation of 2 or 3 counts is pretty stable for such a simple setup.

How many samples do you accumulate in that 200ms time frame?

For this algorithm to work correctly, the averaging has to be done over an EXACT number of cycles of the input signal.

If I assume a 50Hz Input signal and you integrate over 200ms that is 10 cycles.

But if your input frequency is 49.9Hz you have to integrate over 200.4ms.

If I did the calculation below correct you have 1923.1 samples to average. How does that work out for you ???

```octave:1> M=1e6
M =  1000000
octave:2> 8*M/64
ans =  125000
octave:3> 8*M/64/13
ans =  9615.4
octave:4> m=1e-3
m =  0.0010000
octave:5> 8*M/64/13*200*m
ans =  1923.1
octave:6>
```

If you do the averaging right you can get about 13 bit's of signal out of an AVR's 10 bit ADC. The rest is noise.

The calculation of the RMS value can be done in lots of (slightly) different ways.

Take a lot of samples, and draw the difference from the average in a graph. Does it look like noise, or is there some beat frequency in those 2 lowest bits?

You may improve your results a bit by starting the ADC at a fixed delay after the zero crossing of the input signal. It will not give you more "real" accuracy, but you might be able to always measure the error in the same way and get (slightly) more stable results that way.

Note:

Are you sure the AVR is fast enough to do the squaring and accumulation of all the samples without missing some?

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

For this algorithm to work correctly, the averaging has to be done over an EXACT number of cycles of the input signal.

Well, not 100% true.  For example, you may not know the freq, or it may  be variable.  If you accumulate many cycles (say 100) over a long time, then any partial cycle will become rather insignificant (assuming being within a percent or so is good enough).

You could also simply use an RMS converter chip & read the voltage from that. It will not "miss" anything "fast" since it is not sampling.

When in the dark remember-the future looks brighter than ever.

avrcandies wrote:

You probably don't need to start off using ADC interrupts.

Triggering the ADC from main is prone to jitter which in turn compromises the overall measurement.

avrcandies wrote:

Do you need to take the square root?   Only if you need to show the result, otherwise use the avg squared value.

You measure now a current i. After one second the real current doubles which without the square root will show as 4i, which is wrong. Therefore the square root is needed.

avrcandies wrote:

For this algorithm to work correctly, the averaging has to be done over an EXACT number of cycles of the input signal.

I do find this statement 100% true.

avrcandies wrote:

For example, you may not know the freq, or it may  be variable.  If you accumulate many cycles (say 100) over a long time, then any partial cycle will become rather insignificant (assuming being within a percent or so is good enough).

If you accumulate over "many" cycles you lose responsiveness, which reduces the quality of the meter. A rather foggy comment.

avrcandies wrote:

You could also simply use an RMS converter chip & read the voltage from that. It will not "miss" anything "fast" since it is not sampling.

Off topic.

Triggering the ADC from main is prone to jitter which in turn compromises the overall measurement.

Start off and don't worry about interrupts yet, there are many other things to take care of first.  A slight change of a sample location won't matter. to get things up and running.

Therefore the square root is needed.

This is not correct....say you wanted to sound an alarm if the RMS value exceeds 13. You can take the complicated square square root and compare to 13 , or avoid the square root delays & simply compare to 169.

If you accumulate many cycles (say 100) over a long time, then any partial cycle will become rather insignificant (assuming being within a percent or so is good enough).  so an exact cycle multiple is not needed, if you don't need a fast response.

Off topic.

What is your issue?  An RMS chip is a reasonable alternative; don't sound like you are a robot chatbox.

When in the dark remember-the future looks brighter than ever.

Where the sound alarm comes from, your guess ? I the OP looking for an alternative chip ?

Paulvdh wrote:
You may improve your results a bit by starting the ADC at a fixed delay after the zero crossing of the input signal.

I would start measuring at 90° or 270° where the amplitude varies the least. But it is not clear it is about an AC signal at all.

I would use a timer as trigger source instead the auto trigger mode in order to have a better control of the sampling rate. If it is about an AC signal, the internal oscillator may not be accurate enough.

angelu wrote:
I would start measuring at 90° or 270° where the amplitude varies the least
This is also where the amplitude is greatest, so you get the biggest deviation if the integration time is not an exact multiple of cycles of the input signal. Best theoretical phase shift is probably where the amplitude is near the RMS value. Then the error of a sample will cancel out with the integration time. But any synchronisation with the input signal is probably better than none.

But OP is having 2lsb "noise" after collecting an unknown amount of samples from an unknown frontend and after compex calculations with only a vague description of what those calculations do. This is not a good position to build solid advice on.

If you ignore the 2lsb noise, how many effective bits do you have at the end of your calculation?

To get an idea of the accuracy you can write a program to simulate it on your PC.

Fill an array with "perfect" sine values instead of an ADC, and then do some experiments with small frequency and phase shifts.

What kind of differences do you get while working with floats or integers?

What kind of issues do you have with rounding errors?

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

I did play a bit in Excel in order to see where is best to start sampling.

So I take 10 samples per cycle, for up to 10 cycles starting at zero cross, at 45° after the zero cross (where signal amplitude equals the RMS value) and at 90° after the zero cross.

Here is a graph when sampling with a period 10% longer than the original signal:

In this case, best is to start sampling at 45°. The fact that all three cases converge after 50 and 100 samples it is due to the fact I do sample ten times per cycle, and the sampling period is 10% longer. This is a particular case, and it does not converge for any difference in sampling frequency or number of samples taken.

And here is a graph when sampling with a period of 90% of the sampled signal:

In this case best is to start sampling at 0 or 90 degrees.

Edit: The signal amplitude is 1, so the RMS would be sqrt(2)/2 = 0.707...

The x axis represents the number of cycles, therefore 1 represents 10 samples taken, 2 - 20 samples taken and so on.

## Attachment(s):

Last Edited: Sat. Apr 21, 2018 - 06:13 PM