In the past I build several Raspberry Pi robots with a L298N Dual H-Bridge. With the L298N motor driver the Raspberry Pi is able to control the spinning direction and speed independently of two DC motors. But the problem for the Raspberry Pi is the generation of a clear PWM signal. From time to time I see the motors not spinning correct because of the software generated PWM signal by the Raspberry Pi. To generate a clear and constant PWM signal I decided to buy the cheap Adafruit PCA9685 servo controller board. With this board it is easy to generate a PWM signal for up to 16 servos.

The idea was to use a L298N H-Bridge connected to the PCA9685 board instead of a servo and to control the speed of two DC motors with the PWM signal generated by the PCA9685 servo controller.

The picture below shows the setup with a L298N H-Bridge, a Step-Down converter, the servo controller, an I2C level-shifter, a Raspberry Pi 3 Model B and two DC motors.

L298N H-Bridge with a PCA9685 overview

L298N H-Bridge with a PCA9685 overview

List of components

This is the list of components I used for my setup.

  • PCA9685
Loading ...
  • Step-Down converter
Loading ...
  • L298N H-Bridge
Loading ...
  • I2C level shifter
Loading ...
  • Raspberry Pi 3 Model B
Loading ...
  • Two DC motors
Loading ...
  • Female to female jumper
Loading ...
  • Wires
Loading ...

Software PCA9685 Adafruit library

All the programs you need to control the DC motors are written in Python. First you need to activate the I2C Bus on your Raspberry Pi and to install the Adafruit PCA9685 library.

Adafruit PCA9685 library

To install the Adafruit library for the servo controller visit the GitHub page from Adafruit or just execute the following commands each after the other in the terminal window on your Raspberry Pi. I used for my setup the Raspbian Jessie.

Adafruit GitHub URL: https://github.com/adafruit/Adafruit_Python_PCA9685

Commands to install the Adafruit PCA9685 library:

Command: sudo apt-get install git build-essential python-dev

Command: cd ~

Command: git clone https://github.com/adafruit/Adafruit_Python_PCA9685.git

Command: cd Adafruit_Python_PCA9685

The next command is optional instead of the four commands before.

Command: sudo python setup.py install

After installing the library and wiring everything up you could check if the servo controller is available on the I2C bus. Execute the following command and you should see the PCA9685 servo controller with the address 40.

Command: sudo i2cdetect -y 1

Now you schould see the servo controller with the address 40 on your I2C bus.

i2c detect bus result

i2c detect bus result

Now you could download the two Python programs I developed to control two DC motors with a L298N H-Bridge and a PCA9685 servo controller.

With the “RobotControlPCA9685.py” program you control the speed and spinning direction of the two DC motors via your keyboard. Start the program in the terminal window.

Download: Robot Control PCA9685 Python program

The Python module “L298NHBridgePCA9685.py“encapsulate the logic to speak with the Adafruit servo controller and with the L298N H-Bridge. This module is imported by the “RobotControlPCA9685.py” program.

Download: L298N H-Bridge PCA9685 Python module

The software is available on GitHub too: https://github.com/custom-build-robots

The next picture shows the setup from another perspective.

L298N H-Bridge with a PCA9685 wiring

L298N H-Bridge with a PCA9685 wiring

Video

The video shows the two DC motors spinning.

Summary

This setup works perfect for complex robots like my Big Rob robot I build. With the PWM signal generated by the PCA9685 servo controller I am now able to control the speed of DC motors with an H-Bridge very accurate. The setup I build is very cheap compared to motor drivers which have an I2C interface on board.

(Visited 9,955 times, 1 visits today)