Arduino UNO R4 Actuator có Phản Hồi

Hướng dẫn Arduino UNO R4 này sẽ chỉ cho bạn cách sử dụng *linear actuator có phản hồi* trong các dự án DIY của bạn.

Trong bài học trước, chúng ta đã sử dụng Arduino UNO R4 - Actuator. Loại đó chỉ di chuyển ra hoặc vào, nhưng không cho chúng ta biết vị trí của nó.

Bây giờ, chúng ta sẽ tìm hiểu về linear actuator có phản hồi. Nó có thể cho chúng ta biết vị trí của nó trong khi di chuyển, vì vậy chúng ta có thể điều khiển vị trí chính xác của nó.

Trong hướng dẫn đơn giản này, bạn sẽ học:

Arduino UNO R4 điều khiển linear bộ chấp hành có phản hồi 12v với trình điều khiển động cơ bts7960

Dự án Arduino đơn giản này phù hợp cho người mới bắt đầu. Hãy làm theo hướng dẫn dễ hiểu này để học lập trình Arduino và tạo ra một hệ thống chuyển động thông minh!

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 Amazon
1×USB Cable Type-A to Type-C (for USB-A PC) Amazon
1×USB Cable Type-C to Type-C (for USB-C PC) Amazon
1×12V Linear Actuator with Feedback Amazon
1×BTS7960 Motor Driver Module Amazon
1×12V Power Adapter Amazon
1×DC Power Jack Amazon
1×dây jumper Amazon
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Sensors/Servo Expansion Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Enclosure for Arduino UNO R4 Amazon
1×(Khuyến nghị) Power Splitter for Arduino UNO R4 Amazon
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO Amazon

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included) Amazon
1×DIYables STEM V4B IoT Starter Kit (Arduino included) Amazon
1×DIYables Sensor Kit (18 sensors/displays) Amazon
Tiết lộ: Một số liên kết trong bài viết này là liên kết tiếp thị Amazon và có thể nhận hoa hồng theo chính sách của chương trình.
Arduino Uno R4 WiFi Compatible Boards

Về Linear Actuator Có Phản Hồi

Linear actuator có phản hồi là một loại linear actuator có tín hiệu phản hồi để giám sát và điều khiển vị trí của nó. Phản hồi này đến từ một biến trở (potentiometer) cho ra điện áp tương ứng với vị trí của actuator.

Sơ Đồ Chân Linear Actuator Có Phản Hồi

Linear Actuator có phản hồi có năm dây:

  • Dây Dương Actuator: Dây này điều khiển linear actuator sử dụng điện áp cao (12V, 24V, 48V).
  • Dây 5V: Dây này kết nối với biến trở phản hồi. Nối nó với 5V hoặc 3.3V.
  • Dây GND: Dây này kết nối với biến trở phản hồi. Nối nó với đất (GND).
  • Dây Biến Trở: Còn được gọi là dây phản hồi hoặc dây đầu ra, dây này gửi ra giá trị điện áp thay đổi theo vị trí của hành trình (stroke).
sơ đồ chân linear bộ chấp hành có phản hồi

Cách Hoạt Động

Khi chúng ta cấp điện áp cao cho dây dương và dây âm, actuator sẽ kéo dài hoặc rút lại. Cụ thể, nếu chúng ta kết nối:

  • Kết nối 12V (như 12V, 24V, 48V...) và GND lần lượt với dây dương và dây âm: linear actuator kéo dài ở tốc độ tối đa cho đến khi dừng ở cuối hành trình.
  • Kết nối 12V (như 12V, 24V, 48V...) và GND lần lượt với dây âm và dây dương: linear actuator rút lại ở tốc độ tối đa cho đến khi dừng ở cuối hành trình.
  • Nếu nguồn điện bị ngắt khỏi actuator (GND kết nối với cả dây dương và dây âm) trong khi nó đang kéo dài hoặc rút lại, actuator sẽ dừng di chuyển.

※ Lưu ý:

  • Điện áp cần thiết để điều khiển actuator thay đổi theo thông số kỹ thuật của nó. Kiểm tra datasheet hoặc sách hướng dẫn để biết điện áp phù hợp.
  • Actuator có thể giữ vị trí mà không cần nguồn điện, ngay cả khi nó đang giữ một vật nặng.

Điện áp trên dây của biến trở thay đổi theo vị trí actuator di chuyển đến. Bằng cách kiểm tra điện áp này, chúng ta có thể biết được vị trí của hành trình.

Sơ Đồ Đấu Dây

Hai dây lớn của actuator lấy nguồn từ một driver BTS7960. Driver này nhận adapter 12V trên các vít B+ và B-, và đưa nguồn ra trên M+ và M-. Arduino UNO R4 điều khiển nó bằng bốn chân: RPWM và LPWM chọn chiều (và tốc độ), R_EN và L_EN bật driver. BTS7960 rất mạnh, nên dòng khởi động cao của actuator không phải là vấn đề với nó.

sơ đồ chân trình điều khiển động cơ bts7960

