Raspberry Pi - Piezo Buzzer

Deze handleiding leert u hoe u een Raspberry Pi gebruikt om een piezo buzzer te besturen. In detail leert u:

Hardware Benodigdheden

1×Raspberry Pi 5
1×3-24V Actieve Piëzo Zoemer
1×Actieve Piëzo Zoemer Module
1×Passieve Piëzo Zoemer 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 de Piezo Buzzer

Een Piezo Buzzer wordt gebruikt om geluid, pieptonen, of zelfs een melodie van een lied te produceren.

Op de markt is een 3V-24V actieve buzzer verkrijgbaar die dubbel gebruikt kan worden: zowel als een 3-5V actieve buzzer als een buzzer voor hogere spanning (12V en hoger).

  • Wanneer deze buzzer rechtstreeks wordt aangesloten op een Arduino-pin, produceert hij een standaard geluid, ideaal voor toepassingen zoals geluidsindicatoren, bijvoorbeeld toetsgeluiden.
  • Wanneer hij via een relais op een hoge spanning wordt aangesloten, maakt de buzzer een luid geluid, wat geschikt is voor waarschuwingssignalen.

De Pinout van de Piezo Buzzer

Een Piezo Buzzer heeft doorgaans twee pinnen:

  • De Negatieve (-) pin moet worden aangesloten op GND (0V)
  • De Positieve (+) pin ontvangt het stuursignaal van de Raspberry Pi (direct of via een relais)
Piezo Buzzer pinout

Hoe een Piezo Buzzer Werkt

Zie Hoe Piezo Buzzer werkt

Aansluitschema

  • Het aansluitschema tussen Raspberry Pi en Piezo Buzzer
Raspberry Pi Piezo Buzzer aansluitschema

This image is created using Fritzing. Click to enlarge image

  • Het aansluitschema tussen Raspberry Pi en Piezo Buzzer module
Raspberry Pi Piezo Buzzer module aansluitschema

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

Snel aan de slag

  • Zorg dat u Raspbian of een ander Raspberry Pi compatibel besturingssysteem hebt geïnstalleerd op uw Pi.
  • Zorg dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw pc.
  • Zorg dat uw Raspberry Pi verbonden is met internet als u benodigde libraries wilt installeren.
  • Als dit uw eerste keer met Raspberry Pi is, bekijk dan hoe u de Raspberry Pi instelt
  • 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 verbindt met Raspberry Pi.
  • Controleer of u de RPi.GPIO library geïnstalleerd hebt. Zo niet, installeer deze met onderstaande opdracht:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python scriptbestand 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-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 = 18 # Set up the GPIO pin as an output GPIO.setup(BUZZER_PIN, GPIO.OUT) # Constants for note names and their corresponding frequencies C4 = 261 G3 = 196 A3 = 220 B3 = 247 # Dictionary to map numeric values to note names note_names = { C4: "C4", G3: "G3", A3: "A3", B3: "B3", } # List of notes in the melody melody = [ C4, G3, G3, A3, G3, 0, B3, C4 ] # List of note durations (in milliseconds) note_durations = [ 400, 200, 200, 400, 400, 400, 400, 400 ] # 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) try: while True: # Infinite loop # Iterate over the notes of the melody for i in range(len(melody)): # To calculate the note duration, take the value from the list and divide it by 1,000 (convert to seconds) 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 the tone play_tone(BUZZER_PIN, note_freq, note_duration) # Add a brief pause between notes (optional) time.sleep(pause_duration / 1000.0) # Stop the tone playing (optional) GPIO.output(BUZZER_PIN, GPIO.LOW) # 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 met de volgende opdracht in de terminal:
python3 buzzer.py
  • Luister naar de melodie die door de piezo buzzer wordt afgespeeld.

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

Raspberry Pi Code Aanpassen

Nu gaan we de code aanpassen om het lied "Jingle Bells" af te spelen.

  • Maak een Python scriptbestand 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-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 = 18 # Set up the GPIO pin as an output GPIO.setup(BUZZER_PIN, GPIO.OUT) # 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) try: while True: # Infinite loop # Iterate over the notes of the melody for i in range(len(melody)): # To calculate the note duration, take the value from the list and divide it by 1,000 (convert to seconds) 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 the tone play_tone(BUZZER_PIN, note_freq, note_duration) # Add a brief pause between notes (optional) time.sleep(pause_duration / 1000.0) # Stop the tone playing (optional) GPIO.output(BUZZER_PIN, GPIO.LOW) # 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 met de volgende opdracht in de terminal:
python3 buzzer_Jingle_Bells.py
  • U kunt deze code vergelijken met de vorige om de verschillen te zien.

Met deze aanpassing speelt de code nu de melodie van "Jingle Bells" af via de piezo buzzer aangesloten op de Raspberry Pi. Geniet van de feestelijke klanken!

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 begeleiding om u te helpen het efficiënt en eenvoudig te begrijpen.

Daag uzelf uit

  • Gebruik een Piezo Buzzer om uw favoriete liedje te spelen.
  • Gebruik een Raspberry Pi bewegingssensor om automatisch een alarm te laten afgaan wanneer iemand in de buurt komt van uw waardevolle spullen. Raadpleeg Raspberry Pi - Motion Sensor voor meer informatie.

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