หน้าแรกHome › Arduino › บทที่ 06Lesson 06
เซอร์โวมอเตอร์ (Servo)Servo Motor
ควบคุมตำแหน่งมอเตอร์Turn a motor to an angle
บทนี้เราจะได้...In this lesson you will...
- ต่อ servo 3 สาย: ไฟเลี้ยง กราวด์ และสัญญาณWire a servo's 3 wires: power, ground and signal
- ใช้ไลบรารี Servo.h สั่งให้หมุนไปที่มุมที่ต้องการUse the Servo.h library to turn to an angle
- เข้าใจว่า servo หมุนได้ 0° ถึง 180°Know a servo turns from 0° to 180°
อุปกรณ์ที่ใช้What you need
บอร์ดส่งพัลส์ทุก 20 ms ความกว้างพัลส์ 1 ms = 0° และ 2 ms = 180° ข้างใน servo มีตัวต้านทานปรับค่าได้ติดกับแกน คอยวัดมุมจริงแล้วหมุนจนตรงกับที่สั่งThe board sends a pulse every 20 ms: 1 ms wide = 0°, 2 ms = 180°. Inside, a small knob-resistor on the shaft measures the real angle and the motor turns until it matches.
โต๊ะทดลอง: ต่อวงจร แล้วรันโค้ดLab bench: wire it, then run the code
ลากจากขาหรือช่องหนึ่งไปอีกช่องเพื่อต่อสาย · ชี้ที่ช่องเพื่อดูว่าช่องไหนเชื่อมกัน · แตะที่สายเพื่อลบ · แตะที่ LED เพื่อกลับขาDrag from one pin or hole to another to add a wire · point at a hole to see what's connected · tap a wire to remove it · tap an LED to flip it
ภารกิจMissions
อ่านโค้ดทีละบรรทัดReading the code, line by line
ช่องโค้ดเริ่มจากโครงเปล่า ปิ๊งจะบอกทีละบรรทัดว่าต้องพิมพ์อะไร ตอนกดรัน บรรทัดที่บอร์ดกำลังทำจะเป็นสีเหลือง คำสั่งสำคัญของบทนี้มีดังนี้The code box starts as an empty sketch, and Ping tells you what to type line by line. While it runs, the line the board is doing turns yellow. The key commands in this lesson:
#include <Servo.h>ดึงไลบรารี (ชุดคำสั่งสำเร็จรูป) ของ servo มาใช้Bring in the servo library (ready-made commands)Servo myServo;สร้าง servo ในโค้ดหนึ่งตัว ตั้งชื่อว่า myServoMake one servo in the code, called myServomyServo.attach(9)บอกว่า servo ต่อสายสัญญาณไว้ที่ขา 9Say the servo's signal wire is on pin 9myServo.write(90)สั่งให้หมุนไปที่มุม 90 องศาTurn to 90 degreesลองแก้เองดูสิTry changing it
แก้โค้ดในช่องโค้ดของโต๊ะทดลองด้านบนได้เลย แล้วกด ▶ รันโค้ด ดูผล อยากเริ่มใหม่ทั้งหมด กด "ล้างโค้ด"Edit the code in the lab bench's code box above, then press ▶ Run code. Want to start over? Press "Clear code".
- ให้หมุนแค่ 0° กับ 45° สลับกันเหมือนโบกมือTurn only between 0° and 45°, like waving
- ทำให้หมุนช้า ๆ ทีละ 10° ด้วยการเขียน write หลายบรรทัดTurn slowly, 10° at a time, with several write lines
- ท้าทาย: ใช้
for (int a = 0; a <= 180; a++)ให้หมุนนุ่ม ๆ ทีละองศาChallenge: usefor (int a = 0; a <= 180; a++)to sweep smoothly one degree at a time
มีบอร์ด Arduino จริง? กดคัดลอกโค้ด ไปวางในโปรแกรม Arduino IDE (ฟรี) แล้วกดอัปโหลดลงบอร์ดได้เลยHave a real Arduino? Copy the code into the free Arduino IDE and upload it to your board.
ควิซท้ายบทQuick quiz
servo ทั่วไปหมุนได้กี่องศา?How far does a usual servo turn?
สายสีไหนของ servo คือ GND?Which servo wire is GND?
คำสั่งไหนสั่งให้ servo หมุนไปที่ 45°?Which command turns the servo to 45°?
#include <Servo.h> มีไว้ทำอะไร?What is #include <Servo.h> for?
ใบงาน: Worksheet: เซอร์โวมอเตอร์ (Servo)Servo Motor
ออกแบบสิ่งประดิษฐ์ที่ใช้ servo เช่น ประตูอัตโนมัติ หรือแขนโบกมือDesign an invention with a servo, like an auto door or a waving arm
ทำครบทุกภารกิจแล้ว?All missions done?
กดปุ่มเพื่อให้จุดบนแผนที่บทเรียนสว่างขึ้นPress the button to light up this lesson on the map.