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ề Piezo Buzzer và Cảm biến chuyển động
Nếu bạn chưa biết về buzzer piezo và cảm biến chuyển động (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:
Xin lưu ý rằng hướng dẫn này sử dụng còi 3-5V, nhưng bạn có thể điều chỉnh nó cho còi 12V. Bạn có thể tìm hiểu về Arduino - Chuông điện
Sơ đồ đấu dây
This image is created using Fritzing. Click to enlarge image
Cài đặt 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 - Âm thanh đơn giản
/* * 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-piezo-buzzer */constint MOTION_SENSOR_PIN = 7; // Arduino pin connected to the OUTPUT pin of motion sensorconstint BUZZER_PIN = 3; // Arduino pin connected to Buzzer's pinint motionStateCurrent = LOW; // current state of motion sensor's pinint motionStatePrevious = LOW; // previous state of motion sensor's pinvoidsetup() {Serial.begin(9600); // initialize serialpinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input modepinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode}voidloop() { motionStatePrevious = motionStateCurrent; // store old state motionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new stateif (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGHSerial.println("Motion detected!");digitalWrite(BUZZER_PIN, HIGH); // turn on }elseif (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOWSerial.println("Motion stopped!");digitalWrite(BUZZER_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 đú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
Di chuyển tay trước cảm biến
Nghe âm thanh của buzzer Piezo
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!
Mã Arduino - Giai điệu
/* * 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-piezo-buzzer */#include"pitches.h"constint MOTION_SENSOR_PIN = 7; // Arduino pin connected to the OUTPUT pin of motion sensorconstint BUZZER_PIN = 8; // Arduino pin connected to Buzzer's pin// notes in the melody:int melody[] = {NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,NOTE_E5,NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,NOTE_D5, NOTE_G5};// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4};voidsetup() {Serial.begin(9600); // initialize serialpinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode}voidloop() {int motionState = digitalRead(MOTION_SENSOR_PIN); // read new stateif (motionState == HIGH) {Serial.println("The motion is detected");buzzer(); }}voidbuzzer() {// iterate over the notes of the melody:intsize = sizeof(noteDurations) / sizeof(int);for (int thisNote = 0; thisNote < size; thisNote++) {// to calculate the note duration, take one second divided by the note type.//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.int noteDuration = 1000 / noteDurations[thisNote];tone(BUZZER_PIN, melody[thisNote], noteDuration);// to distinguish the notes, set a minimum time between them.// the note's duration + 30% seems to work well:int pauseBetweenNotes = noteDuration * 1.30;delay(pauseBetweenNotes);// stop the tone playing:noTone(BUZZER_PIN); }}
Hướng dẫn từng bước
Sao chép mã ở trên và mở bằng Arduino IDE
Tạo tệp pitches.h trên Arduino IDE bằng cách:
Hoặc nhấp vào nút ngay dưới biểu tượng Serial Monitor và chọn Tab Mới, hoặc dùng tổ hợp phím Ctrl+Shift+N.
Đặt tên tệp là pitches.h và nhấn nút OK
Sao chép mã dưới đây và dán nó vào tệp được tạo pitches.h.
Nhấn nút Upload trên Arduino IDE để tải mã lên cho Arduino
Di chuyển tay bạn trước cảm biến
Nghe giai điệu của còi piezo
Giải thích mã nguồn
Đọc giải thích từng dòng trong các dòng chú thích của mã nguồn!
※ Lưu ý:
Đoạn mã ở trên sử dụng hàm delay(). Hàm này chặn các phần mã khác trong quá trình phát giai điệu. Để tránh chặn các phần mã khác, hãy sử dụng thư viện ezBuzzer thay thế. Thư viện này được thiết kế cho buzzer để beep hoặc phát giai điệu mà không làm gián đoạn các phần mã khác.
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.