Arduino Cảm biến chuyển động Rơ-le

Chúng ta sẽ học cách làm thế nào:

Bằng cách kết nối rơ-le với bóng đèn, dải LED, động cơ hoặc bộ truyền động... Chúng ta có thể sử dụng cảm biến chuyển động để điều khiển bóng đèn, dải LED, động cơ hoặc bộ truyền động...

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×Relay
1×dây jumper
1×(Tùy chọn) Solenoid Lock
1×(Tùy chọn) 12V Power Adapter
1×(Tùy chọn) DC Power Jack
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ề rơ-le và cảm biến chuyển động

Nếu bạn chưa biết về rơ-le và cảm biến chuyển động (bố trí chân, cách hoạt động, cách lập trình ...), hãy tìm hiểu chúng trong các hướng dẫn sau:

Sơ đồ đấu dây

sơ đồ đấu dây rơ-le cảm biến chuyển động Arduino

This image is created using Fritzing. Click to enlarge image

Cài đặt 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.
cài đặt ban đầu của cảm biến chuyển động Arduino

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-motion-sensor-relay */ const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to the OUTPUT pin of motion sensor const int RELAY_PIN = A5; // Arduino pin connected to the IN pin of relay int motionStateCurrent = LOW; // current state of motion sensor's pin int motionStatePrevious = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { motionStatePrevious = motionStateCurrent; // store old state motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new state if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); digitalWrite(RELAY_PIN, LOW); // turn off } }

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

  • Kết nối Arduino với máy tính bằng cáp USB
  • Mở Arduino IDE, chọn board và cổng phù hợp
  • Sao chép mã ở trên và mở bằng Arduino IDE
  • Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên Arduino
Arduino ide: tải mã lên
  • Di chuyển tay bạn trước cảm biến
  • Xem sự thay đổi trạng thái của rơ-le

Giải thích mã nguồn

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