Ba dây nhỏ của biến trở phản hồi nối thẳng vào Arduino UNO R4.

sơ đồ đấu dây Arduino UNO R4 linear bộ chấp hành có phản hồi trình điều khiển bts7960

This image is created using Fritzing. Click to enlarge image

Chân Linh Kiện Chân Arduino UNO R4 / Linh Kiện Khác
BTS7960 RPWM D9
BTS7960 LPWM D10
BTS7960 R_EN D12
BTS7960 L_EN D11
BTS7960 VCC 5V
BTS7960 GND GND
BTS7960 R_IS, L_IS không kết nối
BTS7960 B+, B- Cực + và - của adapter nguồn 12V (qua jack nguồn DC)
BTS7960 M+, M- dây dương và dây âm của actuator
Dây 5V của actuator (VCC biến trở) 5V
Dây GND của actuator (GND biến trở) GND
Dây biến trở của actuator (OUT phản hồi) A0

Ở đây có hai dây cần chân 5V: VCC của BTS7960 và biến trở. Một screw shield giúp việc này dễ dàng, vì nó cho chân 5V nhiều hơn một cổng bắt vít. Hãy dùng một Screw Shield V2 hoặc một Proto Screw Terminal Block Shield trên Arduino UNO R4, như trong sơ đồ này:

sơ đồ đấu dây Arduino UNO R4 linear bộ chấp hành có phản hồi trình điều khiển bts7960 với screw shield

This image is created using Fritzing. Click to enlarge image

Cách điều khiển kéo dài/rút lại linear actuator

Xem hướng dẫn Arduino UNO R4 Actuator Arduino UNO R4 - Actuator. Tóm lại: giữ R_EN và L_EN ở mức HIGH, gửi PWM đến RPWM để kéo dài, gửi PWM đến LPWM để rút lại, và đặt cả hai về 0 để dừng.

Cách tìm vị trí của linear actuator

Đây là cách tìm vị trí hành trình trên linear actuator:

Hiệu Chuẩn

  • Đo chiều dài hành trình của actuator (theo milimét) bằng thước hoặc kiểm tra datasheet.
  • Tìm các giá trị đầu ra khi linear actuator kéo dài hết và rút lại hết bằng cách chạy đoạn code sau.
/* * 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-actuator-with-feedback */ // the code for getting the feedback when the actuator fully extended and retracted #define RPWM_PIN 9 // The Arduino UNO R4 pin connected to the RPWM pin of the BTS7960 (extend) #define LPWM_PIN 10 // The Arduino UNO R4 pin connected to the LPWM pin of the BTS7960 (retract) #define R_EN_PIN 12 // The Arduino UNO R4 pin connected to the R_EN pin of the BTS7960 #define L_EN_PIN 11 // The Arduino UNO R4 pin connected to the L_EN pin of the BTS7960 #define POTENTIOMETER_PIN A0 // The Arduino UNO R4 pin connected to the potentiometer of the actuator void setup() { Serial.begin(9600); // initialize digital pins as outputs. pinMode(RPWM_PIN, OUTPUT); pinMode(LPWM_PIN, OUTPUT); pinMode(R_EN_PIN, OUTPUT); pinMode(L_EN_PIN, OUTPUT); // no PWM signal yet, then turn the driver on analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 0); digitalWrite(R_EN_PIN, HIGH); digitalWrite(L_EN_PIN, HIGH); } void loop() { // extend the actuator analogWrite(LPWM_PIN, 0); analogWrite(RPWM_PIN, 255); delay(20000); // wait for actuator fully extends. It will stop extending automatically when reaching the limit // read the analog in value: int POTENTIOMETER_MAX = analogRead(POTENTIOMETER_PIN); Serial.print("POTENTIOMETER_MAX = "); Serial.println(POTENTIOMETER_MAX); // retracts the actuator analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 255); delay(20000); // wait for actuator fully retracts. It will stop retracting automatically when reaching the limit int POTENTIOMETER_MIN = analogRead(POTENTIOMETER_PIN); Serial.print("POTENTIOMETER_MIN = "); Serial.println(POTENTIOMETER_MIN); }
  • Bạn sẽ thấy log trên Serial Monitor như ví dụ bên dưới.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
POTENTIOMETER_MAX = 987 POTENTIOMETER_MIN = 13
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2
  • Ghi lại những giá trị này.
  • Giá trị nhỏ nhất lớn hơn giá trị lớn nhất? Khi đó RPWM_PIN và LPWM_PIN phải đổi chỗ cho nhau trong code.
  • Cập nhật ba giá trị trong đoạn code bên dưới.

Code Arduino UNO R4 tính toán vị trí của actuator

