Skip to content

Commit b683f99

Browse files
committed
Correct PWM frequency prescale computation
Assuming this datasheet https://www.digikey.be/htmldatasheets/production/1640697/0/0/1/pca9685-datasheet.html#pf19 is correct, the prescale value computation is off by one. Prior to this change a PWM frequency of about 2 kHz could be set, and would be reported. After this change a maximum PWM frequency of 1526 Hz is accepted and reported, consistent with the datasheet.
1 parent fcc8ced commit b683f99

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

adafruit_pca9685.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ def frequency(self) -> float:
166166
raise ValueError(
167167
"The device pre_scale register (0xFE) was not read or returned a value < 3"
168168
)
169-
return self.reference_clock_speed / 4096 / prescale_result
169+
return self.reference_clock_speed / 4096 / (prescale_result + 1)
170170

171171
@frequency.setter
172172
def frequency(self, freq: float) -> None:
173-
prescale = int(self.reference_clock_speed / 4096.0 / freq + 0.5)
173+
prescale = int(self.reference_clock_speed / 4096.0 / freq + 0.5) - 1
174174
if prescale < 3:
175175
raise ValueError("PCA9685 cannot output at the given frequency")
176176
old_mode = self.mode1_reg # Mode 1

0 commit comments

Comments
 (0)