measuring ac signal with microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by aamirali, Jul 4, 2014.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I have 60hz ac signal with 5 Vp-p. i.e +2.5V peak & -2.5V peak.
    This signal is riding over 2.5V dc.


    So at micro-controller pin voltage varies from 0 to 5V with 60Hz.

    I have measured dc voltages many times but not ac varying voltages. At different intervals measured voltage will be different. So how to measure.


    What samling frequency should I choose. should it be twice the signal frequency. i.e 120Hz ?

    Should I take multiple samples & average them to measure peak voltage??
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    How you measure will depend on what parameter is important to you.

    Are you interested in the peak value? Average value? Frequency value? Distortion? True RMS value? True wave shape?

    Two things to note: If your 5 Vpp wave ever becomes 5.1 Vpp how will you ever know? You've maxed out your range as you have it. Best leave some room for expansion.
    +
    - Next, it may be very convenient to make a zero crossing detector by greatly amplifying the signal or sending to a comparator. That signal at an interrupt pin gives you the ability to synch your measurement to the signal.
    + Then synch your measurement time to the signal and finding the peak is just one measurement whenever you need an update.
     
    Last edited by a moderator: Jul 8, 2014
    absf likes this.
  3. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    i want to measure peak value.
    I have already kept the margins in voltage range.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Then synch your measurement time to the signal and finding the peak is just one measurement whenever you need an update.
     
  5. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,394
    497
    You have a lot of choice for sampling frequency. The usual textbook way is that the MINIMUM sampling frequency should be 2x of the signal being sampled. In your case you are sampling 60 Hz signal so the MINIMUM sampling frequency should be 2x60 Hz=120 Hz. But the key here is that the sampling frequency is not required to be 120 Hz. 120 Hz is the MINIMUM. You are not allowed to go below 120 Hz. But you can go higher! So if you don't find ADC that has 120 Hz sampling frequency, you can go 180 Hz, 240 Hz, 300 Hz, etc. You can go as high as you like/want/can get parts for.

    Since the signal is AC, I think I would take samples with ADC, store the results in an array, then run the results through sorting algorithm to find the highest value. The question becomes:
    * How many sample to take? 10? 50? 100?
    * How many highest value samples do I want to keep to do the average?
    Note that you don't have to do the average. If you collect enough samples, one or more of them should be close enough to 5 volts to tell you that you have 5 volt wave.
     
    Last edited: Jul 6, 2014
  6. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    If you assume that you have a pure sine wave, you can measure the peak and then divide by square root of 2. Cheap multimeters do it that way.

    More expensive multimeters caluculate so called RMS (root mean square). That's why such devices are often called "True RMS multimeters". They sample at high frequency, take a square of each measurewment, average these squares, then take a square root of the avearage. That's why it's called "root mean square". Good sampling frequencies are somewhere from 3kHz to 100kHz. Of course, more frequent measurements will give you better precision.
     
  7. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I need to further clear this point.
    I have 60Hz signal with ac signal +-2.5V riding over +2.5dc offset.
    So MCU will see 0-5V ac signal with 60Hz freq.

    I am using PC12f675 with internal RC oscillator & have Tad = 4us (typical A/D clock source time).

    How many samples I need to take?
    In how much interval, i.e difference between I should take reading.
    Any example code where this measurement has shown
     
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Sampling at 10 times the sine frequency will give a pretty good result.

    Since your freq (60Hz) is so low, personally I would sample at 20 times (60*20 = 1200Hz) which will increase accuracy by a tiny amount.

    You still need to explain WHAT you want to measure (ie what the goal of measuring is), because if we know that we can suggest the best ways to do it.
     
  9. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I need to measure the peak voltage i.e +2.5V.

    Problem is ac signal varies everytime. So everytime I read it will give different result.
    I don' know what algo should I apply to read the peak value?? Any example code.

    Circuit which I am using is attached.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    If you connect the junction of R3 and Rct to a comparator input you get a synch pulse. You already made a 1/2Vcc reference for comparison. That gives you a zero crossing reference to synch your signal. (See Note 1)

    If you have a synch signal you can measure the time between two zero cross times and use that to predict when the next peak will occur.

    If you know when the peak is going to occur you can take one measurement at that time and you have your peak signal.

    This is similar to how one measures the peak of a waveform on an oscilloscope. And you can take this measurement each and every peak for 120 times a second, and always have the last instantaneous value.

    Note 1: if a comparator input is not available then one can use an external comparator and a digital input.
     
  11. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305

    There's no need to do zero-cross sensing if you sample at a slightly faster samplerate.

    Also, zero cross to detect the "peak" wil not work well if this is sensing mains AC before or after a transformer. The mains AC waveform has deformed peaks. My AC mains here is flattened on the top, and the peak is 10 degrees before where it should be (the flattened top slopes down to the right).

    Here's an algorithm to give the peak to peak;
    1. take 20 samples over a 60Hz mains cycle (one sample every 833uS).
    2. find the largest and the smallest sample, these are the + and - peak values
    3. peak to peak distance = (largest sample - smallest sample)
    4. use ratio scaling to convert the result to volts if needed
    :)
     
    Last edited: Jul 9, 2014
    aamirali likes this.
  12. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,394
    497
    1. Take sample with ADC.
    2. Compare sample to previous sample.
    3. Store the highest sample.

    This is C for Engineers type class material. If you are in college/uni, you should have had it in your second year of four year program.
     
  13. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Looks like you're measuring mains current. For most real life loads, it doesn't look anything like a sine wave. Therefore, your peak measurement will be totally useless.

    You need to measure RMS. Sample often, calculate an average of squared values, then take square root.

    Do you only measure current, or do you want overall power? What kind of loads do you have?
     
  14. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,394
    497
    The signal is from 0 to 5 volts.

    The signal is in volts.

    Did I miss the part where the signal changed to 110 volts (mains) and turned into current instead of voltage?
     
  15. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    It is exact opposite. Current is turned into voltage with CT transformer. Schematics in post #9.
     
  16. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,394
    497
    Ok. I saw a text file and did not look inside it. I have looked now.
     
  17. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I am able to detect peak voltage.

    As mentioned in post #6
    1. Now one method is to find RMS by dividing peak value by square root of 2.
    2. More accurate by RMS method.

    Assume:
    taking 20 samples over a 60Hz mains cycle (one sample every 833uS).


    Query:
    To implement RMs, do I need to square all these 20 samples & average & then take root??
     
  18. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    You are misinterpreting the Nyquist Criterion. Sampling at > 2x the max frequency component is a requirement only if you intend to accurately digitally represent the original analog waveform.

    Computing a peak, average, or RMS of an analog waveform, absolutely does not require sampling greater than 2x, especially if you have prior knowledge of what the waveform looks like!

    For example, if you know (in advance) you have a pure 60 hz wave form, and you know where the zero crossing is, selecting 1 single data point at the correct time is adequate for computing peak, average, and RMS (neglecting noise!).

    Alternatively, if you have a 60 Hz waveform that varies only slowly through time, you can select fewer data points distributed over multiple periods, each data point skewed slightly forward or backward in time from the previous, and mathematically compute a peak, average, or RMS. In this way, no zero-crossing knowledge is required, and the processing overhead is greatly reduced.

    Before you object, I have implemented the second case -- with 50/60 hz with lots of harmonics -- and it works quite well.

    Edit: Apologies to Lestraveled. I didn't see this post prior to my comment. I guess great minds think alike!
     
    Last edited: Jul 11, 2014
Loading...