.. 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 [|link_sf_facebook|] and join today! .. _1.2.1_py_pi5: 1.2.1 Active Buzzer =================== Introduction ------------ In this project, we will learn how to drive an active buzzer to beep with a NPN transistor. Required Components ------------------------------ In this project, we need the following components. .. image:: ../python_pi5/img/1.2.1_active_buzzer_list.png It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_resistor` - |link_resistor_buy| * - :ref:`cpn_buzzer` - \- * - :ref:`cpn_transistor` - |link_transistor_buy| Schematic Diagram ----------------- In this experiment, an active buzzer, an NPN transistor, and a 1kΩ resistor are used. The resistor is connected between the GPIO pin and the transistor’s base to limit the base current and protect the transistor. When GPIO17 on the Raspberry Pi outputs a high level (3.3V), the transistor enters saturation mode, allowing current to flow through the buzzer, which then sounds. When the GPIO17 outputs a low level (0V), the transistor is turned off, and the buzzer remains silent. ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO17 Pin 11 0 17 ============ ======== ======== === .. image:: ../python_pi5/img/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.) .. image:: ../python_pi5/img/1.2.1_ActiveBuzzer_circuit.png **Step 2**: Open the code file. .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **Step 3**: Run. .. raw:: html .. code-block:: sudo python3 1.2.1_ActiveBuzzer_zero.py The code run, the buzzer beeps. .. warning:: If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` **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. .. raw:: html .. code-block:: python #!/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** #. These statements import the ``Buzzer`` class from the ``gpiozero`` library and the ``sleep`` function from the ``time`` module. .. code-block:: python #!/usr/bin/env python3 from gpiozero import Buzzer from time import sleep #. This line creates a ``Buzzer`` object connected to GPIO pin 17 on the Raspberry Pi. .. code-block:: python # Initialize a Buzzer object on GPIO pin 17 buzzer = Buzzer(17) #. 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. .. code-block:: python 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 #. This segment ensures the program can be terminated safely using a keyboard interrupt (Ctrl+C) without throwing an error. .. code-block:: python except KeyboardInterrupt: # Handle KeyboardInterrupt (Ctrl+C) for clean script termination pass