Arduino and Motor Control

/*
* Brushed_H_Bridge_simple2 sketch
* commands from serial port control motor direction
* + or - set the direction, any other key stops the motors
*/
const int in1Pin = 5; // H-Bridge input pins
const int in2Pin = 4;
const int in3Pin = 3; // H-Bridge pins for second motor
const int in4Pin = 2;
void setup()
{
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
Serial.println("+ - sets direction of motors, any other key stops motors");
}
void loop()
{
if ( Serial.available()) {
char ch = Serial.read();
if (ch == '+')
{
Serial.println("CW");
// first motor
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,HIGH);
//second motor
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,HIGH);
}
else if (ch == '-')
{
Serial.println("CCW");
digitalWrite(in1Pin,HIGH);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);
digitalWrite(in4Pin,LOW);
}
else
{
Serial.print("Stop motors");
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
}
}
}วงจรที่เห็นนี้ทำหน้าที่ได้ทั้งคุมทิศทางและความเร็วของมอเตอร์นะครับ มาว่ากันทีละส่วน เริ่มจากการคุมทิศทางการหมุนก่อนครับ
โดยปกติหากต้องการกลับทิศการหมุนของมอเตอร์กระแสตรง วิธีนึงที่ทำได้คือ กลับทิศแหล่งจ่าย ทีนี้ลองดูที่รูปวงจร H-Bridge ด้านบนนะครับ
- หากต้องการให้หมุนตามเข็ม (Clockwise :CW) ก็ให้ S1 และ S4 ปิดวงจร และให้ S2 และ S3 เปืดวงจร
- หากต้องการให้หมุนทวนเข็ม (Conter Clockwise :CCW) ก็ให้ S2และ S3 ปิดวงจร และให้ S1 และ S4 เปืดวงจร
จะเห็นว่าสวิตช์จะทำงานเป็นคู่นะครับ S1 คู่กับ S4 และ S2 คู่กับ S3 คู่แรกทำงาน คู่สองต้องเปิดวงจร และในทางตรงข้ามก็คือคู่สองทำงาน คู่แรกต้องเปิดวงจร
แหล่งที่มา
https://www.arduitronics.com/article/22/arduino-and-motor-control-part-1
ความคิดเห็น
แสดงความคิดเห็น