搜索代码

使用BTS7960模块和Arduino控制多个直流电动机

使用BTS7960模块和Arduino控制多个直流电动机

在本教程中,我们将探索如何使用BTS7960模块和Arduino控制多个直流电机。BTS7960是一款强大的电机驱动器,能够处理高电流,并通过PWM(脉宽调制)实现对电机方向和速度的精确控制。在项目结束时,您将拥有一个能够控制两个直流电机的工作设置,使它们可以顺时针和逆时针旋转。本教程将指导您完成硬件设置、接线说明以及操作电机所需的代码。为了更清楚地理解整个过程,请务必查看附带的视频(视频在00:00时)。

BTS7960模块-3

硬件解析

该项目的主要组件是BTS7960电机驱动模块。该模块包含两个H桥驱动器,可以通过切换施加到电机的电压极性来控制电机的方向。每个驱动器能够处理高达43A的电流,这使其适合高功率应用。该模块还具有针对过温和过流情况的内置保护。除了BTS7960,您还需要一个Arduino板来控制电机。Arduino向电机驱动器发送PWM信号,使您可以调整电机的速度和方向。Arduino与BTS7960模块之间的连接至关重要,因为它们决定了电机如何响应信号。

数据表详细信息

制造商 英飞凌科技
部件编号 BTS7960
逻辑/IO电压 3.3 V到5 V
供电电压 5-40 V
输出电流(每通道) 43 A 最大
峰值电流(每通道) 60 A
PWM频率指导 5-25 kHz
输入逻辑阈值 0.8 伏 (高),0.3 伏 (低)
电压降 / RDS(开)/饱和度 16 毫欧
热限制 最大150 °C
包裹 TO-263
备注 / 变体 双H桥配置

  • 确保在高电流下运行时有适当的散热。
  • 使用适当的线规进行电源连接。
  • 仔细检查Arduino与BTS7960之间的引脚映射。
  • 实施电流限制以防止模块损坏。
  • 在电源附近使用解耦电容以提高稳定性。
BTS7960模块-2

接线说明

BTS7960-_2_motor_wiring

要将BTS7960模块接线到Arduino,请仔细按照以下连接方式进行操作:1. **电源连接**:将电源的正极连接到BTS7960模块的B+端子,将负极连接到B-端子。确保电源的电压与您的电机要求相符。2. **电机连接**:将电机电线连接到模块的M+和M-端子。这将使模块能够控制电机。3. **Arduino引脚**:将Arduino上的以下引脚连接到BTS7960模块:- 引脚3(RPWM)连接到模块的RPWM引脚。- 引脚4(R_EN)连接到模块的R_EN引脚。- 引脚5(R_IS)连接到模块的R_IS引脚。- 引脚6(LPWM)连接到模块的LPWM引脚。- 引脚7(L_EN)连接到模块的L_EN引脚。- 引脚8(L_IS)连接到模块的L_IS引脚。4. **接地连接**:将Arduino的接地引脚连接到BTS7960模块的接地引脚,以确保信号的共同参考。确保所有连接牢固,以防止操作过程中出现意外行为。

安装所需库

安装该robojax_BTS7960_motor_driver_library在Arduino IDE中,首先从提供的链接下载库的ZIP文件。保存文件后,打开Arduino IDE并导航到草图 > 包含库 > 添加 .ZIP 库...在文件选择对话框中,浏览到下载的 ZIP 文件,选择该文件,然后点击“打开”。然后,IDE 将安装该库。您可以通过检查来确认安装是否成功文件 > 示例菜单中应该出现一个名为“Robojax BTS7960电机驱动库”的新类别。您现在可以在代码中包含库头文件。#include <RobojaxBTS7960.h>.

代码示例与演练

以下代码片段演示了如何使用BTS7960模块设置和控制电机。初始设置定义了连接到电机的引脚并初始化电机对象:


// pins for motor 1
#define RPWM_1 3
#define R_EN_1 4
#define R_IS_1 5
#define LPWM_1 6
#define L_EN_1 7
#define L_IS_1 8

// pins for motor 2
#define RPWM_2 9
#define R_EN_2 10
#define R_IS_2 12
#define LPWM_2 11
#define L_EN_2 A0
#define L_IS_2 A1

#include 
RobojaxBTS7960 motor1(R_EN_1, RPWM_1, R_IS_1, L_EN_1, LPWM_1, L_IS_1, debug);
RobojaxBTS7960 motor2(R_EN_2, RPWM_2, R_IS_2, L_EN_2, LPWM_2, L_IS_2, debug);

这段代码设置了引脚定义,并为两个电机创建了`RobojaxBTS7960`类的实例。`setup()`函数初始化电机和串口监视器:


void setup() {
  Serial.begin(9600); // setup Serial Monitor
  motor1.begin();
  motor2.begin();
}

这里,`motor.begin()` 方法为每个电机准备操作,使您可以在 `loop()` 函数中控制它们。在 `loop()` 函数中,您可以使用如下命令控制电机:


void loop() {
   motor1.rotate(100, CW); // run motor 1 at 100% speed CW
   delay(5000); // run for 5 seconds
   motor1.stop(); // stop motor 1
   delay(3000); // stop for 3 seconds
   motor2.rotate(100, CCW); // run motor 2 at 100% speed CCW
   delay(5000); // run for 5 seconds
   motor2.stop(); // stop motor 2
   delay(3000); // stop for 3 seconds
}

