Raspberry Pi - Bewegingssensor - Piezo Buzzer

Deze handleiding legt uit hoe u een Raspberry Pi en een HC-SR501 bewegingssensor gebruikt om een piezo buzzer aan te sturen. In detail:

Dit kan worden toegepast in een automatiseringsproces dat acties activeert bij het detecteren van menselijke aanwezigheid.

Hardware Benodigdheden

1×Raspberry Pi 5
1×HC-SR501 Bewegingssensor
1×3-24V Actieve Piezo Buzzer
1×Actieve Piezo Buzzer Module
1×Passieve Piezo Buzzer Module
1×Breadboard (experimenteerprint)
1×Jumper Draden
1×(Aanbevolen) Schroefklem Block Shield voor Raspberry Pi
1×(Aanbevolen) Raspberry Pi Prototyping Basisplaat & Breadboard Kit
1×(Aanbevolen) HDMI-Touchscreen-Monitor voor Raspberry Pi

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over Piezo Buzzer en Bewegingssensor

Als u niet bekend bent met piezo buzzer en bewegingssensor (inclusief pinout, werking en programmeren), helpen de volgende tutorials u op weg:

Bedradingsschema

Raspberry Pi Bewegingssensor Piezo Buzzer bedrading schema

This image is created using Fritzing. Click to enlarge image

Eerste Instellingen

Time Delay Adjuster Draai deze volledig tegen de klok in.
Detection Range Adjuster Draai deze volledig met de klok mee.
Repeat Trigger Selector Plaats de jumper zoals afgebeeld in de afbeelding.
arduino motion sensor initial setting

Raspberry Pi Code - Eenvoudig Geluid

In dit gedeelte gebruiken we een piezo buzzer met de Raspberry Pi om een eenvoudig geluid te produceren telkens wanneer beweging wordt gedetecteerd.

Snelle Stappen

  • Zorg dat u Raspbian of een ander Raspberry Pi compatibel besturingssysteem op uw Pi heeft geïnstalleerd.
  • Zorg dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw pc.
  • Zorg dat uw Raspberry Pi verbonden is met het internet indien u nog libraries moet installeren.
  • Gebruikt u voor het eerst een Raspberry Pi? Zie hoe u de Raspberry Pi instelt
  • Verbind uw pc met de Raspberry Pi via SSH gebruikmakend van de ingebouwde SSH-client op Linux en macOS of PuTTY op Windows. Zie hoe u uw pc verbindt met Raspberry Pi via SSH.
  • Zorg dat u de RPi.GPIO library geïnstalleerd heeft. Zo niet, installeer deze dan met de volgende opdracht:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python scriptbestand aan genaamd motion_sensor_buzzer.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/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()
  • Sla het bestand op en voer het Python-script uit door de volgende opdracht in de terminal te typen:
python3 motion_sensor_buzzer.py
  • Beweeg uw hand voor de sensor.
  • Luister naar het geluid van de piezo buzzer.

Het script loopt in een oneindige lus door totdat u Ctrl + C indrukt in de terminal.

Code Uitleg

Bekijk de regel-voor-regel uitleg in de comments van de broncode!

Raspberry Pi speelt een melodie af

In dit gedeelte zorgen we dat de Raspberry Pi de piezo buzzer laat spelen van het nummer "Jingle Bells" wanneer beweging gedetecteerd wordt.

Snelle Stappen

  • Maak een Python scriptbestand aan genaamd motion_sensor_buzzer_Jingle_Bells.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/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()
  • Sla het bestand op en voer het Python-script uit door de volgende opdracht in de terminal te typen:
python3 motion_sensor_buzzer_Jingle_Bells.py
  • Beweeg uw hand voor de sensor.
  • Luister naar de melodie die de piezo buzzer afspeelt.

Code Uitleg

Bekijk de regel-voor-regel uitleg in de comments van de broncode!

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!