ESP32 Stepper Motor và Limit Switch

Trong tutorial ESP32 này, chúng ta sẽ khám phá cách sử dụng ESP32, limit switch, driver L298N và stepper motor. Cụ thể, chúng ta sẽ tìm hiểu:

Những chủ đề này sẽ giúp bạn tạo ra các ứng dụng chuyển động được điều khiển và linh hoạt với ESP32.

Phần Cứng Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×Cáp USB Type-C
1×Limit Switch (KW12-3)
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×Stepper Motor Nema 17
1×Module Driver Motor L298N
1×Adapter Nguồn 12V
1×Jack Nguồn DC
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Stepper Motor và Limit Switch

Nếu bạn chưa biết về stepper motor và limit switch (pinout, cách hoạt động, cách lập trình...), hãy tìm hiểu trong các tutorial sau:

Sơ Đồ Đấu Nối

Tutorial này cung cấp code ESP32 cho hai trường hợp: Một stepper motor + một limit switch, Một stepper motor + hai limit switch.

  • Sơ đồ đấu nối giữa stepper motor và một limit switch
sơ đồ đấu nối ESP32 động cơ bước và limit switch

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

  • Sơ đồ đấu nối giữa stepper motor và hai limit switch
sơ đồ đấu nối ESP32 động cơ bước và hai limit switch

This image is created using Fritzing. Click to enlarge image

※ Lưu ý:

Tùy thuộc vào loại stepper motor, kết nối đấu dây giữa stepper motor và L298N có thể khác nhau. Vui lòng xem kỹ tutorial ESP32 - Stepper Motor để biết cách kết nối stepper motor với driver motor L298N.

Code ESP32 - Dừng Stepper Motor bằng Limit Switch

Có nhiều cách để làm stepper motor dừng lại:

  • Gọi hàm stepper.stop(): Cách này KHÔNG dừng stepper motor ngay lập tức mà từ từ
  • KHÔNG gọi hàm stepper.run(): Cách này dừng stepper motor ngay lập tức

Code dưới đây làm cho stepper motor quay vô hạn và dừng ngay lập tức khi limit switch được chạm

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(25); // ESP32 pin: GPIO25 AccelStepper stepper(AccelStepper::FULL4WIRE, 19, 18, 17, 16); // ESP32 pin: GPIO19, GPIO18, GPIO17, GPIO16 bool isStopped = false; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position stepper.moveTo(MAX_POSITION); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); isStopped = true; } if (isStopped == false) { // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(MAX_POSITION); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function } else { // without calling stepper.run() function, motor stops immediately // NOTE: stepper.stop() function does NOT stops motor immediately Serial.println(F("The stepper motor is STOPPED")); } }

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

  • Nếu đây là lần đầu bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện đấu nối như hình trên.
  • Kết nối bo mạch ESP32 với PC qua cáp micro USB
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Kết nối ESP32 với PC qua cáp USB
  • Mở Arduino IDE, chọn đúng bo mạch và cổng
  • Nhấp vào biểu tượng Libraries trên thanh bên trái của Arduino IDE.
  • Tìm kiếm "ezButton", sau đó tìm thư viện button của ArduinoGetStarted.com
  • Nhấp nút Install để cài đặt thư viện ezButton.
thư viện nút nhấn ESP32
  • Tìm kiếm "AccelStepper", sau đó tìm thư viện AccelStepper của Mike McCauley
  • Nhấp nút Install để cài đặt thư viện AccelStepper.
thư viện accelstepper ESP32
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để tải code lên ESP32
  • Nếu đấu nối đúng, bạn sẽ thấy motor quay theo chiều kim đồng hồ.
  • Chạm vào limit switch
  • Bạn sẽ thấy motor dừng ngay lập tức
  • Kết quả trên Serial Monitor như sau
COM6
Send
The limit switch: TOUCHED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Giải Thích Code

Đọc giải thích từng dòng trong các dòng comment của code!

