Make a Buzzer Beep

This section describes how to use VisionFive 2 or VisionFive 2 Lite's GPIO pins to make a buzzer beep through an example program with Python.

Hardware Setup

Connect the buzzer to the 40-pin header of VisionFive 2 or VisionFive 2 Lite as follows.

Figure 1. Connect Buzzer to 40-Pin GPIO Header
Passive Buzzer 40-Pin GPIO Header
Pin Number Pin Name
VCC 1 3.3V Power
GND 6 GND
I/O 18 GPIO51

Run Demo Code

Perform the following steps on Debian of VisionFive 2 or VisionFive 2 Lite.

  1. Locate the directory where the test code, buzzer.py, exists.

    1. Source into the PYthon3 Virtual Environment.
      source ./myvenv/bin/activate
    2. Install the dependency.
      python3 -m pip install pillow
    3. Get the directory where VisionFive.gpio exists.

      python3 -m pip show VisionFive.gpio
      Note: The output depends on how the application is installed.
    4. Enter the VisionFive.gpio directory.

      cd <VisionFive.gpio directory>
    5. Enter the sample-code directory.

      cd ./VisionFive/sample-code/
  2. Under the sample-code directory, execute the following command to run the demo code.

    sudo python  buzzer.py
    Alternatively, you can also run the following command.
    sudo python3  buzzer.py
    Note: For details about the source code, see buzzer.py Source Code.
  3. Enter the values as prompted to configure the pitch and the lasting time of the beeps.

    • Enter Pitch (200 to 20000): The frequency (in Hz) of the buzzer, ranging from 200 to 20000

      Note: If your value is out of the range, a warning message is displayed.
    • Enter Cycle (seconds): The duration (in seconds) the buzzer keeps beeping

  4. (Optional) Exit the Python3 Virtual Environment.

    deactivate