Electronic – arduino – Setting the PCA9685 PWM module prescaler

arduinoi2cpwm

I'm trying to make a 60Hz frequency, this is How I do it :

    freq_hz = 60
    pca9685_frequency = 25000000.0 #pca9685 clock : 25Mhz
    pca9685_resolution = 4096.0 #12 bits resolution
    freq_in_step = pca9685_resolution*float(freq_hz)
    prescaleval = pca9685_frequency/freq_in_step
    print "prescaler value = {}".format(prescaleval)
    prescale = int(math.floor(prescaleval + 0.5))
    print "however only round values can be set so we wil use {} as a prescaler value".format(prescale)
    print "which makes a frequency of {}".format(pca9685_frequency/(prescale*pca9685_resolution))
    oldmode = self.pca9685_device.readU8(self.MODE1);
    newmode = (oldmode & 0x7F) | 0x10    # sleep
    self.pca9685_device.write8(self.MODE1, newmode)  # go to sleep
    self.pca9685_device.write8(self.PRESCALE, prescale)
    self.pca9685_device.write8(self.MODE1, oldmode)
    time.sleep(0.005)
    self.pca9685_device.write8(self.MODE1, oldmode | 0x80)

On the screen the result is :

prescaler value = 101.725260417

however only round values can be set so we will use 102 as a prescaler value which makes a frequency of 59.8383884804

But with my oscilloscope, a Tektronix TDS210 I get a 64.1Hz frequency, not 59.84Hz

Did I make a mistake somewhere?

https://www.nxp.com/docs/en/data-sheet/PCA9685.pdf

Best Answer

From old awnser of the adafruit forum, the PCA9685 is not very precise. So you have to manually check and adapt the prescaler value.

My math are correct according to page 25 of the datasheet and 102 should indeed generates a frequency of 59.84Hz.

However my oscilloscope measurement gives me this :

110 = 60.24Hz
111 = 59.52Hz

TL:DR ; due to the low precision of this component the prescaler computation only gives you a close estimate, you will have to measure the real thing with an oscilloscope.

see second post : https://forums.adafruit.com/viewtopic.php?f=19&t=72554

Blue = real, measured with my oscilloscope

Red = from the datasheet formula

enter image description here