Raspberry Pi - Knop - Piezo Buzzer

Deze tutorial legt uit hoe u een Raspberry Pi en een knop gebruikt om een piezo buzzer te bedienen. De volgende functies worden uitvoerig besproken:

Over Piezo Buzzer en Knop

Als u niet vertrouwd bent met piezo buzzer en knop (inclusief pinout, werking en programmering), kunnen de volgende tutorials u helpen:

Bedradingsschema

Raspberry Pi Knop Piezo Buzzer bedradingsschema

This image is created using Fritzing. Click to enlarge image

Raspberry Pi code - Eenvoudig geluid

In dit deel leert u hoe u een piezo buzzer kunt gebruiken om een eenvoudig geluid te maken wanneer de knop wordt ingedrukt met behulp van Raspberry Pi.

Snel aan de slag

  • Zorg dat u Raspbian of een ander Raspberry Pi-compatible 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 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 instelt.
  • Verbind uw pc via SSH met de Raspberry Pi met behulp van de ingebouwde SSH-client op Linux en macOS of PuTTY op Windows. Zie hoe u pc verbindt met Raspberry Pi via SSH.
  • Controleer of u de RPi.GPIO library geïnstalleerd heeft. Zo niet, installeer die dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python scriptbestand button_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-button-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 # Define the GPIO pin number to which the button is connected BUTTON_PIN = 16 # Set up the GPIO pins GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up resistor GPIO.setup(BUZZER_PIN, GPIO.OUT) # Output try: while True: button_state = GPIO.input(BUTTON_PIN) if button_state == GPIO.LOW: print("The button is being pressed") GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn the buzzer on else: print("The button is unpressed") GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn the buzzer off # Add a slight delay to debounce the button (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 het volgende commando in de terminal te typen:
python3 button_buzzer.py
  • Houd de knop enkele seconden ingedrukt.
  • 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

Lees de regel-voor-regel uitleg die in de commentaren van de broncode staat!

Raspberry Pi speelt de melodie van het liedje

In dit deel laten we de Raspberry Pi de piezo buzzer aansturen om het liedje "Jingle Bells" te spelen wanneer de knop wordt ingedrukt.

Snel aan de slag

  • Maak een Python scriptbestand button_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-button-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 # Define the GPIO pin number to which the button is connected BUTTON_PIN = 16 # Set up the GPIO pins GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(BUTTON_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: button_state = GPIO.input(BUTTON_PIN) if button_state == GPIO.LOW: 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 met het volgende commando in de terminal:
python3 button_buzzer_Jingle_Bells.py
  • Druk op de knop
  • Luister naar de melodie van de piezo buzzer.

Code uitleg

Lees de regel-voor-regel uitleg die in de commentaren van de broncode staat!

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 helpt u stap voor stap bij de uitvoering.

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