Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

1.2.1 Active Buzzer¶

Introduction¶

In this project, we will learn how to drive an active buzzer to beep with a PNP transistor.

Required Components¶

In this project, we need the following components.

../_images/1.2.1_active_buzzer_list.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

Buzzer

-

Transistor

BUY

Schematic Diagram¶

In this experiment, an active buzzer, a PNP transistor and a 1k resistor are used between the base of the transistor and GPIO to protect the transistor. When the GPIO17 of Raspberry Pi output is supplied with high level (3.3V) by programming, the transistor will conduct because of current saturation and the buzzer will make sounds. But when low level is supplied to the IO of Raspberry Pi, the transistor will be cut off and the buzzer will not make sounds.

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

../_images/1.2.1_active_buzzer_schematic.png

Experimental Procedures¶

Step 1: Build the circuit. (The active buzzer has a white table sticker on the surface and a black back.)

../_images/1.2.1_ActiveBuzzer_circuit.png

Step 2: Open the code file.

cd ~/raphael-kit/python-pi5

Step 3: Run.

sudo python3 1.2.1_ActiveBuzzer_zero.py

The code run, the buzzer beeps.

Code

Note

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

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

# Initialize a Buzzer object on GPIO pin 17
buzzer = Buzzer(17)

try:
    while True:
        # Turn on the buzzer
        print('Buzzer On')
        buzzer.on()
        sleep(0.1)  # Keep the buzzer on for 0.1 seconds

        # Turn off the buzzer
        print('Buzzer Off')
        buzzer.off()
        sleep(0.1)  # Keep the buzzer off for 0.1 seconds

except KeyboardInterrupt:
    # Handle KeyboardInterrupt (Ctrl+C) for clean script termination
    pass

Code Explanation

  1. These statements import the Buzzer class from the gpiozero library and the sleep function from the time module.

    #!/usr/bin/env python3
    from gpiozero import Buzzer
    from time import sleep
    
  2. This line creates a Buzzer object connected to GPIO pin 17 on the Raspberry Pi.

    # Initialize a Buzzer object on GPIO pin 17
    buzzer = Buzzer(17)
    
  3. In an infinite loop (while True), the buzzer is turned on and off every 0.1 seconds. print statements provide a console output for each action.

    try:
        while True:
            # Turn on the buzzer
            print('Buzzer On')
            buzzer.on()
            sleep(0.1)  # Keep the buzzer on for 0.1 seconds
    
            # Turn off the buzzer
            print('Buzzer Off')
            buzzer.off()
            sleep(0.1)  # Keep the buzzer off for 0.1 seconds
    
  4. This segment ensures the program can be terminated safely using a keyboard interrupt (Ctrl+C) without throwing an error.

    except KeyboardInterrupt:
    # Handle KeyboardInterrupt (Ctrl+C) for clean script termination
    pass