Search Code

ควบคุมเซอร์โวด้วยปุ่มกดโดยใช้ Arduino

ควบคุมเซอร์โวด้วยปุ่มกดโดยใช้ Arduino

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

โปรเจกต์นี้เป็นวิธีที่ดีในการทำความเข้าใจวิธีการเชื่อมต่อปุ่มกดกับมอเตอร์เซอร์โวโดยใช้ Arduino เราจะครอบคลุมส่วนประกอบฮาร์ดแวร์ที่จำเป็น คำแนะนำการเดินสายไฟ และโค้ดที่จำเป็นเพื่อให้ทุกอย่างทำงานได้ เพื่อความเข้าใจที่ชัดเจนยิ่งขึ้น คุณสามารถอ้างอิงวิดีโอที่มาพร้อมกับบทช่วยสอนนี้ (ในวิดีโอที่เวลา 01:00)

อธิบายฮาร์ดแวร์

สำหรับโปรเจกต์นี้ คุณจะต้องใช้บอร์ด Arduino มอเตอร์เซอร์โว และปุ่มกดสองปุ่ม มอเตอร์เซอร์โวมีหน้าที่ในการเคลื่อนไหว ในขณะที่ปุ่มกดทำหน้าที่เป็นอินพุตเพื่อควบคุมทิศทางของการเคลื่อนไหวนั้น Arduino จะประมวลผลอินพุตเหล่านี้และส่งสัญญาณที่เหมาะสมไปยังเซอร์โว

มอเตอร์เซอร์โวทำงานโดยอาศัยสัญญาณมอดูเลตความกว้างพัลส์ (PWM) โดยการส่งความกว้างพัลส์ที่แตกต่างกัน คุณสามารถควบคุมมุมของเซอร์โวได้ ปุ่มกดจะเชื่อมต่อกับพินอินพุตดิจิทัลบน Arduino และเราจะใช้ตัวต้านทานดึงขึ้นภายในเพื่อลดความซับซ้อนของการเดินสายไฟ

รายละเอียดจากดาต้าชีต

ผู้ผลิตหลายราย
หมายเลขชิ้นส่วนSG90
แรงดันไฟฟ้าลอจิก/IO5 V
แรงดันไฟฟ้าจ่าย4.8–6 V
กระแสเอาต์พุต (ต่อช่อง)สูงสุด 1.5 A
กระแสสูงสุด (ต่อช่อง)สูงสุด 2.5 A
คำแนะนำความถี่ PWM50 Hz
เกณฑ์ลอจิกอินพุต0.3VCC ถึง 0.7VCC
แรงดันตก / RDS(on) / ความอิ่มตัว0.4 V
ขีดจำกัดความร้อน85 °C
แพ็คเกจพลาสติก
หมายเหตุ / รุ่นต่างๆการหมุนมาตรฐาน 180°

  • ตรวจสอบให้แน่ใจว่าเซอร์โวรองรับแรงดันไฟฟ้าจ่ายของคุณ
  • ใช้แผ่นระบายความร้อนที่เหมาะสมหากทำงานที่กระแสสูง
  • เชื่อมต่อปุ่มกดเข้ากับกราวด์ด้วยการกำหนดค่าดึงขึ้น
  • ดีบาวซ์ปุ่มกดของคุณหากพบพฤติกรรมที่ไม่เสถียร
  • ตรวจสอบเอาต์พุตอนุกรมเพื่อการดีบัก

คำแนะนำการเดินสายไฟ

Arduino wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons

ในการเดินสายไฟส่วนประกอบ ให้เริ่มต้นด้วยการเชื่อมต่อมอเตอร์เซอร์โว เชื่อมต่อสายสีแดงของเซอร์โวเข้ากับพิน 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)

ข้อผิดพลาดทั่วไป ได้แก่ ปัญหาการเดินสายไฟ เช่น การเชื่อมต่อที่ไม่ถูกต้องหรืออินพุตลอย ตรวจสอบให้แน่ใจเสมอว่าการเชื่อมต่อของคุณแน่นหนาและคุณใช้พินที่ถูกต้องตามที่ระบุในโค้ด

ภาพ

Control a servo with a single push button using Arduino
Control Servo with single push button with Arduino
SG90_servo_motor-1
SG90_servo_motor-1
SG90_servo_motor-0
SG90_servo_motor-0
Arduino wriing for Sevo motor with 2 push buttons
Arduino wriing for Sevo motor with 2 push buttons
98-Control a servo with a single push button using Arduino
ภาษา: C++
/*
 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

  
}
99-Control a servo with two push buttons using Arduino
ภาษา: C++
/*
 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
  }// 

  
}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ยังไม่มีทรัพยากรเลย

ไฟล์📁

ไม่มีไฟล์ที่พร้อมใช้งาน