Raspberry Pi - Potentiometer Piezo Buzzer

Deze tutorial begeleidt u bij het gebruik van de Raspberry Pi en een potentiometer om een piezo buzzer aan te sturen. In detail:

Over Piezo Buzzer en Potentiometer

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

Bedradingsschema

Raspberry Pi Potentiometer Piezo Buzzer bedradingsschema

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - Simpel Geluid

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 internet als u bibliotheken moet installeren.
  • Als dit de eerste keer is dat u de Raspberry Pi gebruikt, bekijk dan hoe u de Raspberry Pi instelt.
  • Verbind uw pc met de Raspberry Pi via SSH met de ingebouwde SSH-client op Linux en macOS, of PuTTY op Windows. Zie hoe u uw pc verbindt met Raspberry Pi via SSH.
  • Controleer of de RPi.GPIO bibliotheek geïnstalleerd is. Zo niet, installeer deze dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Installeer de Adafruit_ADS1x15 bibliotheek door het volgende commando uit te voeren in de terminal van uw Raspberry Pi:
sudo pip install Adafruit-ADS1x15
  • Maak een Python-scriptbestand aan met de naam potentiometer_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-potentiometer-piezo-buzzer import time import RPi.GPIO as GPIO import Adafruit_ADS1x15 # Constants ADC_CHANNEL = 0 # Analog channel on ADS1015 GAIN = 1 # Gain (1, 2/3, 1, 2, 4, 8, 16) BUZZER_PIN = 23 # Raspberry Pi GPIO pin connected to the piezo buzzer # Threshold for triggering the buzzer THRESHOLD = 700 # Adjust this value based on your requirement # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(BUZZER_PIN, GPIO.OUT) # Create ADS1x15 instance ads = Adafruit_ADS1x15.ADS1015() try: while True: # Read the raw ADC value from the potentiometer pot_value = ads.read_adc(ADC_CHANNEL, gain=GAIN) # Trigger the buzzer if the analog value is greater than the threshold if pot_value > THRESHOLD: GPIO.output(BUZZER_PIN, GPIO.HIGH) else: GPIO.output(BUZZER_PIN, GPIO.LOW) print(f"Potentiometer Value: {pot_value}") time.sleep(0.1) except KeyboardInterrupt: GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn off the buzzer before cleanup GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 potentiometer_buzzer.py
  • Draai aan de knop van de potentiometer.
  • Luister naar het geluid afkomstig 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 commentaren van de broncode!

Raspberry Pi speelt de melodie van het liedje

Laten we de melodie van "Jingle Bells" spelen wanneer de potentiometer wordt gedraaid tot voorbij een drempelwaarde.

Snelle Stappen

  • Maak een Python-scriptbestand aan met de naam potentiometer_buzzer_song.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-potentiometer-piezo-buzzer import time import RPi.GPIO as GPIO import Adafruit_ADS1x15 # Constants ADC_CHANNEL = 0 # Analog channel on ADS1015 GAIN = 1 # Gain (1, 2/3, 1, 2, 4, 8, 16) BUZZER_PIN = 23 # Raspberry Pi GPIO pin connected to the piezo buzzer # Threshold for triggering the buzzer THRESHOLD = 700 # Adjust this value based on your requirement # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(BUZZER_PIN, GPIO.OUT) # Create ADS1x15 instance ads = Adafruit_ADS1x15.ADS1015() # 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: # Read the raw ADC value from the potentiometer pot_value = ads.read_adc(ADC_CHANNEL, gain=GAIN) # Trigger the buzzer if the analog value is greater than the threshold if pot_value > THRESHOLD: GPIO.output(BUZZER_PIN, GPIO.HIGH) else: GPIO.output(BUZZER_PIN, GPIO.LOW) print(f"Potentiometer Value: {pot_value}") time.sleep(0.1) except KeyboardInterrupt: GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn off the buzzer before cleanup GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 potentiometer_buzzer_song.py
  • Draai aan de potentiometer.
  • Hoor het liedje uit de piezo buzzer komen.

Code-uitleg

Bekijk de regel-voor-regel uitleg in de commentaren 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!