This tutorial is part of: มอเตอร์เซอร์โว
วิดีโอทั้งหมดที่เกี่ยวข้องกับมอเตอร์เซอร์โว แสดงไว้ที่นี่ ลิงก์ไปยังวิดีโออื่นๆ อยู่ด้านล่างบทความนี้
ควบคุมเซอร์โวด้วยปุ่มกดโดยใช้ Arduino
ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีควบคุมมอเตอร์เซอร์โวโดยใช้ปุ่มกดสองปุ่ม ปุ่มหนึ่งสำหรับเลื่อนเซอร์โวไปทางซ้าย และอีกปุ่มหนึ่งสำหรับเลื่อนไปทางขวา เมื่อคุณกดปุ่มขวา เซอร์โวจะเลื่อนไปทางขวา 10 องศา และเมื่อคุณกดปุ่มซ้าย เซอร์โวจะเลื่อนไปทางซ้ายในปริมาณเท่ากัน หากคุณกดปุ่มค้างไว้ เซอร์โวจะเคลื่อนที่ไปในทิศทางนั้นต่อไปจนกว่าจะปล่อยปุ่ม

โปรเจกต์นี้เป็นวิธีที่ดีในการทำความเข้าใจวิธีการเชื่อมต่อปุ่มกดกับมอเตอร์เซอร์โวโดยใช้ Arduino เราจะครอบคลุมส่วนประกอบฮาร์ดแวร์ที่จำเป็น คำแนะนำการเดินสายไฟ และโค้ดที่จำเป็นเพื่อให้ทุกอย่างทำงานได้ เพื่อความเข้าใจที่ชัดเจนยิ่งขึ้น คุณสามารถอ้างอิงวิดีโอที่มาพร้อมกับบทช่วยสอนนี้ (ในวิดีโอที่เวลา 01:00)
อธิบายฮาร์ดแวร์
สำหรับโปรเจกต์นี้ คุณจะต้องใช้บอร์ด Arduino มอเตอร์เซอร์โว และปุ่มกดสองปุ่ม มอเตอร์เซอร์โวมีหน้าที่ในการเคลื่อนไหว ในขณะที่ปุ่มกดทำหน้าที่เป็นอินพุตเพื่อควบคุมทิศทางของการเคลื่อนไหวนั้น Arduino จะประมวลผลอินพุตเหล่านี้และส่งสัญญาณที่เหมาะสมไปยังเซอร์โว
มอเตอร์เซอร์โวทำงานโดยอาศัยสัญญาณมอดูเลตความกว้างพัลส์ (PWM) โดยการส่งความกว้างพัลส์ที่แตกต่างกัน คุณสามารถควบคุมมุมของเซอร์โวได้ ปุ่มกดจะเชื่อมต่อกับพินอินพุตดิจิทัลบน Arduino และเราจะใช้ตัวต้านทานดึงขึ้นภายในเพื่อลดความซับซ้อนของการเดินสายไฟ
รายละเอียดจากดาต้าชีต
| ผู้ผลิต | หลายราย |
|---|---|
| หมายเลขชิ้นส่วน | SG90 |
| แรงดันไฟฟ้าลอจิก/IO | 5 V |
| แรงดันไฟฟ้าจ่าย | 4.8–6 V |
| กระแสเอาต์พุต (ต่อช่อง) | สูงสุด 1.5 A |
| กระแสสูงสุด (ต่อช่อง) | สูงสุด 2.5 A |
| คำแนะนำความถี่ PWM | 50 Hz |
| เกณฑ์ลอจิกอินพุต | 0.3VCC ถึง 0.7VCC |
| แรงดันตก / RDS(on) / ความอิ่มตัว | 0.4 V |
| ขีดจำกัดความร้อน | 85 °C |
| แพ็คเกจ | พลาสติก |
| หมายเหตุ / รุ่นต่างๆ | การหมุนมาตรฐาน 180° |
- ตรวจสอบให้แน่ใจว่าเซอร์โวรองรับแรงดันไฟฟ้าจ่ายของคุณ
- ใช้แผ่นระบายความร้อนที่เหมาะสมหากทำงานที่กระแสสูง
- เชื่อมต่อปุ่มกดเข้ากับกราวด์ด้วยการกำหนดค่าดึงขึ้น
- ดีบาวซ์ปุ่มกดของคุณหากพบพฤติกรรมที่ไม่เสถียร
- ตรวจสอบเอาต์พุตอนุกรมเพื่อการดีบัก
คำแนะนำการเดินสายไฟ