Code ESP32 - Thay Đổi Hướng Stepper Motor bằng Limit Switch

Code dưới đây làm cho stepper motor quay vô hạn và thay đổi hướng khi limit switch được chạm

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(25); // ESP32 pin: GPIO25 AccelStepper stepper(AccelStepper::FULL4WIRE, 19, 18, 17, 16); // ESP32 pin: GPIO19, GPIO18, GPIO17, GPIO16 int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position targetPos = direction * MAX_POSITION; stepper.moveTo(targetPos); } void loop() { limitSwitch.loop(); // MUST call the loop() function first if (limitSwitch.isPressed()) { Serial.println(F("The limit switch: TOUCHED")); direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); } // without this part, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } stepper.run(); // MUST be called in loop() function }

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

  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để tải code lên ESP32
  • Nếu đấu nối đúng, bạn sẽ thấy motor quay theo chiều kim đồng hồ.
  • Chạm vào limit switch
  • Bạn sẽ thấy hướng của stepper motor thay đổi sang chiều ngược kim đồng hồ
  • Chạm vào limit switch một lần nữa
  • Bạn sẽ thấy hướng của stepper motor thay đổi về chiều kim đồng hồ
  • Kết quả trên Serial Monitor như sau
COM6
Send
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code ESP32 - Thay Đổi Hướng Stepper Motor bằng hai Limit Switch

Code dưới đây làm cho stepper motor quay vô hạn và thay đổi hướng khi một trong hai limit switch được chạm

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-stepper-motor-and-limit-switch */ #include <ezButton.h> #include <AccelStepper.h> #define DIRECTION_CCW -1 #define DIRECTION_CW 1 #define STATE_CHANGE_DIR 1 #define STATE_MOVE 2 #define STATE_MOVING 3 #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch_1(25); // ESP32 pin: GPIO25 ezButton limitSwitch_2(26); // ESP32 pin: GPIO26 AccelStepper stepper(AccelStepper::FULL4WIRE, 19, 18, 17, 16); // ESP32 pin: GPIO19, GPIO18, GPIO17, GPIO16 int stepperState = STATE_MOVE; int direction = DIRECTION_CW; long targetPos = 0; void setup() { Serial.begin(9600); limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds stepper.setMaxSpeed(500.0); // set the maximum speed stepper.setAcceleration(50.0); // set acceleration stepper.setSpeed(100); // set initial speed stepper.setCurrentPosition(0); // set position } void loop() { limitSwitch_1.loop(); // MUST call the loop() function first limitSwitch_2.loop(); // MUST call the loop() function first if (limitSwitch_1.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 1: TOUCHED")); } if (limitSwitch_2.isPressed()) { stepperState = STATE_CHANGE_DIR; Serial.println(F("The limit switch 2: TOUCHED")); } switch (stepperState) { case STATE_CHANGE_DIR: direction *= -1; // change direction Serial.print(F("The direction -> ")); if (direction == DIRECTION_CW) Serial.println(F("CLOCKWISE")); else Serial.println(F("ANTI-CLOCKWISE")); stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor break; case STATE_MOVE: targetPos = direction * MAX_POSITION; stepper.setCurrentPosition(0); // set position stepper.moveTo(targetPos); stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity break; case STATE_MOVING: // without this state, the move will stop after reaching maximum position if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position stepper.setCurrentPosition(0); // reset position to 0 stepper.moveTo(targetPos); // move the motor to maximum position again } break; } stepper.run(); // MUST be called in loop() function }

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

  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để tải code lên ESP32
  • Nếu đấu nối đúng, bạn sẽ thấy motor quay theo chiều kim đồng hồ.
  • Chạm vào limit switch 1
  • Bạn sẽ thấy hướng của stepper motor thay đổi sang chiều ngược kim đồng hồ
  • Chạm vào limit switch 2
  • Bạn sẽ thấy hướng của stepper motor thay đổi về chiều kim đồng hồ
  • Kết quả trên Serial Monitor như sau
COM6
Send
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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