1.2.2 Passive Buzzer

Introduction

In this project, we will learn how to make a passive buzzer play music.

Required Components

In this project, we need the following components.

../_images/1.2.2_passive_buzzer_list.png

Schematic Diagram

In this experiment, a passive buzzer, a PNP transistor and a 1k resistor are used between the base of the transistor and GPIO to protect the transistor.

When GPIO17 is given different frequencies, the passive buzzer will emit different sounds; in this way, the buzzer plays music.

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

../_images/1.2.2_passive_buzzer_schematic.png

Experimental Procedures

Step 1: Build the circuit. (The Passive buzzer with green circuit board on the back.)

../_images/1.2.2_PassiveBuzzer_circuit.png

Step 2: Change directory.

cd ~/davinci-kit-for-raspberry-pi/python-pi5

Step 3: Run.

sudo python3 1.2.2_PassiveBuzzer_zero.py

The code run, the buzzer plays a piece of music.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like davinci-kit-for-raspberry-pi/python-pi5. After modifying the code, you can run it directly to see the effect.

#!/usr/bin/env python3
from gpiozero import TonalBuzzer
from time import sleep

# Initialize a TonalBuzzer connected to GPIO pin 17
tb = TonalBuzzer(17)  # Update this pin number based on your setup

def play(tune):
    """
    Play a musical tune using the buzzer.
    :param tune: List of tuples (note, duration), where each tuple represents a note and its duration.
    """
    for note, duration in tune:
        print(note)  # Output the current note being played
        tb.play(note)  # Play the note on the buzzer
        sleep(float(duration))  # Delay for the duration of the note
    tb.stop()  # Stop playing after the tune is complete

# Define a musical tune as a sequence of notes and durations
tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2),
    ('Eb4', 0.2), ('E4', 0.2), (None, 0.6),
    ('F#4', 0.2), ('G4', 0.2), (None, 0.6),
    ('Eb4', 0.2), ('E4', 0.2), (None, 0.2),
    ('F#4', 0.2), ('G4', 0.2), (None, 0.2),
    ('C4', 0.2), ('B4', 0.2), (None, 0.2),
    ('F#4', 0.2), ('G4', 0.2), (None, 0.2),
    ('B4', 0.2), ('Bb4', 0.5), (None, 0.6),
    ('A4', 0.2), ('G4', 0.2), ('E4', 0.2),
    ('D4', 0.2), ('E4', 0.2)]

try:
    play(tune)  # Execute the play function to start playing the tune

except KeyboardInterrupt:
    # Handle KeyboardInterrupt for graceful termination
    pass

Code Explanation

  1. These lines import the TonalBuzzer class from the gpiozero library for buzzer control and the sleep function from the time module for creating delays.

    #!/usr/bin/env python3
    from gpiozero import TonalBuzzer
    from time import sleep
    
  2. This line initializes a TonalBuzzer object on GPIO pin 17.

    # Initialize a TonalBuzzer connected to GPIO pin 17
    tb = TonalBuzzer(17)  # Update this pin number based on your setup
    
  3. The play function iterates over a list of tuples representing musical notes and their durations. Each note is played for its specified duration, and the buzzer stops after completing the tune.

    def play(tune):
        """
        Play a musical tune using the buzzer.
        :param tune: List of tuples (note, duration), where each tuple represents a note and its duration.
        """
        for note, duration in tune:
            print(note)  # Output the current note being played
            tb.play(note)  # Play the note on the buzzer
            sleep(float(duration))  # Delay for the duration of the note
        tb.stop()  # Stop playing after the tune is complete
    
  4. The tune is defined as a sequence of notes (frequency) and durations (seconds).

    # Define a musical tune as a sequence of notes and durations
    tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2),
        ('Eb4', 0.2), ('E4', 0.2), (None, 0.6),
        ('F#4', 0.2), ('G4', 0.2), (None, 0.6),
        ('Eb4', 0.2), ('E4', 0.2), (None, 0.2),
        ('F#4', 0.2), ('G4', 0.2), (None, 0.2),
        ('C4', 0.2), ('B4', 0.2), (None, 0.2),
        ('F#4', 0.2), ('G4', 0.2), (None, 0.2),
        ('B4', 0.2), ('Bb4', 0.5), (None, 0.6),
        ('A4', 0.2), ('G4', 0.2), ('E4', 0.2),
        ('D4', 0.2), ('E4', 0.2)]
    
  5. The play(tune) function is called inside a try block. A KeyboardInterrupt (like Ctrl+C) will stop the program gracefully.

    try:
        play(tune)  # Execute the play function to start playing the tune
    
    except KeyboardInterrupt:
        # Handle KeyboardInterrupt for graceful termination
        pass