Dual Motor Driver BTS7960

Here is the code for testing Motor DC Driver BTS7960 BEN-MD240S1


Do some wiring as below table

M1_RPWMArduino Uno Pin 2
M1_LPWMArduino Uno Pin 3
M1_R_ENArduino Uno Pin 4
M1_L_ENArduino Uno Pin 5
M2_RPWMArduino Uno Pin 6
M2_LPWMArduino Uno Pin 7
M2_R_ENArduino Uno Pin 8
M2_L_ENArduino Uno Pin 9
+5VArduino pin 5V
GNDArduino pin GND
+24VPSU/Battery 12-24V
GNDPSU/Battery GND
M1+Motor 1 DC +
M1-Motor 1 DC –
M2+Motor 2 DC +
M2-Motor 2 DC –

And finally here is working code written in platformio (C++) code that you can try:

#include <Arduino.h>
// Motor 1 pins
#define M1_RPWM 2
#define M1_LPWM 3
#define M1_R_EN 4
#define M1_L_EN 5

// Motor 2 pins
#define M2_RPWM 6
#define M2_LPWM 7
#define M2_R_EN 8
#define M2_L_EN 9

// Function to rotate Motor 1 clockwise
void motor1_cw(int speed)
{
  analogWrite(M1_RPWM, speed);
  digitalWrite(M1_LPWM, LOW);
}

// Function to rotate Motor 1 counter-clockwise
void motor1_ccw(int speed)
{
  digitalWrite(M1_RPWM, LOW);
  analogWrite(M1_LPWM, speed);
}

// Function to stop Motor 1
void motor1_stop()
{
  digitalWrite(M1_RPWM, LOW);
  digitalWrite(M1_LPWM, LOW);
}

// Function to rotate Motor 2 clockwise
void motor2_cw(int speed)
{
  analogWrite(M2_RPWM, speed);
  digitalWrite(M2_LPWM, LOW);
}

// Function to rotate Motor 2 counter-clockwise
void motor2_ccw(int speed)
{
  digitalWrite(M2_RPWM, LOW);
  analogWrite(M2_LPWM, speed);
}

// Function to stop Motor 2
void motor2_stop()
{
  digitalWrite(M2_RPWM, LOW);
  digitalWrite(M2_LPWM, LOW);
}

void setup()
{
  // Motor 1 pins setup
  pinMode(M1_RPWM, OUTPUT);
  pinMode(M1_LPWM, OUTPUT);
  pinMode(M1_R_EN, OUTPUT);
  pinMode(M1_L_EN, OUTPUT);

  // Motor 2 pins setup
  pinMode(M2_RPWM, OUTPUT);
  pinMode(M2_LPWM, OUTPUT);
  pinMode(M2_R_EN, OUTPUT);
  pinMode(M2_L_EN, OUTPUT);

  // Initialize Motor 1 outputs
  digitalWrite(M1_RPWM, LOW);
  digitalWrite(M1_LPWM, LOW);
  digitalWrite(M1_R_EN, HIGH);
  digitalWrite(M1_L_EN, HIGH);

  // Initialize Motor 2 outputs
  digitalWrite(M2_RPWM, LOW);
  digitalWrite(M2_LPWM, LOW);
  digitalWrite(M2_R_EN, HIGH);
  digitalWrite(M2_L_EN, HIGH);
}

void loop()
{
  // Test Motor 1 - Forward
  motor1_cw(200);
  delay(2000);

  // Test Motor 1 - Stop
  motor1_stop();
  delay(500);

  // Test Motor 1 - Backward
  motor1_ccw(200);
  delay(2000);

  // Test Motor 1 - Stop
  motor1_stop();
  delay(500);

  // Test Motor 2 - Forward
  motor2_cw(200);
  delay(2000);

  // Test Motor 2 - Stop
  motor2_stop();
  delay(500);

  // Test Motor 2 - Backward
  motor2_ccw(200);
  delay(2000);

  // Test Motor 2 - Stop
  motor2_stop();
  delay(500);

  // Test both motors together - Forward
  motor1_cw(150);
  motor2_cw(150);
  delay(2000);

  // Stop both motors
  motor1_stop();
  motor2_stop();
  delay(1000);

  // Test both motors together - Backward
  motor1_ccw(150);
  motor2_ccw(150);
  delay(2000);

  // Stop both motors
  motor1_stop();
  motor2_stop();
  delay(1000);

  // Test differential movement (turn)
  motor1_cw(100);
  motor2_ccw(100);
  delay(1000);

  // Stop both motors
  motor1_stop();
  motor2_stop();
  delay(2000);
}

if you have any queries related to this project please let me know in the comment bellow.

Leave a Reply

Your email address will not be published. Required fields are marked *