Arduino LED mờ dần bằng chiết áp
Trong Arduino - Biến trở kích hoạt LED, chúng ta đã học cách biến trở kích hoạt một đèn LED. Trong bài hướng dẫn này, chúng ta sẽ học cách điều chỉnh độ sáng của đèn LED theo giá trị đầu ra của biến trở.
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 | × | Chiết áp | ||
| 1 | × | Alternatively, 10k Ohm Trimmer Potentiometer | ||
| 1 | × | Mô-đun chiết áp có núm xoay | ||
| 1 | × | (Hoặc) Potentiometer Kit | ||
| 1 | × | (Hoặc) Potentiometer Module with Knob | ||
| 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à Biến trở
Nếu bạn chưa biết về đèn LED và điện trở biến thiên (cấu hình 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 bài hướng dẫn dưới đây:
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 từ chân analog A0 (giá trị từ 0 đến 1023)
int analogValue = analogRead(A0);
- điều chỉnh độ sáng của nó (giá trị từ 0 đến 255)
int brightness = map(analogValue, 0, 1023, 0, 255);
- điều chỉnh độ sáng của LED kết nối với chân 3
analogWrite(LED_PIN, brightness);
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-potentiometer-fade-led
*/
int LED_PIN = 3; // the PWM pin the LED is attached to
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// declare LED pin to be an output:
pinMode(LED_PIN, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// reads the input on analog pin A0 (value between 0 and 1023)
int analogValue = analogRead(A0);
// scales it to brightness (value between 0 and 255)
int brightness = map(analogValue, 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(analogValue);
Serial.print(", Brightness: ");
Serial.println(brightness);
delay(100);
}
Hướng dẫn từng bước
- 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 biến trở
- Nhìn thấy đèn LED
- Xem kết quả trên Serial Monitor
COM6
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
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.