Raspberry Pi - BTS7960 Motor Driver

Picture a heavy rover platform: a metal frame, big 12V gear motors, and a battery that can deliver many amps. A Linux program on the Raspberry Pi decides when the rover drives, turns or stops. But a GPIO pin only gives a weak 3.3V signal, and a big motor would destroy it in a moment. A strong driver must stand between the Pi and the motor, and the BTS7960 module is made for exactly this job. This tutorial instructs you how to use Raspberry Pi to control a 12V DC motor through the BTS7960 motor driver, with a Python script started over SSH. In detail, we will learn:

Raspberry Pi 4 driving a 12V DC motor through a BTS7960 H-bridge motor driver

We suggest:

Hardware Preparation

1×Raspberry Pi 5
1×BTS7960 Motor Driver Module
1×12V DC Motor
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of BTS7960 Motor Driver

The BTS7960 module is a motor driver for currents that small driver boards cannot handle. Shops also sell the same board under the names "IBT-2" and "HW-039". On top of the board, there are two Infineon BTS7960B chips, cooled by a big heat sink under the board. Every chip is half of the bridge, so the two of them make the complete H-bridge. So one module controls one brushed DC motor. It can turn the motor in both directions, and it can change the speed with PWM. Next to the DRV8833 or the L298N, it is a real heavyweight. People use it for large gear motors, car wiper motors, winches, e-scooters, wheelchair motors and heavy robots.

How does it work? Each BTS7960B chip holds one motor wire and can connect it to B+ or to B- of the motor supply. When one chip connects its wire to B+ and the other chip connects its wire to B-, the current flows through the motor in one direction. When the two chips swap these jobs, the current flows the other way, and the motor turns the other way. A PWM signal switches the chip on and off very fast. A longer ON time in each pulse means a faster motor. The Raspberry Pi never switches the 12V itself. It only sends logic signals, and a 74HC14 buffer chip on the module (the product page calls it the isolation chip) passes them on to the two power chips.

BTS7960 motor driver module pinout

The control side is a header with 8 male pins in two rows of four. The power side is a screw terminal with four positions. Here is the job of each pin:

  • RPWM: PWM input of the right half-bridge. A PWM signal on this pin turns the motor forward
  • LPWM: PWM input of the left half-bridge. A PWM signal on this pin turns the motor in reverse
  • R_EN: enable pin of the right half-bridge. HIGH turns it on, LOW turns it off
  • L_EN: enable pin of the left half-bridge. HIGH turns it on, LOW turns it off
  • R_IS and L_IS: current-sense and error outputs. This tutorial does not use them, so they must stay unconnected
  • VCC: This pin must be connected to a 5V pin of the Raspberry Pi. This pin runs the 74HC14 chip and the power LED, nothing else
  • GND: This pin must be connected to ground (0V). On the module, it is already joined to B-
  • B+: This terminal must be connected to the positive side of the motor supply (5.5V to 27V DC)
  • B-: This terminal must be connected to the negative side of the motor supply
  • M+ and M-: the two wires of the DC motor are screwed in here

※ NOTE THAT:

Many factories make this board, and the pin order or the printed names can change between them. For example, many boards print R_IS and L_IS as "R_S" and "L_S". Always follow the labels on your own module, not only the position of the pins in the picture.

The main specs of the module:

BTS7960 Motor Driver Module
Power chips 2 x Infineon BTS7960B half-bridge (one full H-bridge)
Motors per module 1 brushed DC motor, both directions
Motor supply (B+ and B-) 5.5V to 27V DC (6V, 12V and 24V systems)
Current up to 43A (the current-limit level of the chip, not a running current)
PWM frequency up to 25 kHz on RPWM and LPWM
Logic chip 74HC14 buffer, powered from the VCC pin
Protection current limiting, over-temperature protection

How the Four Control Pins Move the Motor

R_EN and L_EN work like a main switch for the two half-bridges. RPWM and LPWM choose the direction and the speed. In Python, the RPi.GPIO library sets a PWM duty cycle from 0 to 100: 0 is the same as LOW, and 100 is the same as HIGH.

R_EN and L_EN RPWM LPWM What the motor does
HIGH PWM (duty cycle 0 to 100) 0 turns forward, the duty cycle sets the speed
HIGH 0 PWM (duty cycle 0 to 100) turns in reverse, the duty cycle sets the speed
HIGH 0 0 brakes, the driver joins the two motor wires, so the motor stops fast
LOW 0 0 coasts, both half-bridges are off, so the motor rolls to a stop by itself

Keep at least one of RPWM and LPWM at 0 all the time. Never send PWM to both pins at once. The script below has one small function for each row of this table, so it always keeps this rule.

Which stop should a rover use? Brake is the hard stop: the wheels stop almost at once, which helps on a slope or in front of a wall. Coast is the soft stop: the rover rolls on for a short distance and the gears get less shock.

