Arduino Cảm biến chuyển động Động cơ servo
Chúng ta sẽ học:
- Nếu phát hiện chuyển động, quay động cơ servo đến 90 độ
- Nếu không phát hiện chuyển động, quay động cơ servo về 0 độ
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 | × | động cơ servo | ||
| 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ề động cơ servo và cảm biến chuyển động
Nếu bạn chưa biết về động cơ servo và cảm biến chuyển động (pinout, 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 sau đây:
Sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image
Thiết lập ban đầu
| Time Delay Adjuster | Screw it in anti-clockwise direction fully. |
| Detection Range Adjuster | Screw it in clockwise direction fully. |
| Repeat Trigger Selector | Put jumper as shown on the image. |

Mã Arduino - Cảm biến chuyển động điều khiển động cơ servo
/*
* 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-servo-motor
*/
#include <Servo.h>
// constants won't change
const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 0; // the current angle of servo motor
int lastMotionState; // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor
void setup() {
Serial.begin(9600); // initialize serial
pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}
void loop() {
lastMotionState = currentMotionState; // save the last state
currentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new state
if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
servo.write(90);
}
else
if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
servo.write(0);
}
}
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 bo mạch và cổng đúng
- 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
- Đưa tay trước cảm biến
- Quan sát sự thay đổi của động cơ servo
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.