| Author |
Message |
|
|
Posted: Mar 05, 2009 - 04:43 PM |
|

Joined: Sep 12, 2006
Posts: 5
|
|
Hi Could someone please tell me how to calculate rms
value of 50hz sine wave signal which is going into adc channel 0. All the code is working correctly except im struggling how to detect peaks and calculate rms. A little pease of code would greatly help for any processor. thanks |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 04:50 PM |
|


Joined: Nov 22, 2002
Posts: 12052
Location: Tangent, OR, USA
|
|
Is the sine offset and scaled so that the minimum value is greater than zero and the maximum value is less than Vref?
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 05:14 PM |
|

Joined: Sep 22, 2007
Posts: 935
Location: Germany
|
|
If you want to calculate the RMS value, you don't have to find the peaks. Just calculate as Root Mean Square.
The calculation is actually tehh other way round: fist do the squares than average and then get the root. You probably have to remove the DC offest value before doing the square values. With a little math this can also be done after the squares are calculated. |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 07:10 PM |
|

Joined: Sep 12, 2006
Posts: 5
|
|
yes, vre- = 1.0v, which is 0 crossing.
vre+ = 2.0 volts (which is maximum value)
Is there any simple code i could use for this |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 07:32 PM |
|


Joined: Nov 22, 2002
Posts: 12052
Location: Tangent, OR, USA
|
|
Just sample starting on a zero crossing, and include the next two zero crossings.
Square each value and add it to an "accumulator". When you get done, divide by the number of samples (this computes the mean). Then take the square root. Subtract the equivalent ADC number associated with the zero level from the result. That gives you the RMS of the input in ADC units.
Then, just rescale this according to the reference voltage and input scaling and that will give you the RMS value in actual volts.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 08:15 PM |
|

Joined: Sep 12, 2006
Posts: 5
|
|
thanks Jim. Thats helpful. Just to clarify:
Is the zero crossing when ADC value of Vref- is subtracted from ADC value of Vref+?
Because i can read both voltages on different adc channels where as Vref+ is the input voltage and Vref- is 1.0v (which can aslo be read)
Or is there better way of fingding zero-crossing? |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 08:55 PM |
|


Joined: Nov 22, 2002
Posts: 12052
Location: Tangent, OR, USA
|
|
If you can measure it, then that removes errors involving assumptions about the ADC reference.
Sounds good to me.
You CAN, of course, subtract the offset immediately after the ADC measurement. This will give you a signed value and zero will be the zero crossing. Be aware that you may need to account for the fact that no measurement is exactly zero. You COULD look at the point where SIGN changes. Or, you could look at some voltage window centered about zero.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 09:36 PM |
|


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"
|
|
You could also rectify the signal with a precision rectifier (OP-AMP and a diode) and divide by 1.414 (two resistors) and get the RMS value direct.
Not having your spec as to how often you will sample the peak you will have to design the discharge time constant.
Some things can be done by hardware!
Lee |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 09:37 PM |
|

Joined: Sep 12, 2006
Posts: 5
|
|
Thank u Jim for your time and advice.
Just to elaborate on one final point when you find the zero crossing by subracting ADC value of Vref- from ADC value of Vref+, you are always going to get almost the same value for ADC vref+. Is that right? If so, then what is the point of measuring vref+ when you know zero crossing is going to occure when ADCVre+ = ADCVref-. |
|
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 10:29 PM |
|


Joined: Nov 22, 2002
Posts: 12052
Location: Tangent, OR, USA
|
|
But your processor does not know what ADCVref- is!
Even if YOU know what that voltage is, there will be errors due to the ADC reference not being exact and various resistors not being exact. So, it is very difficult for you to WRITE in the correct value. Much easier to just measure it.
Also, you will only rarely get a sample that is exactly AT the zero crossing. More often, one will be a little before and one will be a little after. You need a testing scheme that recognizes this fact. Requiring that ADCVre+ = ADCVref- will often fail because this will rarely be true (at most zero crossings).
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Mar 05, 2009 - 10:53 PM |
|

Joined: Sep 12, 2006
Posts: 5
|
|
| Thank you very much guys. |
|
|
| |
|
|
|
|
|
Posted: Mar 06, 2009 - 06:16 PM |
|