Power and Ground

  • The motor gets its power from the 12V power adapter through the DC power jack: the + of the adapter goes to B+, and the - goes to B-. Check that the adapter's current rating is higher than the motor's current.
  • Check + and - again before you plug in the adapter. Getting + and - backwards may break the driver board.
  • VCC goes to the 5V pin of the Raspberry Pi (pin 4). This pin powers only the buffer chip, and the module maker lists 5V as its supply. The control inputs work with 3.3V signals, so the GPIO pins drive them directly. These inputs have pull-down resistors, and R_IS and L_IS stay unconnected, so nothing comes back from the module and no 5V can reach a GPIO pin.
  • GND and B- are joined on the module. One wire from a GND pin of the Pi to the GND pin of the module is enough to give the Pi, the module and the 12V supply the same ground.
  • 43A is the current where the chip starts to limit, not a current to run at. For long runs, keep the current far below it. The steady current limit is set by heat: the cooler the heat sink stays, the more the board can carry.

WARNING

B+ carries the full motor voltage (12V here, up to 27V on other systems). Never connect B+, B- or a motor wire to any pin of the Raspberry Pi header. The GPIO pins accept 3.3V only, and 12V will destroy the board.

Wiring Diagram

The wiring diagram between Raspberry Pi and BTS7960 motor driver

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield
BTS7960 Pin Raspberry Pi Pin Physical Pin
RPWM GPIO19 pin 35
LPWM GPIO13 pin 33
R_EN GPIO5 pin 29
L_EN GPIO6 pin 31
VCC 5V pin 4
GND GND pin 25
R_IS and L_IS not connected -

The motor side of the module uses its screw terminal:

  • BTS7960 B+ to the + terminal of the DC power jack (12V power adapter)
  • BTS7960 B- to the - terminal of the DC power jack
  • BTS7960 M+ and M- to the two wires of the 12V DC motor

The GND wire and the four signal wires go to the row of the 40-pin header with the odd pin numbers, so they are easy to count: pins 25, 29, 31, 33 and 35. Only the VCC wire goes to the other row, to pin 4 (5V). Both the Pi header and the module header have male pins, so use female-to-female jumper wires for this part. Keep the 12V adapter unplugged until the wiring is finished.

How To Program for Raspberry Pi to Control a 12V DC Motor with BTS7960

No extra Python package is needed besides RPi.GPIO. The script makes a software PWM signal of 1000 Hz on GPIO19 (RPWM) and GPIO13 (LPWM), which is far below the 25 kHz limit of the BTS7960. Then it runs the motor through this cycle, again and again:

  • Forward: the duty cycle on RPWM climbs from 0 to 100% in about 2 seconds
  • Forward at 100% for 2 seconds
  • Forward: the duty cycle drops back to 0% in about 2 seconds
  • Brake for 1 second: RPWM and LPWM are both 0, while R_EN and L_EN stay HIGH
  • Reverse at 50% for 2 seconds: the PWM signal moves to LPWM
  • Coast for 2 seconds: R_EN and L_EN go LOW, then they go HIGH again before the next forward run

Five short functions keep the main loop easy to read. forward(duty) and reverse(duty) take a duty cycle from 0 to 100. brake() and coast() are the two kinds of stop. enable_driver(on) sets R_EN and L_EN together.

