Try to find the RMS of an input sine wave using a microcontroller

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Minda
Posts: 1
Joined: Tue Jan 23, 2018 7:25 am
Contact:

Try to find the RMS of an input sine wave using a microcontroller

Post by Minda »

I am trying to find the RMS of an input sine wave using the ARM 7 LPC2119 micro-controller(http://www.kynix.com/Detail/299612/LPC2119FBD.html). I know the theory behind finding the RMS of such a waveform but I am having trouble implementing this in code, especially the sampling at equally spaced mid-ordinates. I also have just used timer interrupts to find the frequency of a sine wave using capture inputs, so I am aware of the basic functionalities in terms of programming an ARM 7 LPC 2119 micro-controller.

Image

I am testing this functionality using a 3 V peak-to-peak sine wave with a frequency of 50 Hz. I have shifted the waveform upwards by 1.5 V so to avoid having any negative values going into the ADC pins of the micro-controller.

To sample at equally spaced intervals I am using timer interrupts, In a very similar way to this example here. Instead of switching on an LED, I am doing an ADC conversion.

Before using the following formula:
Image

I need to make sure that I am sampling at given intervals, and to test this I have set my timer clock to provide a 20ms delay before entering the ADC function by doing so:

Code: Select all

T0CTCR = 0x0; //Set Timer Mode
T0PR = 60000-1; //Increment T0TC at every 60000 clock cycles
//60000 clock cycles @60Mhz = 1 mS

T0MR0 = 21-1;   //Zero Indexed Count-hence subtracting 1
T0MCR = (1<<0) | (1<<1);//Set bit0 & bit1 to Interrupt & Reset TC on MR0  
Once this is done, I am enabling my timer interrupt:

Code: Select all

  VICVectAddr0 = (unsigned )timer0ISR; //Pointer Interrupt Function (ISR)
  VICVectCntl0 = (1<<5) | 4; //(bit 5 = 1)->to enable Vectored IRQ slot 
  VICIntEnable = (1<<4); // Enable timer0 interrupt
The above code gives a 20 ms delay. With a sine wave at 50 Hz, this should give the same value on every reading, since the period of such a sine wave is also 20 ms. However this is not the case and I can see no visible pattern in my results.

The ADC function I am using works well on DC voltages and I can also confirm that the timer is indeed giving a 20 ms delay before entering the ADC function.

Am I missing something obvious here or could there be some other variables I am not taking into consideration? Any ideas would be appreciated.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: Try to find the RMS of an input sine wave using a microcontroller

Post by QMESAR »

Hi ,

I am just shooting out of the shoulder here
However 50Hz sine = 20msec 1 complete cycle positive + negative halve so my guess is sampling at 20mSec you possibly have 1 sample and depending where it happens it could be on the zero point so not much sample to get a RMS from, secondly you need to sample at least at Nyquiest which is min 2 time the Frequency so in your case at 100Hz sampling you might start to see something
so 100Hz = 10msec = 5mSec per half wave so about 2 samples at 50Hz signal .push the sample rate higher my guess would be 1K samples and you will get the RMS properly

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Try to find the RMS of an input sine wave using a microcontroller

Post by Benj »

Hello,

If it were me doing it then I would probably do something along the lines of this, assuming you are working with a pure sine wave.

Sample at least 12 x the frequency so at least 600Hz. The faster you sample the more likely you are to find the very peak of the wave.

Take a number of samples over say a full period and find the largest value. Multiply this largest value by 0.707 to give you the RMS value.

Repeat, maybe filtering the RMS with a low pass filter so that spurious values are filtered out.

Here is a simple low pass filter calculation you can use.

NEWRMS = LargestReading * 0.707
FILTEREDRMS = (0.1 * NEWRMS) + (0.9 * OLDRMS)
OLDRMS = FILTEREDRMS

If the Sine wave is not a pure sine then the 0.707 rule no longer applies and you probably have to go back to the calculations you were using.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Try to find the RMS of an input sine wave using a microcontroller

Post by Steve001 »

Hello,

Is this application note any help?

Steve
Attachments
Accurate and simple AC Messurement.pdf
(161.39 KiB) Downloaded 476 times
Success always occurs in private and failure in full view.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Try to find the RMS of an input sine wave using a microcontroller

Post by Steve001 »

another here ..

Steve
Attachments
an106f Instrumentation Circuitry Using RMS-to-DC Converters .pdf
(345.54 KiB) Downloaded 358 times
Success always occurs in private and failure in full view.

Post Reply