โค้ด Arduino และวิดีโอ: ตัวควบคุมมอเตอร์ DC L293D (ไม่มีการควบคุมความเร็ว)
บทช่วยสอนนี้ครอบคลุมวิธีการใช้ตัวควบคุมมอเตอร์ L293D ร่วมกับ Arduino เพื่อควบคุมมอเตอร์กระแสตรง L293D ช่วยให้คุณหมุนมอเตอร์ได้ทั้งตามเข็มนาฬิกาและทวนเข็มนาฬิกา โดยทำตามคำแนะนำนี้ คุณจะได้เรียนรู้วิธีการเชื่อมต่ออุปกรณ์และเขียนโปรแกรม Arduino เพื่อให้ได้การควบคุมมอเตอร์ตามที่ต้องการ
ในโปรเจกต์นี้ เราจะใช้ไดรเวอร์มอเตอร์ L293D เพื่อควบคุมมอเตอร์กระแสตรงหนึ่งตัว มอเตอร์จะสามารถสลับทิศทางตามคำสั่งที่กำหนดในโค้ด Arduino เพื่อความเข้าใจที่ชัดเจนยิ่งขึ้นเกี่ยวกับการตั้งค่าและโค้ด ควรดูวิดีโอที่เกี่ยวข้อง (ในวิดีโอที่ 00:00)
อธิบายฮาร์ดแวร์
L293D เป็นวงจรรวมที่ออกแบบมาสำหรับขับมอเตอร์ สามารถควบคุมมอเตอร์กระแสตรงสองตัวพร้อมกันหรือมอเตอร์สเต็ปเปอร์หนึ่งตัว ชิปใช้การกำหนดค่าแบบ H-bridge เพื่อให้สามารถควบคุมทิศทางได้โดยการสลับขั้วของแรงดันไฟฟ้าที่จ่ายให้มอเตอร์ ความยืดหยุ่นนี้ทำให้เป็นตัวเลือกที่ยอดเยี่ยมสำหรับโปรเจกต์หุ่นยนต์และระบบอัตโนมัติ
นอกจาก L293D แล้ว เราจะใช้บอร์ด Arduino เพื่อส่งสัญญาณควบคุมไปยังไดรเวอร์มอเตอร์ Arduino จะสร้างระดับลอจิกที่จำเป็นเพื่อเปิดใช้งานมอเตอร์และกำหนดทิศทางการหมุน การตั้งค่านี้ยังต้องใช้มอเตอร์กระแสตรงและแหล่งจ่ายไฟที่เข้ากันได้กับข้อกำหนดแรงดันไฟฟ้าของมอเตอร์
รายละเอียดจากดาต้าชีต
| ผู้ผลิต | Texas Instruments |
|---|---|
| หมายเลขชิ้นส่วน | L293D |
| แรงดันไฟฟ้าลอจิก/IO | 5 V |
| แรงดันไฟฟ้าจ่าย | 4.5 - 36 V |
| กระแสเอาต์พุต (ต่อช่อง) | 600 mA |
| กระแสสูงสุด (ต่อช่อง) | 1.2 A |
| คำแนะนำความถี่ PWM | 20 kHz |
| เกณฑ์ลอจิกอินพุต | 2.5 V (โดยทั่วไป) |
| แรงดันตก / RDS(on) / ความอิ่มตัว | 1.5 V (สูงสุด) |
| ขีดจำกัดความร้อน | 150 °C (สูงสุด) |
| แพ็คเกจ | 16-DIP |
| หมายเหตุ / รุ่นต่างๆ | สามารถควบคุมมอเตอร์สองตัวได้ |
- ตรวจสอบให้แน่ใจว่าแรงดันไฟฟ้าจ่ายไม่เกิน 36 V
- ใช้แผ่นระบายความร้อนที่เหมาะสมเมื่อทำงานใกล้กระแสสูงสุด
- เชื่อมต่อกราวด์ของ Arduino และไดรเวอร์มอเตอร์เสมอ
- ตรวจสอบการเดินสายไฟเพื่อหลีกเลี่ยงการเชื่อมต่อขั้วกลับด้าน
- ใช้สัญญาณ PWM เพื่อควบคุมความเร็วหากจำเป็นในโปรเจกต์อนาคต
คำแนะนำการเดินสายไฟ


