Raspberry Pi Potentiometer Điều Khiển Piezo Buzzer

Tutorial này hướng dẫn bạn cách sử dụng Raspberry Pi và potentiometer để điều khiển piezo buzzer. Cụ thể:

Phần Cứng Cần Thiết

1×Raspberry Pi 5
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Thay thế) Bộ Kit Potentiometer
1×(Thay thế) Module Potentiometer có Núm Xoay
1×Module ADC ADS1115
1×Piezo Buzzer Active 3-24V
1×Module Piezo Buzzer Active
1×Module Piezo Buzzer Passive
1×breadboard (Bo mạch thí nghiệm)
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Piezo Buzzer và Potentiometer

Nếu bạn chưa quen thuộc với piezo buzzer và potentiometer (bao gồm pinout, cách hoạt động và lập trình), các tutorial sau đây sẽ giúp bạn:

Sơ Đồ Kết Nối

Raspberry Pi chiết áp còi piezo sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi - Âm Thanh Đơn Giản

Các Bước Thực Hiện

  • Đảm bảo bạn đã cài đặt Raspbian hoặc bất kỳ hệ điều hành nào khác tương thích với Raspberry Pi trên Pi của bạn.
  • Đảm bảo Raspberry Pi của bạn được kết nối với cùng mạng cục bộ với PC của bạn.
  • Đảm bảo Raspberry Pi của bạn được kết nối internet nếu bạn cần cài đặt một số thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH sử dụng SSH client tích hợp trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Cài đặt thư viện Adafruit_ADS1x15 bằng cách chạy các lệnh sau trên terminal Raspberry Pi:
sudo pip install Adafruit-ADS1x15
  • Tạo file script Python potentiometer_buzzer.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/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()
  • Lưu file và chạy script Python bằng cách thực thi lệnh sau trong terminal:
python3 potentiometer_buzzer.py
  • Xoay núm potentiometer.
  • Lắng nghe âm thanh phát ra từ piezo buzzer.

Script chạy trong vòng lặp vô hạn liên tục cho đến khi bạn nhấn Ctrl + C trong terminal.

Giải Thích Code

Hãy xem giải thích từng dòng có trong phần comment của source code!

Raspberry Pi phát giai điệu bài hát

Hãy phát giai điệu "Jingle Bells" khi potentiometer được xoay đến một giá trị ngưỡng.

Các Bước Thực Hiện

  • Tạo file script Python potentiometer_buzzer_song.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/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()
  • Lưu file và chạy script Python bằng cách thực thi lệnh sau trong terminal:
python3 potentiometer_buzzer_song.py
  • Xoay potentiometer.
  • Nghe bài hát từ piezo buzzer.

Giải Thích Code

Hãy xem giải thích từng dòng có trong phần comment của source code!

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.