Joined: Sep 22, 2007
Posts: 935
Location: Germany
|
|
With the arerage value: you have to substract the square before you take the root.
For the zero crossings: you don't have to find them very accurately. Its just a significant advantage if the time for averaging is an itegral multiple of periodes. Since you know the frequeny before, the easiest way would be to average for something like 10 periods. The longer the time, the less important is the actuall lengh.
I don't see a big advantage in starting / ending at the zero crossing of the signal. The best time would probably be somewhere at an average level. |
|
|
| |
|
|
|
|
|
Posted: Mar 06, 2009 - 08:51 PM |
|


Joined: Nov 22, 2002
Posts: 12052
Location: Tangent, OR, USA
|
|
Correct. One can start anywhere, so long as an integer number of cycles is sampled. One of the challenges, no matter where you start, is determining when you have a full cycle. If the waveform is clean with no noise (no heavy equipment on that circuit, no light dimmers, etc), then it is not so hard. "Remember the first two samples" (or, equally, remember the first sample and the slope determined by the difference between the first two samples)(unless it happens to be very close to one of the peaks), then look for the Nth event (N even) where the sampled value is within some window of the first sample and the slope has the same sense.
If the signal is very noisy, then you will need serious low-pass hardware filtering OR serious numeric filtering before you do any comparisons, squaring, and so forth.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Mar 07, 2009 - 08:04 PM |
|

Joined: Sep 22, 2007
Posts: 935
Location: Germany
|
|
In many cases one knows the main frequency of interresst (e.g.5 Hz / 60 Hz /100 Hz /120 Hz). If you really want RMS this means you have to include the higher frequency components. So there should be no filtering, except for anialiasing. It may be usefull though to use some of the samples at the beginning and end with different weight.
With heasy noise, one should use more samples which measns more than one period. |
|
|
| |
|
|
|
|
|
Posted: Mar 07, 2009 - 11:08 PM |
|


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"
|
|
If I recall, the OP wanted the RMS of a 50 Hz. sinewave. I seemed to me that it is some sort of "power" related project, not a "complex" waveform analysis.
So far what has been described before is a very complicated solution to a very simpe problem.
(see my earlier post)
Why make it so complicated? Why level shifting, zero crossing detection, sampling, filtering etc.
When a diode, a cap ( throw in a op amp if you want precision) and just doing an ADC will do the job?
I know that a hardware solution, is not coding, but it is fast & precise and allows the processor get on with other things. Am I missing something here
Lee |
|
|
| |
|
|
|
|
|
Posted: Mar 08, 2009 - 07:34 PM |
|

Joined: Sep 22, 2007
Posts: 935
Location: Germany
|
|
The solution with a simple diode is not very precise, especially at small amplitudes. One will get a voltage that is about 0.5 V lower than the peak voltage. How much excactly depends on the temperature and current throug the diode.
Detection of the peak voltage is also sensitive to noise.
Doing the rms calculation by software needs less hardware and gives a more accurate result. Due to the larger number of uses samples resolution is much better than 10 Bits.
Edit: Just in case its to complicate to write the code, I have a version in C. |
|
|
| |
|
|
|
|
|
Posted: Mar 08, 2009 - 09:25 PM |
|


