Arduino Bộ mã hóa quay Động cơ servo
Trong bài hướng dẫn này, chúng ta sẽ học cách lập trình Arduino để điều khiển động cơ servo quay theo giá trị đầu ra của bộ mã hóa quay.
Phần cứng cần chuẩn bị
| 1 | × | Arduino Uno R3 | ||
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | ||
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | ||
| 1 | × | động cơ servo | ||
| 1 | × | encoder xoay | ||
| 1 | × | breadboard | ||
| 1 | × | dây jumper | ||
| 1 | × | (Khuyến nghị) Screw Terminal Block Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Breadboard Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Enclosure for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | ||
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | ||
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Về động cơ servo và bộ mã hóa quay
Nếu bạn chưa biết về động cơ servo và encoder quay (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu chúng trong các bài hướng dẫn sau:
Sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image
Mã Arduino
/*
* Mã Arduino này được phát triển bởi newbiely.vn
* Mã Arduino 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/arduino-rotary-encoder-servo-motor
*/
#include <Servo.h>
#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
#define SERVO_PIN 9
#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;
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(0);
}
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) {
counter--;
direction = DIRECTION_CCW;
} else {
// the encoder is rotating in clockwise direction => increase the counter
counter++;
direction = DIRECTION_CW;
}
Serial.print("DIRECTION: ");
if (direction == DIRECTION_CW)
Serial.print("Clockwise");
else
Serial.print("Counter-clockwise");
Serial.print(" | COUNTER: ");
Serial.println(counter);
if (counter < 0)
counter = 0;
else if (counter > 180)
counter = 180;
// sets the servo angle according to the counter
servo.write(counter);
}
// save last CLK state
prev_CLK_state = CLK_state;
}
Hướng dẫn từng bước
- Kết nối Arduino với PC bằng cáp USB
- Mở Arduino IDE, chọn board và cổng đúng
- Sao chép mã ở trên và mở bằng Arduino IDE
- Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên cho Arduino

- Mở Serial Monitor
- Quay bộ mã hóa
- Xem sự quay của động cơ servo
- Xem kết quả trên Serial Monitor
COM6
DIRECTION: Clockwise | COUNTER/ANGLE: 19
DIRECTION: Clockwise | COUNTER/ANGLE: 26
DIRECTION: Clockwise | COUNTER/ANGLE: 34
DIRECTION: Clockwise | COUNTER/ANGLE: 46
DIRECTION: Clockwise | COUNTER/ANGLE: 53
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 46
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 34
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 26
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 16
DIRECTION: Counter-clockwise | COUNTER/ANGLE: 06
Autoscroll
Clear output
9600 baud
Newline
Giải thích mã nguồn
Đọc lời giải thích theo từng dòng trong các dòng chú thích của mã nguồ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.