STOP RUSSIAN WAR OF AGGRESSION ON UKRAINE

Instruction – How a L298N H-Bridge is controlled by a PCA9685 servo controller

Instruction – How a L298N H-Bridge is controlled by a PCA9685 servo controller

Build your own robot car with the Raspberry Pi!

Build your own robot car with the Raspberry Pi!

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,407 times, 1 visits today)

About The Author

Maker

My name is Ingmar and I build Raspberry Pi robots in my sparetime. I wrote a book about Raspberry Pi robots 2016 which is available in German. I am interested in all kinds of electronic projects like differential GPS, machine learning or robotics.

7 Comments

  1. Michael Harris

    why are you using a logic level converter since you can run the pwm controller at 3.3v, the 5v pin on the motor controller can be connected directly to the 5v pin on the pi, since the pwm controller is run at 3.3v the i2c input would be received at 3.3v. I am curious about this, maybe I am missing something . I have the same problem that you were having, with my 6 wheel drive robot I was using 3 L298n motor controllers and I was not getting enough amperage to rotate my robot. It turns just fine but it wont spin so I ordered 2 of the BTS7960B motor Controllers and i hope that it will fix my problem.

    Reply
    • Maker

      Hi Michael,
      two BTS7960B motor controllers are very powerfull. I use them in one of my robots and in combination with the PCA9685 the setup is perfect and cheap.
      Maybe you have a few pictures from your robot you are willing to share with my blog. I will upload them in the hall of fame if you like…
      I power the PCA9685 with 3.3V only for securitiy reasons not to get a too high voltage (5V) back on the I2C bus on the Raspberry Pi side. That would damage the Raspberry Pi SBC.
      Best Regards,
      Maker

      Reply
  2. Michael Harris

    I am using 6 313 RPM HD Premium Planetary Gear Motor that run at .52A @ no load and 20A @ stall and 6 5.4 inch, 137mm off road tires

    Reply
  3. Michael Harris

    why do you use the logic level converter with the L298N H-BRIDGE IS CONTROLLED BY A PCA9685 since the PCA9685 can be pwered from 3v to 5v and if you 3.3v the return to the I2c buss will be at 3.3v. the servo power supply is only a output and the voltage will vary on how much power your servo or any outher PWM device uses.

    Reply
    • Maker

      Hi Michael,
      The time I wrote the post about the PCA9685 connected to a Raspberry Pi I2C bus I connected the controller to 3.3V via a level shifter to be on the safe side.
      This setup hopefully also secures my readers with not such a deep knowledge about electronics protect against an over voltage on the I2C bus.

      But you a right the PCA9685 works with 3.3V and 5V

      Maker

      Reply
  4. Virgil

    This is not a complaint, but still I’m wandering why you using I2C board if you can just add 2 more GPIO pins and get the same effect?
    For instance per each motor:
    2 x EN for direction
    1 x PWM for speed

    The main Idea of using i2c is to reduce usage of the GPIO pins to left it for other purposes of the project.

    May be you can advice how to get read of GPIO pins at all, just to use i2c for managing the motors?

    Reply
  5. Luis Angel Bustamante Palacio

    Hello, I am doing a project in which I have a somewhat similar hardware architecture but I am not very clear on the subject of connections and I was wondering if you could provide me with some information. Mainly on the subject of connections between the cards and the DC motor.

    Raspberry pi 3
    Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi
    Driver L298N
    Machifit 25GA370 DC 6/12V 130rpm

    Software: Codesys

    Thank you

    Reply

Leave a reply

Your email address will not be published.

Book recommendation:

Build your own robot car with the Raspberry Pi!

Build your own robot car with the Raspberry Pi!