/* * 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-actuator-with-feedback */ #define RPWM_PIN 9 // The Arduino UNO R4 pin connected to the RPWM pin of the BTS7960 (extend) #define LPWM_PIN 10 // The Arduino UNO R4 pin connected to the LPWM pin of the BTS7960 (retract) #define R_EN_PIN 12 // The Arduino UNO R4 pin connected to the R_EN pin of the BTS7960 #define L_EN_PIN 11 // The Arduino UNO R4 pin connected to the L_EN pin of the BTS7960 #define POTENTIOMETER_PIN A0 // The Arduino UNO R4 pin connected to the potentiometer of the actuator #define STROKE_LENGTH 102 // PLEASE UPDATE THIS VALUE (in millimeter) #define POTENTIOMETER_MAX 987 // PLEASE UPDATE THIS VALUE #define POTENTIOMETER_MIN 13 // PLEASE UPDATE THIS VALUE void setup() { Serial.begin(9600); // initialize digital pins as outputs. pinMode(RPWM_PIN, OUTPUT); pinMode(LPWM_PIN, OUTPUT); pinMode(R_EN_PIN, OUTPUT); pinMode(L_EN_PIN, OUTPUT); // no PWM signal yet, then turn the driver on analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 0); digitalWrite(R_EN_PIN, HIGH); digitalWrite(L_EN_PIN, HIGH); } void loop() { // extend the actuator analogWrite(LPWM_PIN, 0); analogWrite(RPWM_PIN, 255); int potentiometer_value = analogRead(POTENTIOMETER_PIN); int stroke_pos = map(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH); Serial.print("The stroke's position = "); Serial.print(stroke_pos); Serial.println(" mm"); }
  • Thay đổi ba giá trị đã hiệu chỉnh trong code
  • Tải code lên Arduino UNO R4
  • Kiểm tra kết quả trên Serial Monitor
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
The stroke's position = 2 mm The stroke's position = 35 mm The stroke's position = 43 mm The stroke's position = 60 mm The stroke's position = 68 mm The stroke's position = 79 mm The stroke's position = 83 mm The stroke's position = 96 mm The stroke's position = 100 mm
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

Cách điều khiển linear actuator đến một vị trí cụ thể

/* * 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-actuator-with-feedback */ #define RPWM_PIN 9 // The Arduino UNO R4 pin connected to the RPWM pin of the BTS7960 (extend) #define LPWM_PIN 10 // The Arduino UNO R4 pin connected to the LPWM pin of the BTS7960 (retract) #define R_EN_PIN 12 // The Arduino UNO R4 pin connected to the R_EN pin of the BTS7960 #define L_EN_PIN 11 // The Arduino UNO R4 pin connected to the L_EN pin of the BTS7960 #define POTENTIOMETER_PIN A0 // The Arduino UNO R4 pin connected to the potentiometer of the actuator #define STROKE_LENGTH 102 // PLEASE UPDATE THIS VALUE (in millimeter) #define POTENTIOMETER_MAX 987 // PLEASE UPDATE THIS VALUE #define POTENTIOMETER_MIN 13 // PLEASE UPDATE THIS VALUE #define TOLERANCE 5 // in millimeter int targetPosition_mm = 50; // in millimeter void setup() { Serial.begin(9600); // initialize digital pins as outputs. pinMode(RPWM_PIN, OUTPUT); pinMode(LPWM_PIN, OUTPUT); pinMode(R_EN_PIN, OUTPUT); pinMode(L_EN_PIN, OUTPUT); // no PWM signal yet, then turn the driver on analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 0); digitalWrite(R_EN_PIN, HIGH); digitalWrite(L_EN_PIN, HIGH); } void loop() { int potentiometer_value = analogRead(POTENTIOMETER_PIN); int stroke_pos = map(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH); Serial.print("The stroke's position = "); Serial.print(stroke_pos); Serial.println(" mm"); if (stroke_pos < (targetPosition_mm - TOLERANCE)) ACTUATOR_extend(); else if (stroke_pos > (targetPosition_mm + TOLERANCE)) ACTUATOR_retract(); else ACTUATOR_stop(); } void ACTUATOR_extend() { analogWrite(LPWM_PIN, 0); // LPWM off first analogWrite(RPWM_PIN, 255); // full PWM on RPWM: extend } void ACTUATOR_retract() { analogWrite(RPWM_PIN, 0); // RPWM off first analogWrite(LPWM_PIN, 255); // full PWM on LPWM: retract } void ACTUATOR_stop() { analogWrite(RPWM_PIN, 0); // both PWM pins at 0: brake analogWrite(LPWM_PIN, 0); }

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.

Tóm Tắt

Trong hướng dẫn Arduino UNO R4 này, bạn đã học cách sử dụng linear actuator có phản hồi cho các dự án DIY. Hướng dẫn dễ hiểu này đã dạy bạn cách actuator hoạt động, cách tìm vị trí theo milimét, và cách di chuyển nó đến đúng vị trí với lập trình Arduino và driver BTS7960. Sau bài học về linear actuator không có phản hồi, dự án Arduino đơn giản này cho bạn thấy cách tạo ra một hệ thống chuyển động thông minh. Bây giờ bạn có thể xây dựng các dự án điều khiển chuyển động của riêng mình với Arduino UNO R4!

Bài hướng dẫn liên quan