Hướng dẫn sử dụng mạch cầu H-Bridge Motor Driver [L293D] để làm động cơ xe.
* Mã code
#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2
#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
#define I3 12 // Control pin 1 for motor 2
#define I4 13 // Control pin 2 for motor 2
int speed =255;
void setup() {
Serial.begin(9600);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
}
//--------------
// motor 1
void E1_Dung() {
digitalWrite(E1, LOW);
}
void E1_Tien(int speed) { //speed: từ 0 - MAX_SPEED
analogWrite(E1, speed); // Run in full speed
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
}
void E1_Lui(int speed) {
analogWrite(E1, speed); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
}
//--------------
// motor 2
void E2_Dung() {
digitalWrite(E2, LOW);
}
void E2_Tien(int speed) { //speed: từ 0 - MAX_SPEED
analogWrite(E2, speed); // Run in full speed
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void E2_Lui(int speed) {
analogWrite(E2, speed); // Run in full speed
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
//-------------------
void loop() {
int state = Serial.read();
if(state>0)
switch (state) {
case '1':// Tien
E1_Tien(speed);
E2_Tien(speed);
Serial.println("Tien");
break;
case '2':// Lui
E1_Lui(speed);
E2_Lui(speed);
Serial.println("Lui");
break;
case '3'://Dung
E1_Dung();
E2_Dung();
Serial.println("Dung");
break;
case '4':// Trai
E1_Dung();
E2_Tien(speed);
Serial.println("Quay Trai");
break;
case '5':// Phai
E1_Tien(speed);
E2_Dung();
Serial.println("Quay Phai");
break;
default:
break;
}
}
* Mô hình mạch
Link Mạch
* Mã code
#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2
#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
#define I3 12 // Control pin 1 for motor 2
#define I4 13 // Control pin 2 for motor 2
int speed =255;
void setup() {
Serial.begin(9600);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
}
//--------------
// motor 1
void E1_Dung() {
digitalWrite(E1, LOW);
}
void E1_Tien(int speed) { //speed: từ 0 - MAX_SPEED
analogWrite(E1, speed); // Run in full speed
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
}
void E1_Lui(int speed) {
analogWrite(E1, speed); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
}
//--------------
// motor 2
void E2_Dung() {
digitalWrite(E2, LOW);
}
void E2_Tien(int speed) { //speed: từ 0 - MAX_SPEED
analogWrite(E2, speed); // Run in full speed
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void E2_Lui(int speed) {
analogWrite(E2, speed); // Run in full speed
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
//-------------------
void loop() {
int state = Serial.read();
if(state>0)
switch (state) {
case '1':// Tien
E1_Tien(speed);
E2_Tien(speed);
Serial.println("Tien");
break;
case '2':// Lui
E1_Lui(speed);
E2_Lui(speed);
Serial.println("Lui");
break;
case '3'://Dung
E1_Dung();
E2_Dung();
Serial.println("Dung");
break;
case '4':// Trai
E1_Dung();
E2_Tien(speed);
Serial.println("Quay Trai");
break;
case '5':// Phai
E1_Tien(speed);
E2_Dung();
Serial.println("Quay Phai");
break;
default:
break;
}
}
* Mô hình mạch
No comments:
Post a Comment