Some Observations on PWM Output - both via GPIO and PWM Expansion
-
I have the potential need for a consistent, reliable source of frequency output from my Omega.
I considered the possibility of using PWM output (either GPIO or PWM Expansion).
My initial investigations indicated that the frequency (and duty cycle - though this is not of high importance to me) were not all that accurate.So, I investigated the range and accuracy of 3 different methods of producing PWM output.
- Use of fast-gpio
- Use of my new-gpio alternative to fast-gpio
- Use of pwm-exp with a PWM Expansion
I attach a spreadsheet that summarises pertinent figures: PWM Performance.xlsx
A summary of my findings is:
-
For fast-gpio:
- Actual frequency is lower than requested frequency - the effect being greater for higher frequencies
- Upto a 200Hz or so, the duty cycle is reasonably (but not highly accurate) - gets worse with higher frequencies
- Can't really cope with requested frequencies above about 700Hz
This is all to be largely expected since fast-gpio uses software to drive the GPIO in
-
For new-gpio results are almost identical to that for fast-gpio - to be expected since new-gpio uses very similar techniques to fast-gpio for PWM output
-
For pwm-exp:
- Actual frequency is consistently a bit higher than requested frequency
- Lowest actual frequency attainable is 24.72 Hz, highest is 1582 Hz - the limit on the upper frequency is to be expected based on the documentation of the PWM Expansion
- There seems to be a high degree of granularity in the actual frequency - I estimate 6 bits of accuracy giving 64 discrete steps - though the steps don't seem to be linear
- Over the actually achievable frequency range, duty cycle appears to be reasonably accurate
This is all reasonably commensurate with the capabilities of the PCA9685 chip used in the PWM Expansion other than the granularity of he frequency output. The specs for PCA9685 indicate that the on and off periods can be set with a resolution of 12 bits each, so I would expect that the pwm-exp software could set the frequency to this accuracy - perhaps this is a shortcoming in the pwm-exp software?
Finally, as an aside, I have produced an I2C based source of frequency output (50% duty cycle only) using http://www.gravitech.us/i2c1kto68pro.html - this is programmable for frequencies from 1KHz to 68MHz - along with a 10bit counter, this allows me to set up frequencies from 1Hz to 68MHz depending on the counter connections
-
@Kit-Bishop very thorough! nice!
I'll take a look at the pwm-exp code, I remember there was some equation required to program the crystal frequency.
-
@Lazar-Demin I have had a look at the pwm-exp code and can see nothing wrong with it.
The granularity I observed in the output frequency is purely a factor of the characteristics of the PCA9685 chip.
The actual output frequency used is set by setting the prescale value for the chip.
According to the docs and the pwm-exp code, the prescale value for any required frequency is set by:
prescale = 25000000 / (4096 * frequency) - 1Reorganizing this gives:
frequency = 25000000 / ((prescale + 1) * 4096)This equates to:
- prescale=3 gives frequency = 1526
- prescale = 4 gives frequency = 1221
- etc, etc
This matches closely the figures I reported on granularity
Thus, nothing wrong with the code just a feature of the chip
-
@Kit-Bishop Yeah, I remember writing that part.
I went back and double-checked that there wasn't an extra rounding (missing cast to float) that might have been eating up some of the precision. But the code looks clean