Raspberry Pi - Deursensor - Piezo Buzzer

Deze handleiding legt uit hoe u Raspberry Pi en een deursensor gebruikt om een piezo buzzer te activeren. In detail:

Hardware Benodigd

1×Raspberry Pi 5
1×Deursensor
1×3-24V Actieve Piezo Buzzer
1×Actieve Piezo Buzzer Module
1×Passieve Piezo Buzzer Module
1×Breadboard (experimenteerprint)
1×Jumper wires (verbinddraden)
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 Deursensor

Als u niet bekend bent met de piezo buzzer en deursensor, inclusief hun pinout, functionaliteit en programmering, kunnen de volgende tutorials helpen:

Bedradingsschema

Raspberry Pi Deursensor Piezo Buzzer bedradingsschema

This image is created using Fritzing. Click to enlarge image

Om uw bekabelingsopstelling te vereenvoudigen en te organiseren, raden we het gebruik van een Schroevenklemaansluiting-shield voor Raspberry Pi aan. Deze shield zorgt voor veiligere en beter beheerbare verbindingen, zoals hieronder weergegeven:

Raspberry Pi Schroevenklemaansluiting-Shield

Raspberry Pi Code - Eenvoudig Geluid

In deze sectie leren we hoe u een piezo buzzer gebruikt om een eenvoudig geluid te produceren wanneer de deur open is met behulp van Raspberry Pi.

Snel Starten

  • Zorg dat u Raspbian of een ander Raspberry Pi compatibel besturingssysteem geïnstalleerd hebt op uw Pi.
  • Zorg dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw PC.
  • Zorg dat uw Raspberry Pi met internet verbonden is als u libraries moet installeren.
  • Als dit de eerste keer is dat u Raspberry Pi gebruikt, zie hoe u de Raspberry Pi installeert.
  • Verbind uw PC met de Raspberry Pi via SSH met behulp van de ingebouwde SSH-client op Linux en macOS of PuTTY op Windows. Zie hoe u uw PC via SSH met Raspberry Pi verbindt.
  • Zorg dat u de RPi.GPIO library geïnstalleerd hebt. Als dit niet het geval is, installeer deze dan met de volgende commandos:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python-scriptbestand door_sensor_buzzer.py aan 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-door-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 = 20 # Define the GPIO pin number to which the door sensor is connected DOOR_SENSOR_PIN = 18 # Set up the GPIO pins GPIO.setup(DOOR_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: door_state = GPIO.input(DOOR_SENSOR_PIN) if door_state == GPIO.HIGH: print("The door is open") GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn the buzzer on else: print("The door is closed") GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn the buzzer off # Add a slight delay to debounce the door 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 door_sensor_buzzer.py
  • Breng de magneet vlak bij de reed switch (deursensor) en haal hem weer weg.
  • Luister naar het geluid van de piezo buzzer.

Het script draait in een oneindige lus 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 de melodie van het liedje

In deze sectie zorgen we dat de Raspberry Pi de piezo buzzer het liedje "Jingle Bells" laat spelen wanneer de deur open is.

Snel Starten

  • Maak een Python-scriptbestand door_buzzer_Jingle_Bells.py aan 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-door-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 = 20 # Define the GPIO pin number to which the door sensor is connected DOOR_SENSOR_PIN = 18 # Set up the GPIO pins GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(DOOR_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: door_state = GPIO.input(DOOR_SENSOR_PIN) if door_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 door_buzzer_Jingle_Bells.py
  • Verplaats de magneet van de reed switch van de deursensor vandaan om de deur open te simuleren.
  • Hoor de melodie van de piezo buzzer.

Code Uitleg

Bekijk de regel-voor-regel uitleg in de commentaarsectie 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.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten en laat duidelijk zien hoe u alles aansluit en programmeert.

※ 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!