Lab 8 Servo Motor

จุดประสงค์การเรียนรู้

สามารถเขียนCodeภาษาCในArduino IDEควบคุมการองศาของServo motorได้

สาระการเรียนรู้

1. Servo Motor

เซอร์โวมอเตอร์เป็นมอเตอร์ที่ประกอบด้วยไฟฟ้าคอลโทรลและกลไกในตัวเอง โดยปกติสามารถควบคุมหรือล็อคองศาการทำงานได้ การใช้งานจะใช้ในการควบคุมตำแหน่ง, ความเร็ว, แรงบิด และความแม่นยำให้มีการทำงานสอดคล้องกันอย่างมีประสิทธิภาพ โดยอาศัยวงจรแบบป้อนกลับ(Feedback Controller)

- Feedback Controller คือ ระบบควบคุมี่มีการวัดค่าOutputของระบบ แล้วนำมาเปรียบเทียบค่าInputเพื่อควบคุมและปรับแต่งให้ค่าOutputของระบบมีค่าเท่ากับหรือใกล้เคียงกับค่าInput

- ชนิดของServo Motor หากแบ่งตามชนิดของแหล่งจ่ายไฟจะแบ่งเป็น Servo Motorกระแสตรง และServo Motorกระแสสลับ

- Servo Motor ที่มีขายในทั้งตลาดโดยทั่วไปจะมีแบบทำงานระหว่าง 0องศา - 90 องศา,0องศา - 180 องศา,0องศา - 270 องศา ส่วนสำหรับServo Motorชนิด 360 องศา จะเป็นรุ่นดัดแปลงโดยปกติจะไม่สามารถล็อคตำแหน่งมุมได้เหมือนรุ่นอื่นๆ

DatasheetของสีสายไฟของServo Digitalขนาดเล็กโดยทั่วไป

2. คำสั่งการใช้งาน

รูปแบบการใช้งานคำสั่งในภาษาซีของArduino IDEในเนื้อหานี้ จะใช้คำสั่งดังต่อไปนี้

วัสดุอุปกรณ์

1. ไมโครสวิตซ์ ชนิดกดติดปล่อยดับ จำนวน 1 ตัว

2. ตัวต้านทาน 10 กิโลโอห์ม จำนวน 1 ตัว

3. บอร์ด ESP32 Devkit V.1 จำนวน 1 บอร์ด

4. สายMicro USB จำนวน 1 เส้น

5. Breadboard

6. สายแพร Male-Male

7. หลอด LED จำนวน 3 หลอด

8. ตัวต้านทาน 220 โอห์ม จำนวน 3 ตัว

9. ตัวต้านทานปรับค่าได้ Potentiometer จำนวน 1 ตัว

10. ลำโพงBuzzer จำนวน 1 อัน

11. จอแสดงผล OLED I2C จำนวน 1 อัน

12. Servo Motor จำนวน 1 ตัว

13. โมดูลMb 102 Breadboard จำนวน 1 ตัว

14. Adaptor 9Vdc จำนวน 1 ตัว

วิธีทำการทดลอง

1. ต่อวงจรเพิ่มเติมจากLab1-Lab7 โดยใช้Servo Motor จำนวน 1 ตัว และ ต่อเพิ่มเติมตามDatasheetและรูปการต่อวงจรด้านล่าง

ตารางDatasheetการต่อวงจร

1. Datasheet Servo Motor

2. Datasheet MB 102 Breadboard

Mb 102

-

บอร์ดESP32

GND

** หมายเหตุ : ที่ MB 102 ให้นำจัมเปอร์มาต่อระหว่างขา OFF กับ 5V ถึงจะได้ไฟOutput 5V

2. ทำการCopy Codeด้านล่างก่อนUpload code Arduino IDE ลงไปใบนบอร์ด ESP32 Devkit v1 ควรเลือกที่Tool -> Board -> ESP32 Arduino ->ESP32 Dev Module (ดังภาพประกอบด้านล่าง)

Servo 1 ch Arduino IDE Code

#include <ESP32Servo.h>

Servo myservo;

void setup()

{

myservo.attach(32, 500, 2400); // attaches the servo on pin 32 (D32) to the servo object

// using SG90 servo min/max of 500us and 2400us

// for MG995 large servo, use 1000us and 2000us,

// which are the defaults, so this line could be

// "myservo.attach(servoPin);"

}

void loop() {

myservo.write(0);delay(1000);

myservo.write(30);delay(1000);

myservo.write(60);delay(1000);

}

3.หลังจากนั้นให้เข้ามาที่ Tool ->Port แล้วเลือกพอร์ตCom portที่เชื่อมกับUSB ดังรูปข้างล่าง

** กรณีใช้งานใน Ubuntu พอร์ตที่ใช้อาจจะเป็นพอร์ตttyUSB0 ซึ่งจะต้องเลือกเป็นพอร์ตชื่อนี้แทนCom port และก่อนกดUpload ให้ทำการเปิดTerminal โดยเข้าไปที่desktop คลิกขวาตรงพื้นที่ว่าง แล้วเลือก Open Terminal หลังจากนั้นให้พิมพ์ข้อความในTerminalดังนี้เพื่อทำการเปิดพอร์ตUSB : sudo chmod 666 /dev/ttyUSB0 หลังจากนั้นให้กดEnter และทำการกรอกรหัสผ่านของเครื่องพร้อมกับกดปุ่มEnterอีกครั้ง หลังจากนั้นก็Upload codeในArduino IDE

4.ทำการกดปุ่มUpload ที่เป็นรูปหัวลูกศร หรือจะกด Ctrl+u ที่คีย์บอร์ดเพื่อทำการUploadโปรแกรมไปที่บอร์ดESP32 ดังรูปข้างล่าง

5.หลังจากที่Uploadโปรแกรมแล้ว Servo Motor จะทำงานตามองศาแบบวนรอบดังนี้ 0 องศา 30 องศา และ 10 องศา โดยทำงานองศาละ 1 วินาที

ตอนที่ 2 Servo K-nob

เป็นการใช้งานเซอร์โวชนิด0-180องศา ซึ่งทำการmapค่าจากสัญญาณanalog input (AnalogRead) โดยสามารถสังเกตองศาการทำงานของServo Motorที่Serial MonitorของโปรแกรมArduino IDE สามารถUploadการใช้งานโปรแกรมที่Codeข้างล่างได้เลย

Servo K-nob Arduino IDE Code

#include <ESP32Servo.h>

Servo myservo; // create servo object to control a servo

// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33

int servoPin = 32; // GPIO pin used to connect the servo control (digital out)

int potPin = 35; // GPIO pin used to connect the potentiometer (analog in)

int ADC_Max = 4096; // This is the default ADC max value on the ESP32 (12 bit ADC width);

// this width can be set (in low-level oode) from 9-12 bits, for a

// a range of max values of 512-4096

int val; // variable to read the value from the analog pin

void setup()

{

myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 32 (D32) to the servo object

// using SG90 servo min/max of 500us and 2400us

// for MG995 large servo, use 1000us and 2000us,

// which are the defaults, so this line could be

// "myservo.attach(servoPin);"

Serial.begin(115200);

}

void loop() {

val = analogRead(potPin); // read the value of the potentiometer (value between 0 and 1023)

val = map(val, 0, ADC_Max, 0, 180); // scale it to use it with the servo (value between 0 and 180)

Serial.println(val);

myservo.write(val); // set the servo position according to the scaled value

delay(50); // wait for the servo to get there

}