Using the BTS7960 with the Raspberry Pi

Using the BTS7960 with the Raspberry Pi

The BTS7960 motor driver is a powerful H-bridge module capable of handling high currents, making it ideal for controlling DC motors in robotics and automation projects. In this tutorial, you’ll learn how to connect and control the BTS7960 with a Raspberry Pi to drive a motor.


What You Will Need

  1. Raspberry Pi (any model with GPIO capabilities, e.g., Pi 3, Pi 4)
  2. BTS7960 Motor Driver Module
  3. DC Motor (suitable for your project)
  4. External Power Supply (matching your motor’s voltage and current requirements)
  5. Breadboard and Jumper Wires

Step 1: Understanding the BTS7960 Motor Driver

The BTS7960 module includes two high-power half-bridges capable of driving motors with currents up to 43A. It features:

  • PWM input pins for speed control
  • Direction control pins
  • Overcurrent and thermal protection

Pinout

Pin Description
VCC 5V logic power input
GND Ground
RPWM PWM input for forward motion
LPWM PWM input for reverse motion
R_EN Enable pin for forward motion
L_EN Enable pin for reverse motion
Motor+ (M+) Motor positive terminal
Motor- (M-) Motor negative terminal
VIN External motor power supply
GND (Power) Ground for motor power supply

Step 2: Wiring the BTS7960 to the Raspberry Pi

Connections

BTS7960 Pin Raspberry Pi Pin
VCC 5V
GND GND
RPWM GPIO18 (PWM channel 0)
LPWM GPIO19 (PWM channel 1)
R_EN GPIO23
L_EN GPIO24

Motor and Power Supply Connections

  1. Connect the motor terminals to the Motor+ (M+) and Motor- (M-) pins.
  2. Connect the external power supply’s positive terminal to VIN.
  3. Connect the external power supply’s ground to the GND (Power) pin.

Note: Ensure the external power supply matches your motor’s voltage and current requirements.


Step 3: Enabling PWM on the Raspberry Pi

To control motor speed, you’ll use PWM (Pulse Width Modulation). Raspberry Pi GPIO pins 18 and 19 support hardware PWM.

Enable PWM via Raspberry Pi Configuration

  1. Open the terminal and run:
    sudo raspi-config
    
  2. Navigate to Interface Options > P5: I2C, and enable I2C.
  3. Save and reboot the Raspberry Pi:
    sudo reboot
    

Step 4: Writing Python Code to Control the Motor

Install the RPi.GPIO library to control GPIO pins if not already installed:

pip install RPi.GPIO

Example Python Code

This example demonstrates how to control the motor’s speed and direction using the BTS7960.

import RPi.GPIO as GPIO
import time

# Pin Definitions
RPWM_PIN = 18
LPWM_PIN = 19
REN_PIN = 23
LEN_PIN = 24

# GPIO Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(RPWM_PIN, GPIO.OUT)
GPIO.setup(LPWM_PIN, GPIO.OUT)
GPIO.setup(REN_PIN, GPIO.OUT)
GPIO.setup(LEN_PIN, GPIO.OUT)

# PWM Setup
frequency = 1000  # PWM frequency in Hz
rpwm = GPIO.PWM(RPWM_PIN, frequency)
lpwm = GPIO.PWM(LPWM_PIN, frequency)

# Start PWM with 0% duty cycle (off)
rpwm.start(0)
lpwm.start(0)

try:
    while True:
        # Enable Forward Motion
        GPIO.output(REN_PIN, GPIO.HIGH)
        GPIO.output(LEN_PIN, GPIO.LOW)

        # Set forward speed (50% duty cycle)
        rpwm.ChangeDutyCycle(50)
        lpwm.ChangeDutyCycle(0)
        time.sleep(2)

        # Enable Reverse Motion
        GPIO.output(REN_PIN, GPIO.LOW)
        GPIO.output(LEN_PIN, GPIO.HIGH)

        # Set reverse speed (30% duty cycle)
        rpwm.ChangeDutyCycle(0)
        lpwm.ChangeDutyCycle(30)
        time.sleep(2)

except KeyboardInterrupt:
    print("Stopping motor...")

finally:
    rpwm.stop()
    lpwm.stop()
    GPIO.cleanup()

Step 5: Testing Your Setup

  1. Connect the motor and power supply.
  2. Run the Python script:
    python3 bts7960_control.py
    
  3. Observe the motor changing speed and direction as programmed.

Troubleshooting

  1. Motor Not Running:

    • Check wiring connections.
    • Verify the external power supply.
  2. PWM Not Working:

    • Ensure GPIO18 and GPIO19 are configured for PWM.
    • Check the duty cycle values in the script.
  3. Overheating Module:

    • Ensure the motor’s current draw does not exceed the BTS7960’s rating.

Applications of the BTS7960 with Raspberry Pi

  1. Controlling high-current DC motors in robotics
  2. Building automated vehicles or robots
  3. Creating motorized systems for industrial applications
  4. Developing remote-controlled systems

Conclusion

The BTS7960 motor driver is a robust and efficient solution for driving high-current motors. Combined with the Raspberry Pi’s GPIO capabilities, it opens up numerous possibilities for robotics and automation projects. By following this guide, you can set up and control the BTS7960 to create powerful motorized systems. Experiment with different PWM frequencies and duty cycles to optimize performance for your specific application!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.