kramer65
Posts: 33
Joined: Mon Mar 18, 2013 2:59 pm
Location: Amsterdam

How to get motor running with this H-bridge?

Fri Mar 03, 2017 2:29 pm

A couple years back I bought a simple H-bridge to run and reverse a small DC motor from my Raspi. That worked fine but since the motor was not powerful enough to run what I wanted, I recently bought a stronger motor and the following H-bridge which can actually handle 24V and up to 43A:

Image

Unfortunately there aren't really tutorials to get this thing running with the Raspi. So I got to work myself.

I started by testing if the motor works if I hook it up directly to the battery. That works fine.

Then I had a good look at the H-bridge. There are 4 big wire inputs, marked with B-, B+, M+ and M-

Image
From this tutorial I understand that the B and the M stand for Battery and Motor. So I hooked them up to my battery and motor.

Furthermore, there are 8 small pins on the H-bridge, which are

Code: Select all

VCC    R_IS    R_EN    RPWM
GND    L_IS    L_EN    LPWM
From this diagram I found that the left two (VCC and GND) are 5V power supply and ground. So I hooked them up to a 5V and ground pin on my Raspi.

And from this point on it gets tricky. I guess that:
- R_EN and L_EN are used to ENable the motor to turn Right or Left (correct?)
- RPWM and LPWM are used to determine the motor speed using Pulse-width modulation (correct?)
- I have no idea what R_IS and L_IS are for. Does it have something to do with measuring current flowing through?

So if the above is true. I suppose that I can hook up R_EN to a normal GPIO pin and write HIGH to it and at the same time send a 100% pulse width to RPWM to get it to turn right.

Following this logic I hooked up the wires as follows:

Image

and then wrote the following code:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

R_EN = 11
RPWM = 12
GPIO.setup(R_EN, GPIO.OUT)
GPIO.setup(RPWM, GPIO.OUT)

GPIO.output(R_EN, True)
p = GPIO.PWM(RPWM, 100)  # inspired by https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/

try:
    while True:
        p.start(100)  # duty cycle of 100%
except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()
Unfortunately, nothing happens, at all. No errors, no movement, nothing.

So I've been trying about a zillion combinations with code and wires, including using WiringPi with Python bindings. And I saw it moving once, for just a fraction of a second. But despite the effort I put into reproducing what I did, it never moved again. I checked the battery directly on the motor again, and that still works, so my battery is still alive.

So from here I'm kinda lost. Since I'm more of a software man I don't really know how to debug hardware.

Can any brighter mind than me give me some tips and hints on what I'm doing wrong or how I can debug this? All tips are welcome!

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6672
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: How to get motor running with this H-bridge?

Fri Mar 03, 2017 4:36 pm

http://www.hessmer.org/blog/2013/12/28/ ... h-arduino/
http://forum.arduino.cc/index.php?topic=148482.0
1. I think you need to set both ENL and ENR HIGH.
I'd be a little cautious too as to whether you should be feeding 3.3V or 5V to Vcc. With 5V the H-bridge will be expecting 5V logic signals when the Pi only generates 3.3V. You may get away with it, but it may behave erratically.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

kramer65
Posts: 33
Joined: Mon Mar 18, 2013 2:59 pm
Location: Amsterdam

Re: How to get motor running with this H-bridge?

Sat Mar 04, 2017 1:27 pm

Thanks for your tip! In the end I got it to work with only regular GPIO pins and the following code:

Code: Select all

import RPi.GPIO as GPIO  # sudo apt-get install python-rpi.gpio

class Driver:
    def __init__(self):
        GPIO.setmode(GPIO.BOARD)
        self.R_EN = 21
        self.L_EN = 22
        self.RPWM = 23
        self.LPWM = 24
        GPIO.setup(self.R_EN, GPIO.OUT)
        GPIO.setup(self.RPWM, GPIO.OUT)
        GPIO.setup(self.L_EN, GPIO.OUT)
        GPIO.setup(self.LPWM, GPIO.OUT)
        GPIO.output(self.R_EN, True)
        GPIO.output(self.L_EN, True)

    def neutral(self):
        GPIO.output(self.RPWM, False)  # Stop turning right
        GPIO.output(self.LPWM, False)  # stop turning left

    def right(self):
        GPIO.output(self.LPWM, False)  # stop turning left
        GPIO.output(self.RPWM, True)  # start turning right

    def left(self):
        GPIO.output(self.RPWM, False)  # Stop turning right
        GPIO.output(self.LPWM, True)  # start turning left

    def cleanup(self):
        GPIO.cleanup()
And in an interactive command line I can then simply do this:

Code: Select all

>>> from motorlib import Driver
>>> driver = Driver()
>>> driver.right()  # turns right
>>> driver.left()  # turns left
>>> driver.neutral()  # stops turning
>>> driver.cleanup()
Another step in building my world domination robot :D

EscoR
Posts: 1
Joined: Sat Mar 03, 2018 9:26 pm

Re: How to get motor running with this H-bridge?

Sat Mar 03, 2018 9:50 pm

Hi, I know the thread is a bit older, but it's quite difficult to get information about the Raspberry pi in connection with the BTS7960 43A.
I plan to drive a wiper motor (Bosch 9169321) as soon as the Ir-sensor LM393 detects something.
Unfortunately I'm still waiting for the delivery of my power supply, so I can't check if my schematic works like this.
As far as I understood kramer65's comment, the following wiring has been done.

BTS7960 | Raspberry pi
Vcc | +5V
GND | GND
R_EN | GPIO21 (pin 40)
L_EN | GPIO 22 (pin 15)
RPWM | GPIO 23 (pin 16)
LPWN | GPIO 24 (pin 18)

It would be very nice of you to help me out.
Maybe you can post your circuitplan.
A simple "hey that looks very good" is also enough for me:).

http://www.bilder-upload.eu/show.php?fi ... 113760.jpg

Return to “General discussion”