I have a Raspberry Pi project (robot with wheels and servos) and I'd like to control both motors and servos at the same time. I'm trying to use PCA9685 + L298N for driving the motors.

The problem I'm facing is that it seems like the PCA9685 has s single clock, so I can only set PWM frequency once for all pins.

So far my understanding is that if I want to drive DC motors with PWM I need to set PWM frequency to at least a couple of 100Hz, let's say 500Hz

However if I want to drive a servo (SG90) it needs the clock to be set to 50Hz

Does it mean that I can only use the board either for servos or DC motors?

Are there better approaches for driving DC motors from RPi? I'd like to keep sound output while driving, so I don't want to use PWM pins from RPi directly :)
I'd like to keep CPU usage as low as possible, so also not sure if I should use software PWM for that.

  • \$\begingroup\$ All outputs of the PCA9685 operate at the same frequency, yes. Does the rpi not have built-in PWM functionality? \$\endgroup\$ – Hearth Oct 12 '18 at 2:18
  • \$\begingroup\$ @Felthry as far as I understand Rpi3 has 2 hardware PWM outputs, but 1 is shared with the audio output, so if I want to drive both motors and do the audio output (which is what I want) I would have to use a software PWM instead, which I'm trying to avoid. \$\endgroup\$ – Leonti Oct 12 '18 at 6:17

The PCA9685 is non ideal for driving servos as it provides very low resolution apart from the fixed output frame rate. But given the low price of the board you could easily run two of them (it supports multiple I2C addresses) and use a different frame rate for each board.

The frame rate for your servo signal on the SG90 should work out to at least 100Hz, and I've used the PCA9685 board from Adafruit to drive lots of servos and LEDs, and it's easy to program. There are of course manufacturer variations in the servos so YMMV. If you want to run the servo frame rate at 300Hz you will typically have to move to a Digital Servo, which most do to achieve faster command response. The MCU used in the digital servos will typically work with any frame rate from about 40Hz through to 300Hz. 300Hz would however seem to be low for your DC motor PWM drive requirements, so again I think two boards would be the best solution.

The biggest problem with the PCA9685 is the low resolution when driving a servo. It has a 4096 count span and if you are running your servos at 50Hz (20mS) that means your pulse range of 1 - 2 ms only has a count of 204 (less than 8 bit resolution), giving you about 100 point resolution above and below the 1.5ms center zero point. Some servos will work from 0.5ms through 2.5ms (and certainly most digital ones can be tuned for this) but this still only gives you a 406 point count which is less than 9 bit resolution.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.