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
- Raspberry Pi (any model with GPIO capabilities, e.g., Pi 3, Pi 4)
- BTS7960 Motor Driver Module
- DC Motor (suitable for your project)
- External Power Supply (matching your motor’s voltage and current requirements)
- 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
- Connect the motor terminals to the Motor+ (M+) and Motor- (M-) pins.
- Connect the external power supply’s positive terminal to VIN.
- 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
- Open the terminal and run:
sudo raspi-config
- Navigate to Interface Options > P5: I2C, and enable I2C.
- 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
- Connect the motor and power supply.
- Run the Python script:
python3 bts7960_control.py
- Observe the motor changing speed and direction as programmed.
Troubleshooting
-
Motor Not Running:
- Check wiring connections.
- Verify the external power supply.
-
PWM Not Working:
- Ensure GPIO18 and GPIO19 are configured for PWM.
- Check the duty cycle values in the script.
-
Overheating Module:
- Ensure the motor’s current draw does not exceed the BTS7960’s rating.
Applications of the BTS7960 with Raspberry Pi
- Controlling high-current DC motors in robotics
- Building automated vehicles or robots
- Creating motorized systems for industrial applications
- 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!