Arduino Bộ mã hóa quay LED

Trong hướng dẫn này, chúng ta sẽ học cách lập trình Arduino để điều khiển độ sáng của một đèn LED 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×encoder xoay
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
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ề LED và bộ mã hóa quay

Nếu bạn chưa biết về LED và bộ mã hóa quay (sơ đồ 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

sơ đồ kết nối LED cho bộ mã hóa quay Arduino

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-led */ #include <Servo.h> #define CLK_PIN 2 #define DT_PIN 3 #define SW_PIN 4 #define LED_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; int brightness = 125; // middle value 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); pinMode(LED_PIN, OUTPUT); } 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--; brightness -= 10; // you can change this value } else { // the encoder is rotating in clockwise direction => increase the counter direction = DIRECTION_CW; counter++; brightness += 10; // you can change this value } if (brightness < 0) brightness = 0; else if (brightness > 255) brightness = 255; // sets the brightness of LED according to the counter analogWrite(LED_PIN, brightness); Serial.print("COUNTER: "); Serial.print(counter); Serial.print(" | BRIGHTNESS: "); Serial.println(brightness); } // 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ấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Mở Serial Monitor
  • Xoay bộ mã hóa
  • Xem độ sáng của đèn LED
  • Xem kết quả trên Serial Monitor
COM6
Send
COUNTER: 1 | BRIGHTNESS: 135 COUNTER: 2 | BRIGHTNESS: 145 COUNTER: 3 | BRIGHTNESS: 155 COUNTER: 4 | BRIGHTNESS: 165 COUNTER: 5 | BRIGHTNESS: 175 COUNTER: 6 | BRIGHTNESS: 185 COUNTER: 7 | BRIGHTNESS: 195
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Giải thích mã nguồn

Đọc 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.