Detailed Instructions

  1. Check that your Raspberry Pi runs Raspberry Pi OS (Raspbian) or another compatible operating system.
  2. Your Raspberry Pi and your PC must be on the same local network. The Pi also needs internet access if you have to install a package.
  3. Is this your first project with a Raspberry Pi? Then see how to set up the Raspberry Pi first.
  4. Open an SSH connection from your PC to the Raspberry Pi: use the built-in SSH client on Linux and macOS, or PuTTY on Windows. See how connect your PC to Raspberry Pi via SSH.
  5. The script needs the RPi.GPIO library. It usually comes with Raspberry Pi OS. If it is missing, install it with these commands:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  1. Wire the Raspberry Pi, the BTS7960 module, the DC power jack and the motor as shown in the wiring diagram above. The 12V adapter should still be unplugged.
  2. Create a Python script file bts7960_dc_motor.py (for example with nano bts7960_dc_motor.py) and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-bts7960-motor-driver """ import RPi.GPIO as GPIO import time # BTS7960 control pins (BCM numbering). The motor sits on M+ / M-. RPWM_PIN = 19 # GPIO19 (physical pin 35) -> BTS7960 RPWM, forward PWM LPWM_PIN = 13 # GPIO13 (physical pin 33) -> BTS7960 LPWM, reverse PWM R_EN_PIN = 5 # GPIO5 (physical pin 29) -> BTS7960 R_EN, right half-bridge enable L_EN_PIN = 6 # GPIO6 (physical pin 31) -> BTS7960 L_EN, left half-bridge enable PWM_FREQ = 1000 # software PWM frequency in Hz (the BTS7960 accepts up to 25 kHz) RAMP_STEP = 4 # duty cycle change per ramp step, in % RAMP_DELAY = 0.08 # seconds per ramp step (26 steps = about 2 seconds) REVERSE_DUTY = 50 # reverse speed, in % (about half speed) GPIO.setmode(GPIO.BCM) # All four pins are outputs and start LOW, so the driver is off at first for pin in (RPWM_PIN, LPWM_PIN, R_EN_PIN, L_EN_PIN): GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW) # One software PWM signal per direction pin, both at 0% duty cycle rpwm = GPIO.PWM(RPWM_PIN, PWM_FREQ) lpwm = GPIO.PWM(LPWM_PIN, PWM_FREQ) rpwm.start(0) lpwm.start(0) def enable_driver(on): # R_EN and L_EN together: HIGH = both half-bridges work, LOW = both are off level = GPIO.HIGH if on else GPIO.LOW GPIO.output(R_EN_PIN, level) GPIO.output(L_EN_PIN, level) def forward(duty): # LPWM stays at 0, the duty cycle on RPWM sets the speed (0 to 100) lpwm.ChangeDutyCycle(0) rpwm.ChangeDutyCycle(duty) def reverse(duty): # RPWM stays at 0, the duty cycle on LPWM sets the speed (0 to 100) rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(duty) def brake(): # Both PWM pins at 0 while the driver is still enabled: # the driver joins M+ and M-, so the motor stops fast rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(0) def coast(): # Both PWM pins at 0 and the driver turned off: # the motor is free and rolls to a stop by itself rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(0) enable_driver(False) enable_driver(True) # driver on, both PWM pins are still at 0 print("BTS7960 ready. Press Ctrl+C to stop.") try: while True: # Speed up in the forward direction print("[forward] duty 0 to 100%") for duty in range(0, 101, RAMP_STEP): forward(duty) time.sleep(RAMP_DELAY) # Stay at full speed print("[forward] full power for 2 s") time.sleep(2) # Slow down to 0 print("[forward] duty 100 to 0%") for duty in range(100, -1, -RAMP_STEP): forward(duty) time.sleep(RAMP_DELAY) # Hard stop print("[brake] M+ and M- joined for 1 s") brake() time.sleep(1) # Turn the other way at about half speed print("[reverse] 50% duty for 2 s") reverse(REVERSE_DUTY) time.sleep(2) # Soft stop: driver off, the motor rolls freely print("[coast] R_EN and L_EN LOW for 2 s") coast() time.sleep(2) enable_driver(True) # driver on again before the next forward run except KeyboardInterrupt: pass finally: # Stop both PWM signals, turn the driver off and release the GPIO pins rpwm.stop() lpwm.stop() enable_driver(False) GPIO.cleanup() print("Motor stopped, GPIO released")
  1. Save the file (in nano: Ctrl + O, Enter, then Ctrl + X).
  2. Next, the 12V adapter goes into the DC power jack.
  3. Start the script with this command:
python3 bts7960_dc_motor.py
  1. Watch the motor and the text in the terminal:
PuTTY - Raspberry Pi
BTS7960 ready. Press Ctrl+C to stop. [forward] duty 0 to 100% [forward] full power for 2 s [forward] duty 100 to 0% [brake] M+ and M- joined for 1 s [reverse] 50% duty for 2 s [coast] R_EN and L_EN LOW for 2 s [forward] duty 0 to 100% [forward] full power for 2 s [forward] duty 100 to 0% [brake] M+ and M- joined for 1 s Motor stopped, GPIO released

One full round takes about 11 seconds. The motor speeds up, runs at full speed, slows down and stops hard. Then it turns the other way at half speed and finally rolls to a stop on its own. The script repeats this round until you press Ctrl + C keys in the terminal. Then the finally block stops both PWM signals, pulls R_EN and L_EN LOW and releases the GPIO pins, so the motor does not keep running. The last line of the output above shows this moment.

If the motor turns the wrong way while the terminal shows "[forward]", unplug the 12V adapter and swap the two motor wires on M+ and M-.

At a low duty cycle, you may hear a soft, high tone from the motor. The 1000 Hz PWM signal makes this sound, and it is normal.

If the motor does not move at all, check that the 12V adapter is plugged in, that the GND wire sits on pin 25, and that R_EN and L_EN are on pins 29 and 31. Without HIGH on both enable pins, the driver stays off.

Additional Knowledge

BTS7960 vs L298N vs DRV8833

The DRV8833 also has its own Raspberry Pi tutorial on this site. Let's compare the BTS7960 with it and with the older L298N:

BTS7960 L298N DRV8833
Motors per module 1 2 2
Current up to 43A (limit level, heat decides the long-run current) about 2A per motor about 1.5A per motor (2A peak)
Voltage lost inside the driver very little about 2V little
Good for big 12V or 24V motors, heavy rovers, winches motors that need about 2A small 2.7V to 10.8V battery motors

Evidently, the BTS7960 is the right choice when the motor is big and pulls a high current, like the drive motor of a heavy rover. For two small wheels on a light robot car, the DRV8833 is smaller and loses less voltage than the L298N.

From One Motor to a Rover

One BTS7960 module drives one motor. A rover with a left and a right drive motor needs two modules:

  • Give the second module its own four GPIO pins for RPWM, LPWM, R_EN and L_EN, and its own VCC (5V) and GND wires.
  • Feed both modules from one battery or power supply that can give the current of both motors at the same time.
  • To turn on the spot, run one motor forward and the other in reverse, the same way the forward() and reverse() functions of this script do it for one motor.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!