ในการเดินสายไฟส่วนประกอบ ให้เริ่มต้นด้วยการเชื่อมต่อมอเตอร์เซอร์โว เชื่อมต่อสายสีแดงของเซอร์โวเข้ากับพิน 5V บน Arduino สายสีดำเข้ากับพิน GND และสายสัญญาณสีเหลืองหรือสีขาวเข้ากับพิน 9 บน Arduino พินนี้จะควบคุมตำแหน่งของเซอร์โว
ถัดไป เดินสายไฟปุ่มกด เชื่อมต่อขาหนึ่งของปุ่มขวาเข้ากับพิน 2 และอีกขาหนึ่งเข้ากับกราวด์ สำหรับปุ่มซ้าย ให้เชื่อมต่อขาหนึ่งเข้ากับพิน 12 และอีกขาหนึ่งเข้ากับกราวด์เช่นกัน การตั้งค่านี้ช่วยให้ตัวต้านทานดึงขึ้นภายในรักษาพินให้เป็น HIGH เมื่อไม่ได้กดปุ่ม
ตัวอย่างโค้ดและคำอธิบาย
เริ่มต้นด้วยโค้ดสำหรับควบคุมเซอร์โวด้วยปุ่มกดเดียว โค้ดจะเริ่มต้นออบเจกต์เซอร์โวและตั้งค่ามุมเริ่มต้น นี่คือตัวอย่างสั้นๆ:
int angle = 90; // มุมเริ่มต้นสำหรับเซอร์โว
int angleStep = 10; // ขนาดขั้นสำหรับการเคลื่อนที่
ในส่วนนี้ angle แทนตำแหน่งปัจจุบันของเซอร์โว ในขณะที่ angleStep กำหนดว่าเซอร์โวจะเคลื่อนที่เท่าใดในการกดปุ่มแต่ละครั้ง โค้ดนี้จำเป็นสำหรับการติดตามตำแหน่งของเซอร์โว
ถัดไป เรามีฟังก์ชัน setup ที่เราแนบเซอร์โวและกำหนดค่าพินอินพุต:
void setup() {
Serial.begin(9600); // ตั้งค่าอนุกรม
myservo.attach(9); // แนบเซอร์โวเข้ากับพิน 9
pinMode(2, INPUT_PULLUP); // กำหนดค่าพินปุ่มกด
}
ใน setup เราจะเริ่มต้นมอนิเตอร์อนุกรมและแนบเซอร์โวเข้ากับพิน 9 ปุ่มบนพิน 2 ถูกตั้งเป็นอินพุตพร้อมตัวต้านทานดึงขึ้น ซึ่งช่วยลดความซับซ้อนของการเดินสายไฟและหลีกเลี่ยงความจำเป็นในการใช้ตัวต้านทานภายนอก
สุดท้าย เราดูฟังก์ชัน loop ที่จัดการการกดปุ่ม:
while(digitalRead(2) == LOW) {
angle = angle + angleStep; // เพิ่มมุม
myservo.write(angle); // เคลื่อนย้ายเซอร์โว
}
ลูปนี้จะตรวจสอบอย่างต่อเนื่องว่ามีการกดปุ่มหรือไม่ เมื่อกดปุ่ม มุมจะเพิ่มขึ้นตามขั้นที่กำหนด และเซอร์โวจะเคลื่อนที่ตามนั้น สิ่งสำคัญคือต้องแน่ใจว่ามุมอยู่ในช่วงที่กำหนด (0 ถึง 180 องศา) เพื่อป้องกันความเสียหายต่อเซอร์โว
การสาธิต / สิ่งที่คาดหวัง
เมื่อคุณกดปุ่มขวา เซอร์โวควรเคลื่อนที่ไปทางขวาเป็นขั้นๆ ละ 10 องศาจนถึงสูงสุดที่ 180 องศา ในทำนองเดียวกัน การกดปุ่มซ้ายจะเลื่อนไปทางซ้าย หากคุณกดปุ่มค้างไว้ เซอร์โวจะเคลื่อนที่ไปในทิศทางนั้นต่อไปจนกว่าจะปล่อยปุ่ม (ในวิดีโอที่เวลา 01:00)
ข้อผิดพลาดทั่วไป ได้แก่ ปัญหาการเดินสายไฟ เช่น การเชื่อมต่อที่ไม่ถูกต้องหรืออินพุตลอย ตรวจสอบให้แน่ใจเสมอว่าการเชื่อมต่อของคุณแน่นหนาและคุณใช้พินที่ถูกต้องตามที่ระบุในโค้ด
This tutorial is part of: มอเตอร์เซอร์โว
- Control a Servo Motor with a Push Button: Move Servo and Return SPB-1
- Control a Servo Motor with a Push Button: Move Servo in One Direction SPB-2
- Controlling a Servo Motor with a Push Button: Move Servo While Button Is Pressed (SPB-3)
- ควบคุมเซอร์โวด้วยโพเทนชิโอมิเตอร์โดยใช้ Arduino
- ควบคุมเซอร์โวด้วยโพเทนชิโอมิเตอร์และLCD1602 โดยใช้ Arduino
- Controlling Servo Motors Using an Infrared Remote with Arduino
- การควบคุมมอเตอร์เซอร์โว Arduino โดยใช้พอเทนชิโอมิเตอร์
- ควบคุมตำแหน่งเซอร์โวด้วยท่าทางมือสำหรับ Arduino
- Controlling Two or More Servos with Potentiometers Using an Arduino
- How to Control a 360° Servo with Three Push-Button Switches
- วิธีใช้เซอร์โวหมุนต่อเนื่อง 360° กับ Arduino
- Arduino Code and Video for PCA9685 16-Channel 12-Bit Servo Controller V1
- สร้างสวิตช์สลับเซอร์โว Arduino ด้วยปุ่มกด
/*
Controlling a servo with a push button with Arduino
When a push button is pressed, the servo starts moving to the right or left until
it reaches 180 and then returns to 0 degrees.
May 22, 2018 at 01:00
Written by Ahmad S. for Robojax.com in Ajax, Ontario, Canada
Watch a video for this code at https://youtu.be/7woqNH_qby4
This code is taken from http://robojax.com/learn/arduino
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle =90; // initial angle for servo
int angleStep =10;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(2,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
}
void loop() {
while(digitalRead(2) == LOW){
// change the angle for the next time through the loop:
angle = angle + angleStep;
// reverse the direction of the movement at the ends of the angle:
if (angle <= 0 || angle >= 180) {
angleStep = -angleStep;
}
myservo.write(angle); // move the servo to the desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degrees");
delay(100); // waits for the servo to get there
}// while
}
/*
Controlling a servo with two push buttons with Arduino
When the left push button is pressed, the servo starts moving to the left until it reaches 180 (or zero) degrees.
When the right push button is pressed, the servo starts moving to the right until it reaches 180 (or zero) degrees.
At any instance, if the button is released, the servo stops.
May 22, 2018, at 01:00
Written by Ahmad S. for Robojax.com in Ajax, Ontario, Canada
Watch a video for this code at https://youtu.be/7woqNH_qby4
This code is taken from http://robojax.com/learn/arduino
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle =90; // initial angle for servo
int angleStep =5;
#define LEFT 12 // pin 12 is connected to left button
#define RIGHT 2 // pin 2 is connected to right button
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 as input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assign pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println("Robojax Servo Button ");
}
void loop() {
// Servo button demo by Robojax.com
while(digitalRead(RIGHT) == LOW){
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 0;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}// while
// Servo button demo by Robojax.com
while(digitalRead(LEFT) == LOW){
// Servo button demo by Robojax.com
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if(angle >180){
angle =180;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}//
}
สิ่งที่คุณอาจต้องการ
-
อเมซอนServo motor on Amazonamzn.to
-
อาลีเอ็กซ์เพรสPurchase SG90 Servo motor 180 or 360 from AliExpresss.click.aliexpress.com
แหล่งข้อมูลและเอกสารอ้างอิง
ยังไม่มีทรัพยากรเลย
ไฟล์📁
ไม่มีไฟล์ที่พร้อมใช้งาน