Raspberry Pi Cảm Biến Chuyển Động Còi Piezo

Hướng dẫn này sẽ chỉ bạn cách sử dụng Raspberry Pi và cảm biến chuyển động HC-SR501 để điều khiển còi piezo. Cụ thể:

Điều này có thể được áp dụng trong quy trình tự động hóa kích hoạt các hành động khi phát hiện sự hiện diện của con người.

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Cảm Biến Chuyển Động HC-SR501
1×Còi Piezo Tích Cực 3-24V
1×Module Còi Piezo Tích Cực
1×Module Còi Piezo Thụ Động
1×breadboard (Bo Mạch Thí Nghiệm)
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Còi Piezo và Cảm Biến Chuyển Động

Nếu bạn chưa quen với còi piezo và cảm biến chuyển động (bao gồm sơ đồ chân, cách hoạt động và cách lập trình), các hướng dẫn sau có thể giúp bạn:

Sơ Đồ Kết Nối

sơ đồ kết nối Raspberry Pi cảm biến chuyển động còi piezo

This image is created using Fritzing. Click to enlarge image

Cài Đặt Ban Đầu

Nút Điều Chỉnh Độ Trễ Thời GianVặn hoàn toàn theo chiều ngược kim đồng hồ.
Nút Điều Chỉnh Phạm Vi Phát HiệnVặn hoàn toàn theo chiều kim đồng hồ.
Bộ Chọn Kích Hoạt LặpĐặt jumper như hình ảnh hiển thị.
cài đặt ban đầu cảm biến chuyển động Arduino

Code Raspberry Pi - Âm Thanh Đơn Giản

Trong phần này, chúng ta sẽ sử dụng còi piezo với Raspberry Pi để tạo ra âm thanh đơn giản mỗi khi phát hiện chuyển động.

Các Bước Nhanh

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích khác với Raspberry Pi trên Pi của bạn.
  • Đảm bảo Raspberry Pi của bạn đã kết nối với cùng mạng cục bộ với PC của bạn.
  • Đảm bảo Raspberry Pi của bạn đã kết nối internet nếu bạn cần cài đặt một số thư viện.
  • Mới sử dụng Raspberry Pi? Hãy bắt đầu với hướng dẫn Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH bằng SSH client tích hợp sẵn trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Tạo file script Python motion_sensor_buzzer.py và thêm đoạn code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi 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/raspberry-pi/raspberry-pi-motion-sensor-piezo-buzzer import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin number to which the buzzer is connected BUZZER_PIN = 16 # Define the GPIO pin number to which the motion sensor is connected MOTION_SENSOR_PIN = 14 # Set up the GPIO pins GPIO.setup(MOTION_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up resistor GPIO.setup(BUZZER_PIN, GPIO.OUT) # Output try: while True: motion_state = GPIO.input(MOTION_SENSOR_PIN) if motion_state == GPIO.HIGH: print("The movement is detected") GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn the buzzer on else: print("The movement is stopped") GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn the buzzer off # Add a slight delay to debounce the motion sensor (optional) time.sleep(0.1) # Allow the user to stop the buzzer by pressing Ctrl+C except KeyboardInterrupt: GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn off the buzzer GPIO.cleanup()
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 motion_sensor_buzzer.py
  • Vẫy tay trước cảm biến.
  • Lắng nghe âm thanh phát ra từ còi piezo.

Script chạy trong vòng lặp vô hạn liên tục cho đến khi bạn nhấn Ctrl + C trong terminal.

Giải Thích Code

Hãy xem giải thích từng dòng có trong phần comment của source code!

Raspberry Pi phát giai điệu bài hát

Trong phần này, chúng ta sẽ làm cho Raspberry Pi kích hoạt còi piezo phát bài hát "Jingle Bells" khi phát hiện chuyển động.

Các Bước Nhanh

  • Tạo file script Python motion_sensor_buzzer_Jingle_Bells.py và thêm đoạn code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi 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/raspberry-pi/raspberry-pi-motion-sensor-piezo-buzzer import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin number to which the buzzer is connected BUZZER_PIN = 16 # Define the GPIO pin number to which the motion sensor is connected MOTION_SENSOR_PIN = 14 # Set up the GPIO pins GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(MOTION_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Constants for note names and their corresponding frequencies C4 = 261 D4 = 293 E4 = 329 F4 = 349 G4 = 392 A4 = 440 B4 = 493 # Dictionary to map numeric values to note names note_names = { C4: "C4", D4: "D4", E4: "E4", F4: "F4", G4: "G4", A4: "A4", B4: "B4", } # List of notes in the "Jingle Bells" melody melody = [ E4, E4, E4, E4, E4, E4, E4, G4, C4, D4, E4, F4, F4, F4, F4, F4, E4, E4, E4, E4, E4, D4, D4, E4, D4, G4 ] # List of note durations (in milliseconds) note_durations = [ 200, 200, 400, 200, 200, 400, 200, 200, 200, 200, 200, 200, 200, 400, 200, 200, 200, 200, 200, 200, 200, 200, 200, 400, 200, 200 ] # Pause duration between notes (in milliseconds) pause_duration = 300 def play_tone(pin, frequency, duration): # Calculate the period based on the frequency period = 1.0 / frequency # Calculate the time for half of the period half_period = period / 2.0 # Calculate the number of cycles for the given duration cycles = int(duration / period) for _ in range(cycles): # Set the GPIO pin to HIGH GPIO.output(pin, GPIO.HIGH) # Wait for half of the period time.sleep(half_period) # Set the GPIO pin to LOW GPIO.output(pin, GPIO.LOW) # Wait for the other half of the period time.sleep(half_period) def play_jingle_bells(): for i in range(len(melody)): note_duration = note_durations[i] / 1000.0 note_freq = melody[i] note_name = note_names.get(note_freq, "Pause") print(f"Playing {note_name} (Frequency: {note_freq} Hz) for {note_duration} seconds") play_tone(BUZZER_PIN, note_freq, note_duration) time.sleep(pause_duration / 1000.0) GPIO.output(BUZZER_PIN, GPIO.LOW) try: while True: motion_state = GPIO.input(MOTION_SENSOR_PIN) if motion_state == GPIO.HIGH: play_jingle_bells() # Allow the user to stop the buzzer by pressing Ctrl+C except KeyboardInterrupt: GPIO.output(BUZZER_PIN, GPIO.LOW) GPIO.cleanup()
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 motion_sensor_buzzer_Jingle_Bells.py
  • Di chuyển tay trước cảm biến.
  • Lắng nghe giai điệu mà còi piezo phát ra.

Giải Thích Code

Hãy xem giải thích từng dòng có trong phần comment của source code!

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.