搜索代码

第102课:使用ZK-5DA控制两个4A直流电动机

第102课:使用ZK-5DA控制两个4A直流电动机

在本教程中,我们将探索如何使用 ZK-5DA 电机驱动模块来控制两个 4A 的直流电机。该模块基于 TA6586 芯片,提供高效的电机控制,具有低电压降和散热性能。在本教程结束时,您将能够有效地启动、停止和控制电机的速度。为了获得更多说明,请务必查看视频(视频中在 08:55)。

硬件解析

该项目的主要组件是ZK-5DA电机驱动模块。它使用TA6586芯片,可以高效控制两个直流电机。该芯片设计用于每个电机处理高达4A的电流,同时保持低电压降,这有助于在操作过程中减少发热。

除了电机驱动器外,您还需要一个Arduino板来向模块发送控制信号。Arduino将使用PWM(脉宽调制)信号来定义电机的方向和速度。将电机连接到驱动模块上的适当端口可以实现方向控制和速度调整。

数据表详细信息

制造商瑞泽半导体
部件号TA6586
操作电压3-14 伏
峰值电流9 A
连续电流5 A
待机电流< 2 微安
电压降400 毫伏在 4 安培下
温度范围-25至85°C
包裹标准IC封装
备注 / 变体热关断,过流保护

  • 确保在超过3 A的持续运行中有效散热。
  • 使用支持PWM的引脚进行速度控制。
  • 在连接电动机之前,请检查电压等级。
  • 注意接线极性,以避免电机损坏。
  • 在操作过程中监测温度,以防止过热。

接线说明

ZK-5AD_TA6586_wiring-_mor_tan_5V

要接线ZK-5DA电机驱动模块,首先连接电源。将电源的正极连接到电机驱动器的'+'端子,负极连接到'-'端子。两个电机将连接到标记清晰的输出端子。

对于Arduino连接,请使用支持PWM的引脚:连接引脚3连接到电机1的控制引脚,以及引脚5连接到电机1的第二控制引脚。同样,连接引脚6到电机2的控制引脚,以及引脚9连接到电动机 2 的第二个控制引脚。最后,确保所有接地线连接在一起。

代码示例与演练

代码首先定义了相关引脚,并初始化串行监视器以进行调试。电机控制功能,例如M1M2, 处理电机方向和速度。

const int D0=9; // Motor 1 PWM pin
const int D1=6; // Motor 1 direction pin
const int D2=5; // Motor 2 PWM pin
const int D3=3; // Motor 2 direction pin

这里,针脚被设置为使用PWM信号控制电机。setup该函数将这些引脚初始化为输出。

void loop() {
  M2(CW, 80); // Motor 2 runs clockwise at 80% speed
  delay(3000); // Wait for 3 seconds
  brake(2); // Apply brake to motor 2
  delay(1000); 
}

该摘录显示了程序的主循环,其中电机2设置为以80%的速度顺时针运行3秒钟,然后施加刹车。brake该函数在被调用时停止电动机。

void M1(bool direction, int speed) {
  int pwm = map(speed, 0, 100, 0, 255); // Map speed to PWM range
  if (direction == CW) {
    analogWrite(D0, pwm);
    analogWrite(D1, LOW);
  } else {
    analogWrite(D1, pwm);
    analogWrite(D0, LOW);
  }
}

theM1功能接收方向和速度作为输入,将速度映射到PWM值,并设置相应的引脚以控制电机的旋转方向。debugPrint函数被调用以在串口监视器中显示当前状态。

完整代码请记得查看文章下方(视频中在08:55)。

演示 / 期待的内容

当您运行代码时,预计电机会按照设定的方向以设定的速度开始旋转。代码包括制动和速度调整,允许动态控制。如果电机没有如预期那样响应,请仔细检查您的接线连接,并确保代码中正确配置了相应的引脚。此外,请注意防止过热,特别是在大电流负载时,正如在测试中所示(视频时间为23:23)。

视频时间戳

  • 00:00 介绍
  • 03:16 数据表已查看
  • 06:34 电线连接解释
  • 08:55 代码说明
  • 14:28 演示:电机控制
  • 17:22 演示:最大电流测试
  • 23:23 在 3A、4A 和 5A 下的电压降测试
  • 26:28 结论评论

图像

TA6586_ZK5Ad_image
TA6586_ZK5Ad_image
ZK-5AD_TA6586_wiring-_mor_tan_5V
ZK-5AD_TA6586_wiring-_mor_tan_5V
power_adapter
power_adapter
408-Lesson 102: Using ZK-5DA to control two DC motors, each 4A
语言: C++
/*
 * Lesson 102: Using ZK-5DA to control 2 DC motors, each 4A
 * 
 * Full video details: https://youtu.be/W_Wm28nQAYA
 * Video timing:
00:00 Introduction
03:16 Datasheet viewed
06:34 Wiring explained
08:55 Code explained
14:28 Demonstration: Motor control
17:22 Demonstration: Maximum current test
23:23 Voltage drop test at 3A, 4A, and 5A
26:28 Conclusion remarks

 * Written by Ahmad Shamshiri for Arduino Step by Step Course by Robojax
 * www.Robojax.com
 * on Mar 22, 2022 

 * 
 * This code is part of Arduino Step by Step Course which starts here:  https://youtu.be/-6qSrDUA5a8
 * 
 * For the library for this code, visit http://robojax.com/
 * 
If you found this tutorial helpful, please support me so I can continue creating 
content like this. Make a donation using PayPal or a credit card: https://bit.ly/donate-robojax 
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * 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/>.
 */

 
 //all pins must be PWM enabled pins with ~ printed beside them