本节展示了如何使电机双向旋转和控制其速度。完整代码已附在文章下方供您参考。

演示 / 期待内容

在完成接线和上传代码后,您应该观察到电动机分别以顺时针和逆时针方向旋转。代码将使每个电动机以满速运行五秒,然后停止三秒,并重复该过程。注意任何潜在的问题,例如连接反向或电源不足。如果您遇到问题,请仔细检查您的接线,并确保电源充足(视频时间为00:00)。

视频时间戳

  • 00:00 开始
  • 00:48 硬件解释
  • 04:06 数据表已查看
  • 07:07 接线讲解
  • 09:00 代码解释
  • 14:33 示范
  • 16:47 最大电流测试
  • 19:25 热成像
  • 19:27 不同的代码测试

图像

BTS7960_module-1
BTS7960_module-1
BTS7960_module-2
BTS7960_module-2
BTS7960_module-3
BTS7960_module-3
BTS7960_module-4-heat-sink
BTS7960_module-4-heat-sink
BTS7960-_2_motor_wiring
BTS7960-_2_motor_wiring
203-Arduino Code to control two or more DC motors using BTS7960 motor driver
语言: C++
++
/*
* This is the Arduino code for the BTS7960 DC motor driver.
Using this code, you can control more than one motor to rotate in both directions: clockwise (CW) 
and counter-clockwise (CCW).
📚⬇️ Download and resource page for this video https://robojax.com/RJT169
📚⬇️ Download and resource page https://robojax.com/RJT170


Written by Ahmad Shamshiri for Robojax.com on
July 16, 2020 in Ajax, Ontario, Canada.

Watch video instructions for this code:  https://youtu.be/PUL5DZ9TA2o


BTS7960B
If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon: http://robojax.com/L/?id=63

or make a donation using PayPal: http://robojax.com/L/?id=64

 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

// pins for motor 1
#define RPWM_1 3 // define pin 3 for RPWM pin (output)
#define R_EN_1 4 // define pin 2 for R_EN pin (input)
#define R_IS_1 5 // define pin 5 for R_IS pin (output)

#define LPWM_1 6 // define pin 6 for LPWM pin (output)
#define L_EN_1 7 // define pin 7 for L_EN pin (input)
#define L_IS_1 8 // define pin 8 for L_IS pin (output)
// motor 1 pins end here

// pins for motor 2
#define RPWM_2 9 // define pin 9 for RPWM pin (output)
#define R_EN_2 10 // define pin 10 for R_EN pin (input)
#define R_IS_2 12 // define pin 12 for R_IS pin (output)

#define LPWM_2 11 // define pin 11 for LPWM pin (output)
#define L_EN_2 A0 // define pin 7 for L_EN pin (input)
#define L_IS_2 A1 // define pin 8 for L_IS pin (output)
// motor 2 pins end here



#define CW 1 //
#define CCW 0 //
#define debug 1 //

#include <RobojaxBTS7960.h>
RobojaxBTS7960 motor1(R_EN_1,RPWM_1,R_IS_1, L_EN_1,LPWM_1,L_IS_1,debug);//define motor 1 object
RobojaxBTS7960 motor2(R_EN_2,RPWM_2,R_IS_2, L_EN_2,LPWM_2,L_IS_2,debug);//define motor 2 object and the same way for other motors

void setup() {
  // BTS7960 Motor Control Code by Robojax.com 20190622
  Serial.begin(9600);// setup Serial Monitor to display information

   motor1.begin();
   motor2.begin();   
   
    // BTS7960 Motor Control Code by Robojax.com 20190622 
}

void loop() {
   // BTS7960 Motor Control Code by Robojax.com 20190622 
    motor1.rotate(100,CW);// run motor 1 with 100% speed in CW direction
    delay(5000);//run for 5 seconds
    motor1.stop();// stop the motor 1
    delay(3000);// stop for 3 seconds
    motor1.rotate(100,CCW);// run motor 1 at 100% speed in CCW direction
    delay(5000);// run for 5 seconds
    motor1.stop();// stop the motor 1
    delay(3000);  // stop for 3 seconds

    motor2.rotate(100,CW);// run motor 2 with 100% speed in CW direction
    delay(5000);//run for 5 seconds
    motor2.stop();// stop the motor 2
    delay(3000);// stop for 3 seconds
    motor2.rotate(100,CCW);// run motor 2 at 100% speed in CCW direction
    delay(5000);// run for 5 seconds
    motor2.stop();// stop the motor 2
    delay(3000);  // stop for 3 seconds

    
  // slowly speed up the motor 1 from 0 to 100% speed
    for(int i=0; i<=100; i++){ 
        motor1.rotate(i,CCW);
        delay(50);
    } 
  
   // slow down the motor 2 from 100% to 0
    for(int i=100; i>0; i--){ 
        motor2.rotate(i,CCW);
        delay(50);
    } 
    motor2.stop();// stop motor 2
    delay(3000); // stop for 3 seconds        
 // BTS7960 more than 1 Motor Control Code by Robojax.com 20190622  
}// loop ends

文件📁

Arduino 库(zip 格式)

数据手册 (pdf)

Fritzing 文件