Arduino Cảm biến chuyển động Dải đèn LED

Trong hướng dẫn này, chúng ta sẽ khám phá cách triển khai Arduino, cảm biến chuyển động HC-SR501 và một dải đèn LED để tạo ra một hệ thống tự động hóa chiếu sáng liền mạch. Được thiết kế đặc biệt để kích hoạt dải đèn LED khi phát hiện sự hiện diện của con người, hệ thống này linh hoạt và lý tưởng cho nhiều ứng dụng, chẳng hạn như:

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×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×DotStar RGB LED Strip
1×5V Power Adapter
1×DC Power Jack
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ề Dải Đèn LED và Cảm Biến Chuyển Động

Nếu bạn chưa biết về dải đèn LED và cảm biến chuyển động (cách bố trí các chân, cách chúng 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:

Bạn có thể linh hoạt sử dụng NeoPixel, WS2812B hoặc DotStar LED Strips. Để đơn giản trong việc đấu dây, bài hướng dẫn này đặc biệt sử dụng Dải LED DotStar. Việc điều chỉnh mã cho các loại dải LED khác rất dễ dàng; chỉ cần tham khảo các bài hướng dẫn ở trên để được hướng dẫn.

Sơ đồ đấu dây

sơ đồ nối dây Arduino cho cảm biến chuyển động và dải đèn LED

This image is created using Fritzing. Click to enlarge image

Thiết lập ban đầu

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
thiết lập ban đầu cảm biến chuyển động Arduino

Mã Arduino - Cảm biến chuyển động điều khiển dải LED

/* * 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-motion-sensor-led-strip */ #include <Adafruit_DotStar.h> #define NUMPIXELS 144 // Number of LEDs in DotStar strip #define DOTSTAR_DATA_PIN 2 // Arduino pin connected to the data pin of DotStar #define DOTSTAR_CLOCK_PIN 3 // Arduino pin connected to the data pin of DotStar #define MOTION_SENSOR_PIN 12 // Arduino pin connected to the OUTPUT pin of motion sensor int motion_state = LOW; // current state of motion sensor's pin int prev_motion_state = LOW; // previous state of motion sensor's pin Adafruit_DotStar strip(NUMPIXELS, DOTSTAR_DATA_PIN, DOTSTAR_CLOCK_PIN, DOTSTAR_BRG); void setup() { Serial.begin(9600); strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) strip.show(); // Turn OFF all pixels ASAP strip.setBrightness(255); pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode } void loop() { prev_motion_state = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); // turn on the led strip for (int pixel = 0; pixel < NUMPIXELS; pixel++) { // red color int r = 255; // CHANGE COLOR AS YOUR DESIRE int g = 0; // CHANGE COLOR AS YOUR DESIRE int b = 0; // CHANGE COLOR AS YOUR DESIRE strip.setPixelColor(pixel, g, r, b); // set color for each pixel } strip.show(); } else if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); strip.clear(); // turn off all pixel on LED strip strip.show(); } }

Hướng dẫn từng bước

  • Kết nối Arduino với PC qua cáp USB
  • Mở Arduino IDE, chọn bo mạch và cổng đúng
  • Đi tới biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm “Adafruit DotStar”, sau đó tìm thư viện DotStar của Adafruit
  • Nhấp vào nút Cài đặt để cài đặt thư viện DotStar.
thư viện dotstar cho Arduino
  • Bạn sẽ được yêu cầu cài đặt phụ thuộc. Nhấn nút Cài đặt Tất cả.
thư viện dotstar cho Arduino
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp nút Tải lên trên Arduino IDE để tải mã lên Arduino
  • Di chuyển tay trước cảm biến
  • Kiểm tra dải đèn LED

Bạn có thể chỉnh sửa mã để thêm hiệu ứng ánh sáng.

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.