Arduino UNO R4 Potentiometer điều khiển độ sáng LED

Hướng dẫn này sẽ chỉ cho bạn cách lập trình Arduino UNO R4 để điều khiển độ sáng của LED dựa trên giá trị đầu ra của potentiometer.

Arduino UNO R4 chiết áp fade LED

Linh Kiện Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi
1×Arduino UNO R4 Minima (Thay thế)
1×Cáp USB Type-C
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Thay thế) Bộ Kit Potentiometer
1×(Thay thế) Module Potentiometer với núm xoay
1×LED Kit
1×LED (red)
1×LED Module
1×Điện trở 220 ohm
1×breadboard (bo mạch thí nghiệm)
1×Dây nối jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4
1×(Khuyến nghị) Enclosure for Arduino UNO R4
1×(Khuyến nghị) Power Splitter for Arduino UNO R4
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về LED và Potentiometer

Tìm hiểu về LED và potentiometer (sơ đồ chân, chức năng, lập trình) trong các hướng dẫn sau:

Sơ Đồ Kết Nối

Arduino UNO R4 rotary chiết áp LED sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Cách Lập Trình

  • Đọc tín hiệu đầu vào từ chân analog A0 (giá trị từ 0 đến 1023)
int adc_value = analogRead(A0);
  • Chuyển đổi thành mức độ sáng (một số từ 0 đến 255)
int brightness = map(adc_value, 0, 1023, 0, 255);
  • Thiết lập độ sáng cho LED được kết nối với chân 3.
analogWrite(LED_PIN, brightness);

Mã Lệnh Arduino UNO R4

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 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-uno-r4/arduino-uno-r4-potentiometer-fade-led */ #define LED_PIN 3 // The Arduino UNO R4 pin connected LED #define POTENTIOMETER_PIN A0 // The Arduino UNO R4 pin connected potentiometer void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // declare LED pin to be an output: pinMode(LED_PIN, OUTPUT); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int adc_value = analogRead(POTENTIOMETER_PIN); // scales it to brightness (value between 0 and 255) int brightness = map(adc_value, 0, 1023, 0, 255); // sets the brightness LED that connects to pin 3 analogWrite(LED_PIN, brightness); // print out the value Serial.print("Analog: "); Serial.print(adc_value); Serial.print(", Brightness: "); Serial.println(brightness); delay(100); }

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

Thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối các linh kiện theo sơ đồ đã cung cấp.
  • Kết nối bo mạch Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi động Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Sao chép mã lệnh và mở trong Arduino IDE
  • Nhấp vào nút Upload trong Arduino IDE để tải mã lệnh lên Arduino UNO R4
  • Mở Serial Monitor
  • Xoay potentiometer
  • Quan sát LED
  • Kiểm tra kết quả trên Serial Monitor
COM6
Send
Analog: 6, Brightness: 1 Analog: 34, Brightness: 8 Analog: 89, Brightness: 22 Analog: 149, Brightness: 37 Analog: 214, Brightness: 53 Analog: 297, Brightness: 74 Analog: 365, Brightness: 90 Analog: 431, Brightness: 107 Analog: 510, Brightness: 127 Analog: 589, Brightness: 146 Analog: 695, Brightness: 173 Analog: 790, Brightness: 196 Analog: 970, Brightness: 241 Analog: 996, Brightness: 248 Analog: 1018, Brightness: 253 Analog: 1023, Brightness: 255
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.