Arduino UNO R4 Nút nhấn Servo Motor

Trong hướng dẫn này, chúng ta sẽ học cách điều khiển servo motor bằng nút nhấn sử dụng Arduino Uno R4. Nhấn nút sẽ xoay servo motor tới 90 độ, và nhấn lần nữa sẽ đưa nó quay lại 0 độ. Quá trình này lặp lại mỗi khi bạn nhấn nút.

Hướng dẫn được chia thành hai phần:

Dự án này rất tuyệt vời cho người mới bắt đầu để hiểu điều khiển servo motor, đầu vào nút nhấn, và kỹ thuật debouncing với Arduino Uno R4.

Phần Cứng 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×Nút nhấn gắn breadboard kèm nắp
1×Bộ nút nhấn gắn breadboard
1×Nút nhấn gắn bảng điều khiển
1×mô-đun nút nhấn
1×động cơ servo
1×breadboard
1×Dây nối 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à Nút nhấn

Nếu bạn chưa quen thuộc với servo motor và nút nhấn, bao gồm cách đấu chân, cách hoạt động, hoặc cách lập trình chúng, vui lòng tham khảo các hướng dẫn sau để biết thêm thông tin.

Sơ Đồ Đấu Nối

Arduino UNO R4 nút nhấn động cơ servo sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Arduino UNO R4 - Nút nhấn điều khiển Servo Motor không có Debouncing

/* * 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-button-servo-motor */ #include <Servo.h> #define BUTTON_PIN 7 // The Arduino Uno R4 pin connected to button's pin #define SERVO_PIN 9 // The Arduino Uno R4 pin connected to servo motor's pin Servo servo; // create servo object to control a servo int angle = 0; // the current angle of servo motor int lastButtonState; // the previous state of button int currentButtonState; // the current state of button void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); currentButtonState = digitalRead(BUTTON_PIN); } void loop() { lastButtonState = currentButtonState; // save the last state currentButtonState = digitalRead(BUTTON_PIN); // read new state if(lastButtonState == HIGH && currentButtonState == LOW) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Các Bước Thực Hiện

Làm theo các hướng dẫn sau từng bước:

  • Nếu đây là lần đầu 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 bo mạch Arduino Uno R4 với máy tính bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch 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ấn nút Upload trong Arduino IDE để chuyển code lên Arduino UNO R4.
  • Nhấn nút nhiều lần.
  • Quan sát chuyển động của servo motor.

※ Lưu ý:

Đôi khi code đã cho không hoạt động đúng cách. Để đảm bảo nó luôn hoạt động chính xác, chúng ta cần sử dụng button debouncing. Button debouncing có thể khó khăn cho người mới bắt đầu. Tuy nhiên, với sự hỗ trợ của thư viện ezButton, nó trở nên dễ dàng hơn nhiều.

Code Arduino UNO R4 - Nút nhấn điều khiển Servo Motor có Debouncing

Tại sao debouncing lại quan trọng? ⇒ xem Arduino UNO R4 - Button - Debounce

/* * 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-button-servo-motor */ #include <Servo.h> #include <ezButton.h> #define BUTTON_PIN 7 // The Arduino Uno R4 pin connected to button's pin #define SERVO_PIN 9 // The Arduino Uno R4 pin connected to servo motor's pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7; Servo servo; // create servo object to control a servo int angle = 0; // the current angle of servo motor void setup() { Serial.begin(9600); // initialize serial button.setDebounceTime(50); // set debounce time to 50 milliseconds servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Các Bước Thực Hiện

  • Cài đặt thư viện ezButton. Xem Cách thực hiện
  • Sao chép code và mở trong Arduino IDE
  • Nhấn nút Upload trong Arduino IDE để chuyển code lên Arduino UNO R4
  • Nhấn nút nhiều lần
  • Quan sát cách servo motor di chuyển

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.