IN1-IN4 控制转动的方向,IN1为低电平, IN2为高电平, 电机反转;IN1为高电平,IN2 为低电平,电机正转;IN3和IN4控制另一轮。
为了方便,把EN使动端与跳线连接(高电平),然后使用IN的状态控制走停
ENA |
IN1 |
IN2 |
运行状态 |
0 |
任意 |
任意 |
停止 |
1 |
0 |
1 |
正转 |
1 |
1 |
0 |
反转 |
1 |
1 |
1 |
刹停 |
1 |
0 |
0 |
停止 |
使用了航模专用的锂电池,主要考虑到它可以充电,标示为7.2V,充电后测量为8.4V,电池接电机控制模块的VCC,然后用电机控制模块输出的5V给Arduino板供电。输入电压在6V以下时,它就不能给Arduino板供电了。
直流电机驱动模块,Arduino uno,杜邦线,电池,智能小车套装(含车架,车轮,电动机等)
电机驱动模块IN1-IN4 接单片机: IN1-D6 IN2-D7 IN3-D4 IN4-D5
电机驱动模块GND 接电源GND 和 单片机GND
电机驱动模块VCC 接电源VCC
电机驱动模块+5 接单片机 VIN
电机驱动模块OUT1-OUT2接步进电机1,OUT3-OUT4 接步进电机2
#define PIN_CAR_IN1 5
#define PIN_CAR_IN2 6
#define PIN_CAR_IN3 9
#define PIN_CAR_IN4 10
void doForward() {
digitalWrite(PIN_CAR_IN3,LOW);
digitalWrite(PIN_CAR_IN4,HIGH);
digitalWrite(PIN_CAR_IN1,LOW);
digitalWrite(PIN_CAR_IN2,HIGH);
}
void doBackward() {
digitalWrite(PIN_CAR_IN3,HIGH);
digitalWrite(PIN_CAR_IN4,LOW);
digitalWrite(PIN_CAR_IN1,HIGH);
digitalWrite(PIN_CAR_IN2,LOW);
}
void doStop() {
digitalWrite(PIN_CAR_IN3,LOW);
digitalWrite(PIN_CAR_IN4,LOW);
digitalWrite(PIN_CAR_IN1,LOW);
digitalWrite(PIN_CAR_IN2,LOW);
digitalWrite(PIN_CAR_IN3,HIGH);
digitalWrite(PIN_CAR_IN4,HIGH);
digitalWrite(PIN_CAR_IN1,HIGH);
digitalWrite(PIN_CAR_IN2,HIGH);
}
void doLeft() {
digitalWrite(PIN_CAR_IN3,HIGH);
digitalWrite(PIN_CAR_IN4,LOW);
digitalWrite(PIN_CAR_IN1,LOW);
digitalWrite(PIN_CAR_IN2,HIGH);
}
void doRight() {
digitalWrite(PIN_CAR_IN3,LOW);
digitalWrite(PIN_CAR_IN4,HIGH);
digitalWrite(PIN_CAR_IN1,HIGH);
digitalWrite(PIN_CAR_IN2,LOW);
}
void setup()
{
pinMode(PIN_CAR_IN1,OUTPUT);
pinMode(PIN_CAR_IN2,OUTPUT);
pinMode(PIN_CAR_IN3,OUTPUT);
pinMode(PIN_CAR_IN4,OUTPUT);
digitalWrite(PIN_CAR_IN1, OUTPUT);
digitalWrite(PIN_CAR_IN2, OUTPUT);
digitalWrite(PIN_CAR_IN3, OUTPUT);
digitalWrite(PIN_CAR_IN4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
doForward();
delay(1000);
doStop();
delay(1000);
doBackward();
delay(1000);
doStop();
delay(1000);
doLeft();
delay(1000);
doStop();
delay(1000);
doRight();
delay(1000);
doStop();
delay(1000);
}
(请注意:因各厂商硬件不同,引脚位置可能有差异,具体请见硬件说明书)