Skip to content

Instantly share code, notes, and snippets.

@javaplus
Last active August 4, 2025 06:05
Show Gist options
  • Save javaplus/2cae3e595afdfc86ef15da32a8e0a2c8 to your computer and use it in GitHub Desktop.
Save javaplus/2cae3e595afdfc86ef15da32a8e0a2c8 to your computer and use it in GitHub Desktop.
RaspberryPI BTS7960 HBridge Example Simple
###############################################################
## Simple code to use PWM on the PI to control the BTS7960
##
## This simply spins the motor in one direction slowly
## speeding up slowly and then slowing down slowly over
## and over again.
##
###############################################################
import RPi.GPIO as GPIO
# https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/
import time
# We are going to use the BCM not BOARD layout
# https://i.stack.imgur.com/yHddo.png - BCM are the "GPIO#" ones not the ordered pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
RPWM = 19; # GPIO pin 19 to the RPWM on the BTS7960
LPWM = 26; # GPIO pin 26 to the LPWM on the BTS7960
# For enabling "Left" and "Right" movement
L_EN = 20; # connect GPIO pin 20 to L_EN on the BTS7960
R_EN = 21; # connect GPIO pin 21 to R_EN on the BTS7960
# Set all of our PINS to output
GPIO.setup(RPWM, GPIO.OUT)
GPIO.setup(LPWM, GPIO.OUT)
GPIO.setup(L_EN, GPIO.OUT)
GPIO.setup(R_EN, GPIO.OUT)
# Enable "Left" and "Right" movement on the HBRidge
GPIO.output(R_EN, True)
GPIO.output(L_EN, True)
rpwm= GPIO.PWM(RPWM, 100)
lpwm= GPIO.PWM(LPWM, 100)
rpwm.ChangeDutyCycle(0)
rpwm.start(0)
while 1:
for x in range(100):
print("Speeding up " + str(x))
rpwm.ChangeDutyCycle(x)
time.sleep(0.25)
time.sleep(5)
for x in range(100):
print("Slowing down " + str(x))
rpwm.ChangeDutyCycle(100-x)
time.sleep(0.25)
@irfanmevsimgit
Copy link

thank you javaplus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment