Shop OBEX P1 Docs P2 Docs Learn Events
Measuring PWM duty cycle with Analog Input — Parallax Forums

Measuring PWM duty cycle with Analog Input

QuadrtrFlyrQuadrtrFlyr Posts: 73
edited 2013-06-06 09:08 in Propeller 1
Hello folks,

I was wondering if there was a way to measure the duty cycle of a PWM signal with an analog input pin? I have found a lot of examples with digital inputs and know it can be done but I would really like to know if this is possible with an analog input?

Thank you,
Bob

Comments

  • jmgjmg Posts: 15,173
    edited 2013-06-05 15:27
    I was wondering if there was a way to measure the duty cycle of a PWM signal with an analog input pin? I have found a lot of examples with digital inputs and know it can be done but I would really like to know if this is possible with an analog input?

    Sure, there are a few ways to do this.
    a) Filter the PWM well, and measure the average DC - this also works at 0% and 100%
    b) On a prop, if this is someone elses PWM, you can use the gated timer feature, and capture a whole number of cycles.
    DutyCycle is GatedTimer/ElapsedTime
    Usually this needs an edge to sync on, so does not work without adding cases for 0% and 100%
  • QuadrtrFlyrQuadrtrFlyr Posts: 73
    edited 2013-06-06 08:45
    jmg wrote: »
    Sure, there are a few ways to do this.
    a) Filter the PWM well, and measure the average DC - this also works at 0% and 100%
    b) On a prop, if this is someone elses PWM, you can use the gated timer feature, and capture a whole number of cycles.
    DutyCycle is GatedTimer/ElapsedTime
    Usually this needs an edge to sync on, so does not work without adding cases for 0% and 100%

    Interesting, but honestly I don't understand the math behind how this happens. Can anyone explain this in more detail?

    Regards,
    Robert
  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-06 09:05
    It's not a math thing. Focus on choice (a). PWM is just a digital signal that spends some percent of the time in an on state (and the rest of the time in an off state). What you want to do is average this over a period of time to get a % on time. The period of time depends on the pulses and how they're generated. For example, RC servo control pulses are PWM that repeat at roughly 50 times a second and, in a given 20ms window, are on roughly 1 to 2ms for a duty cycle of 5-10%. PWM used for DC motor control is usually at a much higher frequency and covers most of the 0-100% duty cycle range. To measure any of these, you have to use analog circuitry (a filter) to average the on time over a period of time. Typically, you charge a capacitor through a resistor when the PWM signal is on and measure the voltage on the capacitor after a known time period. The result is proportional to the % on time. You can also charge (on time) and discharge (off time) the capacitor through a resistor and get a voltage on the capacitor proportional to the ratio of on to off time. See the Wikipedia for a discussion of RC time constant.

    (b) is really a digital technique, not analog.
  • Heater.Heater. Posts: 21,230
    edited 2013-06-06 09:08
    If you want to measure the pulse width, as in PWM, then simply filtering the signal to get an average DC level is not going to tell you anything interesting.
    A typical servo uses a 1 to 2 ms pulse repeated from 50 to some hundreds of times per second. Clearly the average of that depends not just on the pulse width put also the repetition frequency. I guess if you know your repetition is actually 50Hz, or whatever, then the average DC level will enable you to calculate the pulse width. But then you have to be sure you know the high and low voltage levels accurately as well. Given the ease of measuring time digitally this seems overcomplicated.
Sign In or Register to comment.