Arduino UNO R4 Servo Motor điều khiển bằng Potentiometer

Hướng dẫn này sẽ chỉ bạn cách điều khiển servo motor bằng potentiometer sử dụng Arduino Uno R4. Hướng dẫn này trình bày cách điều chỉnh góc quay của servo motor bằng cách xoay potentiometer. Bạn sẽ học:

Linh kiện cần thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×động cơ servo
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Thay thế) Bộ Kit Potentiometer
1×(Thay thế) Module Potentiometer có núm xoay
1×breadboard (bo mạch thí nghiệm)
1×Dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Servo Motor và Potentiometer

Trước khi tiếp tục, nếu bạn chưa quen thuộc với servo motor và potentiometer (sơ đồ chân, cách hoạt động và lập trình), vui lòng tham khảo các hướng dẫn dưới đây:

Sơ đồ đấu nối

  • Sơ đồ đấu nối giữa Arduino UNO R4, servo motor và potentiometer truyền thống
Arduino UNO R4 động cơ servo chiết áp sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

  • Sơ đồ đấu nối giữa Arduino UNO R4, servo motor và potentiometer module
Arduino UNO R4 động cơ servo chiết áp module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Cách lập trình

  • Đọc giá trị của potentiometer (giá trị trong khoảng từ 0 đến 1023)
int analogValue = analogRead(A0);
  • Chuyển đổi thành góc (giá trị từ 0 đến 180)
int angle = map(analogValue, 0, 1023, 0, 180);
  • Thay đổi vị trí của servo dựa trên góc.
myServo.write(angle);

Code Arduino UNO R4

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-potentiometer-servo-motor */ #include <Servo.h> Servo myServo; // create servo object to control a servo void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); myServo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { // reads the value of the potentiometer (value between 0 and 1023) int analogValue = analogRead(A0); // scales it to use it with the servo (value between 0 and 180) int angle = map(analogValue, 0, 1023, 0, 180); // sets the servo position according to the scaled value myServo.write(angle); // print out the value Serial.print("Potentiometer's Value: "); Serial.print(analogValue); Serial.print(" => Servo Motor's Angle: "); Serial.println(angle); delay(100); }

Các bước thực hiện

Thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Đấu nối các linh kiện theo sơ đồ được cung cấp.
  • Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Sao chép và dán code trên vào Arduino IDE
  • Nhấp vào nút Upload trong Arduino IDE để chuyển code lên Arduino UNO R4
tải mã lên Arduino IDE
  • Mở Serial Monitor
  • Xoay potentiometer
  • Quan sát servo motor quay
  • Kiểm tra kết quả trên Serial Monitor
COM6
Send
Potentiometer's Value: 0 => Servo Motor's Angle: 0 Potentiometer's Value: 85 => Servo Motor's Angle: 14 Potentiometer's Value: 201 => Servo Motor's Angle: 35 Potentiometer's Value: 286 => Servo Motor's Angle: 50 Potentiometer's Value: 370 => Servo Motor's Angle: 65 Potentiometer's Value: 444 => Servo Motor's Angle: 78 Potentiometer's Value: 521 => Servo Motor's Angle: 91 Potentiometer's Value: 608 => Servo Motor's Angle: 106 Potentiometer's Value: 690 => Servo Motor's Angle: 121 Potentiometer's Value: 793 => Servo Motor's Angle: 139 Potentiometer's Value: 907 => Servo Motor's Angle: 159 Potentiometer's Value: 1023 => Servo Motor's Angle: 180 Potentiometer's Value: 1023 => Servo Motor's Angle: 180
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Giải thích code

Kiểm tra các giải thích trong phần comment của source code ⇒ đã được viết từng dòng!

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Bình luận