“(SKU:ARD030300)L298P Shield直流马达驱动”的版本间的差异

来自YwRobot Studio Wiki
跳转至: 导航搜索
样例代码
第32行: 第32行:
 
//This motor shield use Pin 6,5,7,4 to control the motor
 
//This motor shield use Pin 6,5,7,4 to control the motor
 
// Simply connect your motors to M1+,M1-,M2+,M2-
 
// Simply connect your motors to M1+,M1-,M2+,M2-
// Upload the code to Arduino/Roboduino
 
 
// Through serial monitor, type 'a','s', 'w','d','x' to control the motor
 
// Through serial monitor, type 'a','s', 'w','d','x' to control the motor
// www.ywrobot.com
 
  
int EN1 = 6;
+
int EN1 = 6;
 
int EN2 = 5;  //Roboduino Motor shield uses Pin 9
 
int EN2 = 5;  //Roboduino Motor shield uses Pin 9
 
int IN1 = 7;
 
int IN1 = 7;
 
int IN2 = 4; //Latest version use pin 4 instead of pin 8
 
int IN2 = 4; //Latest version use pin 4 instead of pin 8
 
  
 +
void Motor1(int pwm, boolean reverse) {
 +
  analogWrite(EN1, pwm); //set pwm control, 0 for stop, and 255 for maximum speed
 +
  if (reverse) {
 +
    digitalWrite(IN1, HIGH);
 +
  }
 +
  else {
 +
    digitalWrite(IN1, LOW);
 +
  }
 +
}
  
void Motor1(int pwm, boolean reverse)
+
void Motor2(int pwm, boolean reverse) {
        {
+
  analogWrite(EN2, pwm);
          analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
+
  if (reverse) {
        if(reverse)
+
    digitalWrite(IN2, HIGH);
        {
+
  }
          digitalWrite(IN1,HIGH);   
+
  else {
        }
+
    digitalWrite(IN2, LOW);
        else
+
  }
        {
+
}
          digitalWrite(IN1,LOW);   
+
        }
+
        } 
+
       
+
void Motor2(int pwm, boolean reverse)
+
        {
+
          analogWrite(EN2,pwm);
+
        if(reverse)
+
        {  
+
          digitalWrite(IN2,HIGH);  
+
        }
+
        else
+
        {
+
          digitalWrite(IN2,LOW);  
+
        }
+
        }
+
     
+
void setup()
+
{
+
    int i;
+
  // for(i=6;i<=9;i++) //For Roboduino Motor Shield
+
  // pinMode(i, OUTPUT);  //set pin 6,7,8,9 to output mode
+
  
    for(i=4;i<=7;i++) //For Arduino Motor Shield
+
void setup() {
 +
  int i;
 +
  for (i = 4; i <= 7; i++) //For Arduino Motor Shield
 
     pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
 
     pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
 +
}
  
    Serial.begin(9600); 
+
void loop() {
}
+
   Motor1(255, true); //You can change the speed, such as Motor(50,true)
 
+
  Motor2(255, true);
 
+
  delay(500);
void loop()  
+
  Motor1(255, false);
{  
+
  Motor2(255, false);
   int x,delay_en;
+
  delay(500);
  char val;
+
  Motor1(150, false);
  while(1)
+
  Motor2(150, true);
  {
+
  delay(500);
    val = Serial.read();
+
  Motor1(150, true);
    if(val!=-1)
+
  Motor2(150, false );
      {
+
   delay(500);
          switch(val)
+
          {
+
            case 'w'://Move ahead
+
                        Motor1(100,true); //You can change the speed, such as Motor(50,true)
+
                        Motor2(100,true);
+
                     
+
                        break;
+
            case 'x'://move back
+
                        Motor1(100,false);
+
                        Motor2(100,false);
+
                        break;
+
            case 'a'://turn left
+
                        Motor1(100,false);
+
                        Motor2(100,true);
+
                        break;      
+
            case 'd'://turn right
+
                        Motor1(100,true);
+
                        Motor2(100,false);
+
                        break;    
+
              case 's'://stop
+
                        Motor1(0,false);
+
                        Motor2(0,false);
+
                        break;
+
                                 
+
          }   
+
       
+
      }
+
         
+
  }                         
+
 
}
 
}
 
</pre>
 
</pre>
 +
 
==引脚分配图==
 
==引脚分配图==
 
{|border="1" cellspacing="0" align="center" cellpadding="5" width="600px"
 
{|border="1" cellspacing="0" align="center" cellpadding="5" width="600px"

2017年1月9日 (一) 17:18的版本

L298P Shield直流马达驱动



产品参数

  • 尺寸:65*55mm
  • 重量:25g
  • 逻辑电压:5V
  • 端口:数字
  • 驱动方式:双路H桥
  • 驱动电压:7-12V (7-18V单独供电)
  • 驱动电流:<2A
  • 平台:Arduino

产品特性

  • 全新原装正品L298P芯片
  • 双出输出,电流最大2A
  • 高速肖特续流基二极管,性能稳定
  • 4IO控制两路马达转向、调速,节省IO
  • 管脚完全兼容Arduino UNO R3
  • 适用于智能机器人小车的电机驱动
  • 可配合我店的MR2智能小车平台使用

使用教程

  • L298P Shield直流马达驱动LINE.jpg

样例代码

//This motor shield use Pin 6,5,7,4 to control the motor
// Simply connect your motors to M1+,M1-,M2+,M2-
// Through serial monitor, type 'a','s', 'w','d','x' to control the motor

int EN1 = 6;
int EN2 = 5;  //Roboduino Motor shield uses Pin 9
int IN1 = 7;
int IN2 = 4; //Latest version use pin 4 instead of pin 8

void Motor1(int pwm, boolean reverse) {
  analogWrite(EN1, pwm); //set pwm control, 0 for stop, and 255 for maximum speed
  if (reverse) {
    digitalWrite(IN1, HIGH);
  }
  else {
    digitalWrite(IN1, LOW);
  }
}

void Motor2(int pwm, boolean reverse) {
  analogWrite(EN2, pwm);
  if (reverse) {
    digitalWrite(IN2, HIGH);
  }
  else {
    digitalWrite(IN2, LOW);
  }
}

void setup() {
  int i;
  for (i = 4; i <= 7; i++) //For Arduino Motor Shield
    pinMode(i, OUTPUT);  //set pin 4,5,6,7 to output mode
}

void loop() {
  Motor1(255, true); //You can change the speed, such as Motor(50,true)
  Motor2(255, true);
  delay(500);
  Motor1(255, false);
  Motor2(255, false);
  delay(500);
  Motor1(150, false);
  Motor2(150, true);
  delay(500);
  Motor1(150, true);
  Motor2(150, false );
  delay(500);
}

引脚分配图

Pin Function
Digital 4 Motor 2 转向控制,高电平正转,低电平反转
Digital 5 Motor 2 PWM调速控制,范围0-255,0电机停转,255电机最大速度
Digital 6 Motor 1 PWM调速控制,范围0-255,0电机停转,255电机最大速度
Digital 7 Motor 1 转向控制,高电平正转,低电平反转


更多

[YWRobot产品资料下载]

购买 YWRobot商城购买链接