ในการเดินสายไฟตัวควบคุมมอเตอร์ L293D เข้ากับ Arduino ให้เริ่มต้นด้วยการเชื่อมต่อไฟและกราวด์ เชื่อมต่อพิน 4 และพิน 5 ของ L293D เข้ากับกราวด์ จากนั้นเชื่อมต่อพิน 1 (Vcc1) เข้ากับเอาต์พุต 5 V ของ Arduino สำหรับ Vcc2 (พิน 8) ให้เชื่อมต่อกับแหล่งจ่ายไฟภายนอก โดยตรวจสอบให้แน่ใจว่าตรงกับข้อกำหนดแรงดันไฟฟ้าของมอเตอร์
ถัดไป เชื่อมต่อพินเปิดใช้งาน (พิน 1) เข้ากับพิน 8 บน Arduino เพื่อควบคุมกำลังของมอเตอร์ สำหรับอินพุตควบคุมมอเตอร์ ให้เชื่อมต่อพิน 2 (1A) ของ L293D เข้ากับพิน 2 บน Arduino และพิน 7 (2A) เข้ากับพิน 7 บน Arduino สุดท้าย เชื่อมต่อขั้วมอเตอร์เข้ากับพิน 3 (1Y) และพิน 6 (2Y) บน L293D
ตัวอย่างโค้ดและคำอธิบาย
โค้ดต่อไปนี้เริ่มต้นพินและตั้งค่าการควบคุมมอเตอร์ ตัวระบุที่ใช้ประกอบด้วย P1A สำหรับพินควบคุมมอเตอร์ตัวแรกและ EN12 สำหรับพินเปิดใช้งาน ฟังก์ชัน setup กำหนดค่าพินเป็นเอาต์พุต
void setup() {
Serial.begin(9600); // ตั้งค่า Serial Monitor เพื่อแสดงข้อมูล
pinMode(P1A, OUTPUT); // กำหนดพินเป็น OUTPUT สำหรับ P1A
pinMode(P2A, OUTPUT); // กำหนดพินเป็น OUTPUT สำหรับ P2A
pinMode(EN12, OUTPUT); // กำหนดพินเป็น OUTPUT สำหรับ 1,2EN enable
}
ในลูป มอเตอร์จะถูกสั่งให้หมุนทั้งสองทิศทางโดยมีเวลาหน่วงระหว่างนั้น สถานะของมอเตอร์จะถูกพิมพ์ไปยัง serial monitor โดยใช้ Serial.println
void loop() {
Serial.println("Rotating CW");
digitalWrite(EN12, HIGH); // เปิดใช้งาน 1A และ 2A
digitalWrite(P1A, HIGH); // ส่งสัญญาณ HIGH ไปที่ P1A
digitalWrite(P2A, LOW); // ส่งสัญญาณ LOW ไปที่ P2A
delay(3000); // มอเตอร์ทำงานเป็นเวลา 3 วินาที
digitalWrite(EN12, LOW); // ปิดการใช้งาน 1A และ 2A
}
ข้อความนี้แสดงวิธีการควบคุมทิศทางการหมุนของมอเตอร์โดยการเปลี่ยนระดับลอจิกที่ส่งไปยัง P1A และ P2A มอเตอร์จะทำงานเป็นเวลา 3 วินาทีก่อนหยุดและสลับทิศทาง
นี่คือวิธีที่ไดรเวอร์ฮาล์ฟบริดจ์ L293D ถูกใช้งาน:
การสาธิต / สิ่งที่คาดหวัง
เมื่อการตั้งค่าเสร็จสมบูรณ์และอัปโหลดโค้ดแล้ว คุณควรสังเกตเห็นมอเตอร์หมุนในทิศทางหนึ่งเป็นเวลา 3 วินาที หยุดเป็นเวลา 2 วินาที จากนั้นหมุนในทิศทางตรงกันข้ามอีก 3 วินาที สถานะของมอเตอร์จะถูกพิมพ์บนซีเรียลมอนิเตอร์เพื่อยืนยันการทำงาน (ในวิดีโอที่เวลา 12:00)
ข้อผิดพลาดทั่วไปที่ควรระวัง ได้แก่ การตรวจสอบให้แน่ใจว่าแรงดันไฟฟ้าที่จ่ายให้มอเตอร์ถูกต้อง และการตรวจสอบว่าการเชื่อมต่อมอเตอร์ถูกต้อง หากมอเตอร์ไม่ตอบสนอง ให้ตรวจสอบการเดินสายไฟและการเชื่อมต่อแหล่งจ่ายไฟอีกครั้ง
ไทม์สแตมป์ในวิดีโอ
- 00:00 - บทนำ
- 02:30 - คำอธิบายการเดินสายไฟ
- 05:00 - อธิบายโค้ดทีละขั้นตอน
- 10:00 - สาธิตการควบคุมมอเตอร์
++
/*
* This is the Arduino code L293 DC motor driver (Advanced).
Using this code you can control a motor to rotate in both directions: clockwise (CW)
and counter-clockwise (CCW).
* *
// Written by Ahmad S. for Robojax.com on
// February 25, 2018 at 18:30 in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
* watch L293 video for details https://youtu.be/jAmDliHcTJ0
* Code is available at http://robojax.com/learn/arduino
*
*/
// DC motor 1 control
#define P1A 2 // define pin 2 as for P1A
#define P2A 7 // define pin 7 as for P2A
#define EN12 8 // define pin 8 as for 1,2EN enable
// DC motor 2 control
#define P3A 10 // define pin 10 as for P3A
#define P4A 13 // define pin 13 as for P4A
#define EN34 9 // define pin 9 as for EN3 and EN4 enable
/*
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
*/
void setup() {
// L293 Motor Control Code by Robojax.com 2018025
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
pinMode(P3A, OUTPUT);// define pin as OUTPUT for P3A
pinMode(P4A, OUTPUT);// define pin as OUTPUT for P4A
pinMode(EN34, OUTPUT);// define pin as OUTPUT for 3,4EN
// L293 Motor Control Code by Robojax.com 2018025
}
void loop() {
// L293 Motor Control Code by Robojax.com 2018025
Serial.println("Rotating CW");
digitalWrite(EN12 ,HIGH);// Enable 1A and 2A
digitalWrite(P1A,HIGH);// send + or HIGH signal to P1A
digitalWrite(P2A,LOW);// send - or LOW signal to P2A
delay(3000);// motor runs for 3 seconds
digitalWrite(EN12 ,LOW);// Disable 1A and 2A
delay(2000);// motor stop for 3 seconds
Serial.println("Motor Stopped");
// L293 Motor Control Code by Robojax.com 2018025
// now changing the direction of rotation of motor
Serial.println("Rotating CCW");
digitalWrite(EN12 ,HIGH);// Enable 1A and 2A
digitalWrite(P1A,LOW);// send + or HIGH signal to P1A
digitalWrite(P2A,HIGH);// send - or LOW signal to P2A
delay(3000);// motor runs for 3 seconds
digitalWrite(EN12 ,LOW);// Disable 1A and 2A
delay(2000);// motor stop for 3 seconds
Serial.println("Motor Stopped");
Serial.println("=========== Loop done");
delay(500);
// L293 Motor Control Code by Robojax.com 2018025
}
/*
* This is the Arduino code L293 DC motor driver (Advanced).
Using this code you can control a motor to rotate in both directions: clockwise (CW)
and counter-clockwise (CCW).
* *
// Written by Ahmad S. for Robojax.com on
// February 25, 2018 at 18:30 in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
* Watch L293 video for details: https://youtu.be/jAmDliHcTJ0
* Code is available at: http://robojax.com/learn/arduino
*
*/
// DC motor 1 control
#define P1A 2 // define pin 2 as for P1A
#define P2A 7 // define pin 7 as for P2A
#define EN12 8 // define pin 8 as for 1,2EN enable
// DC motor 2 control
#define P3A 10 // define pin 10 as for P3A
#define P4A 13 // define pin 13 as for P4A
#define EN34 9 // define pin 9 as for EN3 and EN4 enable
/*
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
*/
void setup() {
// L293 Motor Contro Code by Robojax.com 2018025
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
pinMode(P3A, OUTPUT);// define pin as OUTPUT for P3A
pinMode(P4A, OUTPUT);// define pin as OUTPUT for P4A
pinMode(EN34, OUTPUT);// define pin as OUTPUT for 3,4EN
// L293 Motor Contro Code by Robojax.com 2018025
}
void loop() {
if(value == 45){
rotateMA("CW",HIGH); // start motor in CW
}
delay(3000);
rotateMA("CW",LOW); // stop motor
delay(2000);
rotateMA("CCW",HIGH); // start motor in CCW
delay(3000);
rotateMA("CW",LOW); /// stop motor
delay(2000);
// L293 Motor Contro Code by Robojax.com 2018025
}// loop ends
/**
* function: rotateMA(String di,int action)
* Sends power to motor A
* di is a string and can be "CW" or "CCW"
* to use:
* rotateMA("CW",HIGH);// to rotate motor CW
* rotateMA("CCW",HIGH);// to rotate motor CCW
* to stop the motor:
* rotateMA("CW",LOW);// to stop motor
*/
void rotateMA(String di,int endis)
{
// L293 Motor Contro Code by Robojax.com 2018025
if(di =="CW"){
Serial.println(" Rotating CW");
digitalWrite(EN12 ,endis);// Enable 1A and 2A
digitalWrite(P1A,HIGH);// send + or HIGH signal to P1A
digitalWrite(P2A,LOW);// send - or LOW signal to P2A
}else{
// L293 Motor Contro Code by Robojax.com 2018025
// now changing the direction of rotation of motor
Serial.println(" Rotating CCW");
digitalWrite(EN12 ,endis);// Enable 1A and 2A
digitalWrite(P1A,LOW);// send + or HIGH signal to P1A
digitalWrite(P2A,HIGH);// send - or LOW signal to P2A
}
// L293 Motor Contro Code by Robojax.com 2018025
}//rotateMA ends here
สิ่งที่คุณอาจต้องการ
-
อเมซอนPurchase L293D motor driver ICamzn.to
-
อาลีเอ็กซ์เพรสPurchase L293D motor driver IC from AliExpresss.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
ยังไม่มีทรัพยากรเลย
ไฟล์📁
แผ่นข้อมูล (pdf)
-
robojax_L293D_motor_shield_manual.pdf
robojax_L293D_motor_shield_manual.pdf0.16 MB -
L293d datasheet
L293d_datasheet.pdf0.34 MB