Raspberry Pi - Touch Sensor - Piezo Buzzer

Deze tutorial legt uit hoe u met Raspberry Pi en een touch sensor een piezo buzzer aanstuurt. In detail:

Hardware Benodigd

1×Raspberry Pi 5
1×Aanraaksensor
1×3-24V Actieve Piezo Buzzer
1×Actief Piezo Buzzer Module
1×Passief 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 Touch Sensor

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

Bedradingsschema

Raspberry Pi Touch Sensor Piezo Buzzer bedrading schema

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 programmeren we de Raspberry Pi om een piezo buzzer te activeren die een eenvoudig geluid produceert telkens wanneer u uw vinger op de touch sensor legt.

Snelle Stappen

  • 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 verbinding met internet heeft als u libraries moet installeren.
  • Als dit de eerste keer is dat u Raspberry Pi gebruikt, bekijk dan 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 uw PC via SSH verbindt met Raspberry Pi.
  • Controleer of u de RPi.GPIO bibliotheek geïnstalleerd hebt. Zo niet, installeer deze dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python scriptbestand aan met de naam touch_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-touch-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 = 18 # Define the GPIO pin number to which the touch sensor is connected TOUCH_SENSOR_PIN = 16 # Set up the GPIO pins GPIO.setup(TOUCH_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: touch_state = GPIO.input(TOUCH_SENSOR_PIN) if touch_state == GPIO.HIGH: print("The sensor is touched") GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn the buzzer on else: print("The sensor is untouched") GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn the buzzer off # Add a slight delay to debounce the touch 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 met onderstaand commando in de terminal:
python3 touch_sensor_buzzer.py
  • Leg uw vinger op de touch sensor en houd deze enkele seconden vast.
  • Luister naar het geluid dat uit de piezo buzzer komt.

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

Code Uitleg

Bekijk de regel-voor-regel uitleg die in de opmerkingen van de broncode staat!

Raspberry Pi speelt de melodie van het nummer

In deze sectie programmeren we de Raspberry Pi zodat de piezo buzzer het liedje "Jingle Bells" speelt wanneer u uw vinger op de touch sensor legt.

Snelle Stappen

  • Maak een Python scriptbestand aan met de naam touch_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-touch-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 = 18 # Define the GPIO pin number to which the touch sensor is connected TOUCH_SENSOR_PIN = 16 # Set up the GPIO pins GPIO.setup(BUZZER_PIN, GPIO.OUT) GPIO.setup(TOUCH_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: touch_state = GPIO.input(TOUCH_SENSOR_PIN) if touch_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 start het script met het volgende commando in de terminal:
python3 touch_sensor_buzzer_Jingle_Bells.py
  • Leg uw vinger op de touch sensor.
  • Luister naar de melodie van de piezo buzzer.

Code Uitleg

Bekijk de regel-voor-regel uitleg die in de opmerkingen 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 om het goed te begrijpen en toe te passen.

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