const int D0=9;//~
const int D1=6;
const int D2=5;
const int D3=3;

bool CW = true;
bool CCW = false;


bool debug = false;

void setup() {
  Serial.begin(9600);
  Serial.println("Robojax TA6586 Motor Control");
 pinMode(D0, OUTPUT);
 pinMode(D1, OUTPUT);
 pinMode(D2, OUTPUT);
 pinMode(D3, OUTPUT);  
 
 
}

void loop() {
  // * Full video details: https://youtu.be/W_Wm28nQAYA
  M2(CW, 80);//motor 1 runs CW at 80% speed
  M1(CW, 100); //motor 1 runs CW at 100% speed
  delay(3000);//wait for 3 seconds
  brake (2);//apply brake to motor 2
  delay(1000);  
  
  M2(CCW, 60);//motor 2 runs CCW at 60% speed
  delay(3000);//wait for 3 seconds
  brake (2);//apply brake to motor 2
  brake (1);//apply brake to motor 1
  delay(3000);//wait for 3 seconds
  for(int i=0; i<=100; i++)
  {
    M1(CCW, i);//run motor 1 to CCW direction with i% speed
    delay(100);
  }
  delay(2000);
  brake (2);//apply brake to motor 2
  delay(3000); delay(3000);//wait for 3 seconds}
}//loop ends

/*
 *  M1(bool direction,int speed)
 * @brief runs motor 1 
 * @param direction is CW or CCW, 
 * @param speed is an integer between 0 to 100

 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on Mar 22, 2022 
 * Full video details: https://youtu.be/W_Wm28nQAYA
 */
void M1(bool direction,int speed)
{
  int pwm=map(speed, 0, 100, 0, 255);
  if(direction == CW)
  {
   analogWrite(D0,pwm);
   analogWrite(D1,LOW);   
  }else{
   analogWrite(D1,pwm);
   analogWrite(D0,LOW);     
  }
  debugPrint(1, direction, speed, false); 
}//M1 end


/*
 *  M2(bool direction,int speed)
 * @brief runs motor 2 
 * @param direction is CW or CCW, 
 * @param speed is an integer between 0 to 100

 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on Mar 22, 2022 
 * Full video details: https://youtu.be/W_Wm28nQAYA
 */
void M2(bool direction,int speed)
{
  int pwm=map(speed, 0, 100, 0, 255);
  if(direction == CW)
  {
   analogWrite(D2,pwm);
   analogWrite(D3,LOW);   
  }else{
   analogWrite(D3,pwm);
   analogWrite(D2,LOW);     
  } 
  debugPrint(2, direction, speed, false);    
}//M2 ends


/*
 *  brake(int motor)
 * @brief applies brake to a motor
 * @param motor is an integer (1 or 2)

 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on Mar 22, 2022 
 * Full video details: https://youtu.be/W_Wm28nQAYA
 */
void brake(int motor)
{
   if(motor == 1)
  {
   analogWrite(D0,HIGH);
   analogWrite(D1,HIGH);   
  }else{
   analogWrite(D2,HIGH);
   analogWrite(D3,HIGH);     
  }
  debugPrint(motor, true,  0, true);  
}//brake ends


/*
 * debugPrint(int motor, bool direction, int speed, bool stop)
 * @brief prints debugging information
 * @param motor is an integer (1 or 2)
 * @param direction is CW or CCW
 * @param speed is an integer from 0 to 100
 * @param stop is true or false; if true, the word "stop" is printed

 * @return returns none
 * Written by Ahmad Shamshiri for robojax.com
 * on Mar 22, 2022 
 * Full video details: https://youtu.be/W_Wm28nQAYA
 */
void debugPrint(int motor, bool direction, int speed, bool stop)
{
  if(debug)
  {
      Serial.print("Motor: ");
      Serial.print(motor);
    if(stop && motor>0)
    {
      Serial.println(" Stopped");
    }else{
      if(direction)
      {
      Serial.print(" CW at ");
      }else{
       Serial.print(" CCW at ");     
      }
      Serial.print(speed);       
      Serial.println(" %");    
    }
  }//debug
  
}

//not used but can be used to apply brake on both motors
void fullBrake()
{
 brake(1);
 brake(2); 

}

|||您可能需要的东西

文件📁

数据手册 (pdf)