Measuring PWM duty cycle with Analog Input
QuadrtrFlyr
Posts: 73
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
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
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
(b) is really a digital technique, not analog.
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.