PWM Frequency Not Correct

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
jmtoys
 
Posts: 4
Joined: Tue Jul 05, 2016 10:52 am

PWM Frequency Not Correct

Post by jmtoys »

Hi All:
I'm a noob here, so I am probably missing something basic. I have the Adafruit 16 channel 12-bit PWM HAT connected to my RPi 3. I have loaded the recommended software, git clone https://github.com/adafruit/Adafruit_python_PCA9685.git. I tried the example simpletest.py, and it worked to move the servo, but the frequency seems to be off by about 14.5%. The frequency is supposed to be 60 Hz, but it is actually about 67 Hz as measured with my oscilloscope. I also tried a different frequency (1000 Hz) and it was also off by about 14.5% (actually measured 1147 Hz). Is there something that I am doing wrong?
Thanks,
John

User avatar
jmtoys
 
Posts: 4
Joined: Tue Jul 05, 2016 10:52 am

Re: PWM Frequency Not Correct

Post by jmtoys »

Follow-up:
I did some research on the PCA9685 that Adafruit uses. This IC is fixed to 12-bt operation. You also cannot preload the PCA9685 counter, which would be great since you could account for almost any error. So, no matter what you want to do, you will always end up with a prescaler that needs to be rational. Unfortunately, the prescaler can only be integer, and since you cannot preload the counter you will always end up with error in the PWM frequency. For example, if you want a freq. of 1000 Hz you would need to prescale the 25 MHz source by 25,000. Which is impossible since you divide by 4096. The refresh rate = 25 MHz / (4096 x (prescale + 1)). So, since you can never obtain a nice whole number for the refresh rate there will always be some kind of error in your PWM freq.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: PWM Frequency Not Correct

Post by adafruit_support_mike »

The PCA9685's frequency adjustments do have some granularity, yes. There will also be variation in the built-in 25MHz oscillator.

The good news is that most servos aren't all that precisely clocked either. They'll accept input within a range of frequencies.

You may need to tune the PWM frequency to get the one that works best with your servos, but shouldn't have too much trouble finding something that works.

User avatar
jmtoys
 
Posts: 4
Joined: Tue Jul 05, 2016 10:52 am

Re: PWM Frequency Not Correct

Post by jmtoys »

Thanks for the reply. You mentioned that I may have to "tune the PWM frequency". How can this be done? I did not see that as an option on the IC or your board. I thought the only tuning that you could do would be to use an external clock on the IC. Is there something that I am missing?

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: PWM Frequency Not Correct

Post by adafruit_support_mike »

Use the .setFrequency() method. That sets the PCA9685's clock divider.

User avatar
jmtoys
 
Posts: 4
Joined: Tue Jul 05, 2016 10:52 am

Re: PWM Frequency Not Correct

Post by jmtoys »

Hmmm..... If .setFrequency only configures the divider/prescaler, I don't think that would work. The problem is the granularity of the divider since it has a legal range of 0x03 to 0xFF, The ideal configuration would be to be able to preload the counter, essentially decreasing the number of bits of the counter. For example, if the counter could range from 95 to 4095, we would have a counter of only 4000. 4000 is a much easier number to work with when the source clock is 25 Mhz. If that was possible the equation for determining resolution would change to 25 Mhz / (4000 * (prescale +1)). For a 50 Hz servo frequency we would end up with a whole number for the prescaler of 124. I know that the PWM module is supposed to be perfect, but it would be nice to have more control over the counter.

I did do some more testing and found a way to generate better frequency accuracy. In order to get as close as possible to frequencies I need, I had to use different numbers when I set the PWM frequency. For example, if I need the freq. of 50 Hz, I set the freq to 45 (pwm.set_pwm_freq(45)). This generates a freq. of 50.5 Hz. For 60 Hz frequency I need to write 53 (generates 59.8 Hz). Obviously, as the frequency gets higher the error will increase. For a frequency output close to 1000 Hz a value of 935 is written, which generates 982 Hz. An error of about 2%.

Sorry for all of the whining, I was hoping to have a more accurate PWM available. I will update what I am doing with a HC9S12C32 MCU from NXP, which allows for a much more accurate PWM.

Thanks again for the help.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”