Joined: May 04, 2007
Posts: 3529
Location: Geelong Australia, Home of the "Cats"
|
|
Hi Kleinstein,
The OP, did not specify the order of accuracy that he required for his project which appears to be some "power control" project.
If you ar rectifying a very large voltage Ie >50 Volt RMS, the error is less then one percent and can probably be ignored. Any peak voltage can be externally scaled down to the range of the ADC (with a suitable clamp diode on the inputs of course)
For voltages less then 50 Volts, the error becomes more significant. My suggestion here was to scale the voltage and use a precision rectifier (using an op-amp which compensates for the 0.6 V drop because it is in the feed-back loop). This takes care of any variation in current & temperature as well.
http://en.wikipedia.org/wiki/Precision_rectifier will give you some refernce for this.
As far as harmonics & noise, if it is a sine wave , the harmonics (should be nil) and the noise should be
insignificant.
The OP, did not specify the order of accuracy that is required for his project, but my guess is that use of a precision rectifier will more then likely do the job for him for a power project.
My comments of course are all based on being a "power control project" and not a "power mains induction spectrum analyzer". The OP did not specify his project clearly enough.
Lee |
_________________ Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
(If you haven't already done so, edit your PostNuke profile and let let us know where you are, what you do & what your interests are.)
|
| |
|
|
|
|
|
Posted: Mar 10, 2009 - 04:18 AM |
|


Joined: Jun 06, 2008
Posts: 10
|
|
i did it this way.....
[code]
#define ROOT_TWO 1.4142135
#define ADC_REF_VOLT 5.001
float adc_ref_factor = (ADC_REF_VOLT / 1024);
// 10 bit ADC ---> 1024
float voltage, voltage_rms;
voltage = adc_average * adc_ref_factor;
voltage_rms = (voltage / (2*ROOT_TWO));
rgds,
jain |
|
|
| |
|
|
|
|
|
Posted: Mar 10, 2009 - 03:40 PM |
|


Joined: Feb 19, 2001
Posts: 25921
Location: Wisconsin USA
|
|
Now, you can do your "stuff" however you want to. But in general consider that:
-- You are starting with a 10-bit value.
-- With single conversion results and using the stated datasheet possible error sources, you have several bits of possible error so now you are down to 8 or 9 bits of accuracy.
-- Your whole analog subsystem has to be squeaky-clean, with no inaccuracies and no ripple, to achieve the above. Not true in practice, so you probably lose one or more additional bits.
[Now, with careful layout and calibration and oversampling and averaging/digital filtering one can get perhaps a good 10 or even 11 bits out of the AVR ADC. Let's say "near 10 bits" with a carefully-crafted setup.]
Summary of the above: You are starting with 3 decimal digits of precision.
Quote:
#define ROOT_TWO 1.4142135
#define ADC_REF_VOLT 5.001
float adc_ref_factor = (ADC_REF_VOLT / 1024);
// 10 bit ADC ---> 1024
float voltage, voltage_rms;
voltage = adc_average * adc_ref_factor;
voltage_rms = (voltage / (2*ROOT_TWO));
So what possible use could it be to do that calculation and report more than 3 digits of precision? And you've probably just taken a nice little app and blown it up to the size where it won't fit into a Mega48.
Should the divisor be 1024 or 1023?
Well, you say, that is not very constructive. OK, here is how I would do it: Use your constants above and find (with calculator) the final conversion factor from your ADC counts to voltage_rms. To keep in integer, let's calculate for millivolts so add another *1000 somewhere. Take that number, and find a nice small integer ratio that will do the calculation. I have a VB6 program that does this by brute-force, trying all logical combinations. It is rare to >>not<< find a small ratio that is not good to 4 decimal places, and typically 6 or 7 decimal places of accuracy between the float constant and the integer ratio.
In many cases you can find one with a numerator of 64 or less so only 16-bit arithmetic is needed.
Now you've done the conversion in maybe 100 words of code in maybe a few hundred cycles. Add another little bit for display adjustment but that will be less than for your floating-point result.
Lee |
|
|
| |
|
|
|
|
|
Posted: Mar 10, 2009 - 04:59 PM |
|

Joined: Sep 22, 2007
Posts: 935
Location: Germany
|
|
The limited resolution is true if you do an analog AC/DC conversion.
If you do the AC-RMS conversion by direkt reading the AC voltage, it is not difficult at all to get the analog part right. Due to the large number of samples (e.g. 1000) there is also significant oversampling. Except for very small AC voltages the samples are also not all close together. This way the resolution can be much higher than 10 bits. 12 resoluion should be possible without to much difficulty. |
|
|
| |
|
|
|
|
|
Posted: Mar 10, 2009 - 05:37 PM |
|


Joined: Feb 19, 2001
Posts: 25921
Location: Wisconsin USA
|
|
It is still a small (less than 16 bits) integer number, and then 3 1/2 digits. My comments still hold on avoiding the float work. (And also on having a layout that can actually guarantee AREF to a millivolt and no wiggle anywhere.)
Lee |
|
|
| |
|
|
|
|
|