ESP32 Stepper Motor và Limit Switch

Trong hướng dẫn ESP32 này, chúng ta sẽ khám phá cách sử dụng ESP32, một limit switch, driver stepper DRV8825 và một 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 tốt và linh hoạt với ESP32.

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

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow Amazon
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide Amazon
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide Amazon
1×Alternatively, ESP32 Uno-form board Amazon
1×Alternatively, ESP32 S3 Uno-form board 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×Limit Switch (KW12-3) Amazon
1×Limit Switch Module Amazon
1×Limit Switch (V-153-1C25) Amazon
1×Limit Switch (V-155-1C25) Amazon
1×Limit Switch (V-156-1C25) Amazon
1×Stepper Motor Nema 17 Amazon
1×DRV8825 Stepper Motor Driver Amazon
1×Expansion Board for DRV8825 Amazon
1×12V Power Adapter Amazon
1×DC Power Jack Amazon
1×dây jumper Amazon
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32 Amazon
1×(Khuyến nghị) Breakout Expansion Board for ESP32 Amazon
1×(Khuyến nghị) Power Splitter for ESP32 Amazon

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included) Amazon
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 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.

Về Stepper Motor và Limit Switch

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

Dự án này dùng một module DRV8825, gắn trên board mở rộng của nó, làm driver cho stepper motor. ESP32 chỉ cần hai chân cho nó: STEP (một xung = một bước) và DIR (hướng quay). Driver giữ dòng điện cuộn dây ở mức an toàn, nên động cơ NEMA 17 luôn mát. Cả ba công tắc DIP trên board mở rộng đều ở vị trí OFF, nên động cơ chạy ở chế độ full step: 200 bước cho một vòng.

sơ đồ chân module drv8825

Sơ Đồ Đấu Nối

Hướng dẫn 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 drv8825 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 drv8825 và hai limit switch

This image is created using Fritzing. Click to enlarge image

Linh kiện Chân Kết nối với
Board mở rộng DRV8825 STEP (chân S) GPIO18 của ESP32
Board mở rộng DRV8825 DIR (chân S) GPIO19 của ESP32
Board mở rộng DRV8825 chân G GND của ESP32
Board mở rộng DRV8825 cầu đấu vít VIN, GND + và - của adapter 12V (qua jack nguồn DC)
Board mở rộng DRV8825 1A, 1B một cuộn dây của stepper motor
Board mở rộng DRV8825 2A, 2B cuộn dây còn lại của stepper motor
Limit Switch 1 hai dây GPIO25 và GND của ESP32
Limit Switch 2 hai dây GPIO26 và GND của ESP32

Chân EN và các chân V của board mở rộng để trống. Limit switch 2 chỉ dùng cho ví dụ cuối cùng.

※ Lưu ý:

Tùy thuộc vào loại stepper motor, màu dây của hai cuộn dây có thể khác nhau. Vui lòng xem kỹ hướng dẫn ESP32 - Stepper Motor để biết cách tìm các cuộn dây và kết nối chúng với 1A/1B và 2A/2B của board mở rộng.

Đấu dây không dùng bo mở rộng

Bạn cũng có thể bỏ qua bo mở rộng và nối module DRV8825 trần. ESP32 có thể tự điều khiển module, nối từng chân như bên dưới.

sơ đồ đấu dây ESP32 với module drv8825 không có bo mở rộng và động cơ bước

This image is created using Fritzing. Click to enlarge image

Đây là cách lắp với hai công tắc hành trình:

sơ đồ đấu dây ESP32 với module drv8825 không có bo mở rộng và động cơ bước và hai công tắc hành trình

This image is created using Fritzing. Click to enlarge image

Chân module DRV8825 Nối với
STEP ESP32 GPIO18
DIR ESP32 GPIO19
GND (cạnh FAULT) ESP32 GND
VMOT cực + của adapter 12V
GND (cạnh VMOT) cực - của adapter 12V
2B và 1B một cuộn dây của động cơ bước
1A và 2A cuộn dây còn lại của động cơ bước
EN, M0, M1, M2, RST, SLP, FAULT không nối

※ Lưu ý:

  • Một tụ 100uF giữa VMOT và GND giúp nguồn 12V ổn định. Hãy dùng tụ có điện áp định mức ít nhất 25V và giữ chân tụ ngắn.
  • Nếu không có gì xảy ra khi mã chạy, hãy nối RST và SLP với nhau.

Code ESP32 - Dừng Stepper Motor bằng một 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à dừng 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 liên tục 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 */ // ESP32 + DRV8825 stepper driver (on its expansion board) + limit switch // Full step (all DIP switches OFF): 200 steps per revolution #include <ezButton.h> #include <AccelStepper.h> #define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type) ezButton limitSwitch(25); // ESP32 pin GPIO25 connected to the limit switch #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 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
  • Cắm adapter 12V để cấp nguồn cho stepper motor.
  • Nếu đấu nối đúng, bạn sẽ thấy động cơ quay theo chiều kim đồng hồ.
  • Chạm vào limit switch
  • Bạn sẽ thấy động cơ dừng ngay lập tức
  • Kết quả trên Serial Monitor như sau
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED The stepper motor is STOPPED
Ln 11, Col 1
ESP32 Dev Module on COM15
2

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 một Limit Switch

Code dưới đây làm cho stepper motor quay liên tục 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 */ // ESP32 + DRV8825 stepper driver (on its expansion board) + limit switch // Full step (all DIP switches OFF): 200 steps per revolution #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 connected to the limit switch #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 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 động cơ quay theo chiều kim đồng hồ.
  • Chạm vào limit switch
  • Bạn sẽ thấy hướng của stepper motor đổi sang ngược chiều 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 đổi về chiều kim đồng hồ
  • Kết quả trên Serial Monitor như sau
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
ESP32 Dev Module on COM15
2

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

Code dưới đây làm cho stepper motor quay liên tục 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 */ // ESP32 + DRV8825 stepper driver (on its expansion board) + two limit switches // Full step (all DIP switches OFF): 200 steps per revolution #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 connected to the limit switch 1 ezButton limitSwitch_2(26); // ESP32 pin GPIO26 connected to the limit switch 2 #define STEP_PIN 18 // ESP32 pin GPIO18 connected to the STEP pin of the DRV8825 #define DIR_PIN 19 // ESP32 pin GPIO19 connected to the DIR pin of the DRV8825 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); // DRIVER mode for the DRV8825 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 động cơ 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 đổi sang ngược chiều kim đồng hồ
  • Chạm vào limit switch 2
  • Bạn sẽ thấy hướng của stepper motor đổi về chiều kim đồng hồ
  • Kết quả trên Serial Monitor như sau
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The limit switch 1: TOUCHED The direction -> ANTI-CLOCKWISE The limit switch 2: TOUCHED The direction -> CLOCKWISE
Ln 11, Col 1
ESP32 Dev Module on COM15
2

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.