ESP32 Rotary Encoder Servo Motor

Trong hướng dẫn này, chúng ta sẽ học cách lập trình ESP32 và rotary encoder để điều khiển góc quay của servo motor.

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×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×động cơ servo
1×encoder xoay
1×breadboard
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ề Servo Motor và Rotary Encoder

Chưa quen với servo motor và rotary encoder, bao gồm cấu trúc chân, chức năng và cách lập trình? Hãy khám phá các hướng dẫn toàn diện về những chủ đề này dưới đây:

Sơ Đồ Kết Nối

ESP32 encoder xoay động cơ servo sơ đồ đấu dây

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.

Code ESP32

/* * 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-rotary-encoder-servo-motor */ #include <ESP32Servo.h> #define CLK_PIN 25 // ESP32 pin GPIO25 connected to the rotary encoder's CLK pin #define DT_PIN 26 // ESP32 pin GPIO26 connected to the rotary encoder's DT pin #define SW_PIN 27 // ESP32 pin GPIO27 connected to the rotary encoder's SW pin #define SERVO_PIN 33 // ESP32 pin GPIO33 connected to the servo motor #define DIRECTION_CW 0 // clockwise direction #define DIRECTION_CCW 1 // counter-clockwise direction int counter = 0; int direction = DIRECTION_CW; int CLK_state; int prev_CLK_state; int angle = 90; Servo servo; // create servo object to control a servo void setup() { Serial.begin(9600); // configure encoder pins as inputs pinMode(CLK_PIN, INPUT); pinMode(DT_PIN, INPUT); // read the initial state of the rotary encoder's CLK pin prev_CLK_state = digitalRead(CLK_PIN); servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); } void loop() { // read the current state of the rotary encoder's CLK pin CLK_state = digitalRead(CLK_PIN); // If the state of CLK is changed, then pulse occurred // React to only the rising edge (from LOW to HIGH) to avoid double count if (CLK_state != prev_CLK_state && CLK_state == HIGH) { // if the DT state is HIGH // the encoder is rotating in counter-clockwise direction => decrease the counter if (digitalRead(DT_PIN) == HIGH) { direction = DIRECTION_CCW; counter--; angle -= 2; // you can change this resolution } else { // the encoder is rotating in clockwise direction => increase the counter direction = DIRECTION_CW; counter++; angle += 2; // you can change this resolution } if (angle < 0) angle = 0; else if (angle > 180) angle = 180; // sets the servo angle according to the counter servo.write(angle); Serial.print("COUNTER: "); Serial.print(counter); Serial.print(" | ANGLE: "); Serial.println(angle); } // save last CLK state prev_CLK_state = CLK_state; }

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

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện kết nối theo sơ đồ trên.
  • Kết nối board ESP32 với PC của bạn qua cáp micro USB
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng board 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 board và cổng
  • Copy đoạn code trên và mở bằng Arduino IDE
  • Nhấn nút Upload trên Arduino IDE để tải code lên ESP32
tải mã lên Arduino IDE
  • Mở Serial Monitor
  • Xoay rotary encoder
  • Quan sát servo motor quay
  • Xem kết quả trên Serial Monitor
COM6
Send
COUNTER: 0 | ANGLE: 90 COUNTER: 1 | ANGLE: 92 COUNTER: 2 | ANGLE: 94 COUNTER: 3 | ANGLE: 96 COUNTER: 4 | ANGLE: 98 COUNTER: 5 | ANGLE: 100 COUNTER: 6 | ANGLE: 102
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 source code!

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

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