Author Message
 ar
 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

 ka7ehk
 Posted: Mar 05, 2009 - 04:50 PM
 Joined: Nov 22, 2002 Posts: 12049 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

 Kleinstein
 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.

 ar
 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

 ka7ehk
 Posted: Mar 05, 2009 - 07:32 PM
 Joined: Nov 22, 2002 Posts: 12049 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

 ar
 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?

 ka7ehk
 Posted: Mar 05, 2009 - 08:55 PM
 Joined: Nov 22, 2002 Posts: 12049 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

 LDEVRIES
 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

 ar
 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-.

 ka7ehk
 Posted: Mar 05, 2009 - 10:29 PM
 Joined: Nov 22, 2002 Posts: 12049 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

 ar
 Posted: Mar 05, 2009 - 10:53 PM
 Joined: Sep 12, 2006 Posts: 5
 Thank you very much guys.

 Kleinstein
 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.

 ka7ehk
 Posted: Mar 06, 2009 - 08:51 PM
 Joined: Nov 22, 2002 Posts: 12049 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

 Kleinstein
 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.

 LDEVRIES
 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

 Kleinstein
 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.

 LDEVRIES
 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.)

 jainvj
 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

 theusch
 Posted: Mar 10, 2009 - 03:40 PM
 Joined: Feb 19, 2001 Posts: 25912 Location: Wisconsin USA

 Kleinstein
 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.

 theusch
 Posted: Mar 10, 2009 - 05:37 PM
 Joined: Feb 19, 2001 Posts: 25912 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

 Display posts from previous:  All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 Jump to: Select a forum Forum index|--[AVR (8-bit) Technical Forums]|   |-- AVR forum|   |-- XMEGA forum|   |-- AVR Wireless forum|   |-- AVR GCC forum|   |-- AVR Studio 5 and Atmel Studio 6 forum|   |-- AVR studio 4 forum|   |-- AVRfreaks Academy forum|   |-- AVR Tutorials|--[AVR Software Framework]|   |-- AVR Software Framework|--[AVR32 (32-bit) Technical Forums]|   |-- AVR32 Linux Forum|   |-- AVR32 General (standalone)|   |-- AVR32 Software Tools|   |-- AVR32 Hardware|--[General Electronics Technical Forums]|   |-- General Electronics|   |-- Atmel Security Products|--[Non-technical forums]|   |-- AVRfreaks.net Housekeeping|--[Non-topical forums]|   |-- Off-topic forum|   |-- AVRfreaks Trading Post
All times are GMT + 1 Hour