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 Amazon
1×USB 2.0 cable type A/B (for USB-A PC) Amazon
1×USB 2.0 cable type C/B (for USB-C PC) Amazon
1×Chiết áp Amazon
1×Alternatively, 10k Ohm Trimmer Potentiometer Amazon
1×Mô-đun chiết áp có núm xoay Amazon
1×(Hoặc) Potentiometer Kit Amazon
1×(Hoặc) Potentiometer Module with Knob Amazon
1×LED Kit Amazon
1×LED (red) Amazon
1×LED Module Amazon
1×Alternatively, Button and LED Kit Amazon
1×220Ω Resistor Amazon
1×breadboard Amazon
1×dây jumper Amazon
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino Uno Amazon
1×(Khuyến nghị) Sensors/Servo Expansion Shield for Arduino Uno Amazon
1×(Khuyến nghị) Breadboard Shield for Arduino Uno Amazon
1×(Khuyến nghị) Enclosure for Arduino Uno Amazon
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO Amazon

Or you can buy the following kits:

1×DIYables STEM V3 Starter Kit (Arduino included) Amazon
1×DIYables Sensor Kit (18 sensors/displays) Amazon
Tiết lộ: Một số liên kết trong bài viết này là liên kết tiếp thị Amazon và có thể nhận hoa hồng theo chính sách của chương trình.

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

sơ đồ nối LED với chiết áp quay cho Arduino

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
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino Uno on COM15
2

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.