Welcome to Raspbot repository
5.1 Drive buzzer
1. Learning target
In this course, we will learn how to drive buzzer on car.
2. Principle
Buzzers are divided into two types: "active buzzers" and "passive buzzers".
Active means that they possess a multi-vibrator inside. It only needs to provide the working voltage externally, it can emit a fixed frequency sound.
Passive means that there is no internal oscillation source, and an external drive circuit is required to provide a certain frequency of the drive signal.
In this experiment, we will use passive buzzer.
From the hardware interface manual, we can see that buzzer are directly driven by Pin 32 of Raspberry Pi.

3. Coding method
The pin comparison table of Raspberry Pi is shown in the figure below.
In this course, we use BOARD coding method.
we need to control the 32 pins of the Raspberry Pi board in the code.
4. About code
Path: /home/pi/Yahboom_project/Raspbot/2.Hardware Control course/1.Drive buzzer/buzzer test.ipynb
1) Import time and RPI.GPIO library

2) Set GPIO pin number mode to BOARD and buzzer pin to output mode. Create the PWM output, the duty cycle is 50, and the range is 0-100.
3) Switch the duty of PWM between 0-100 and keep the cycle in this state
4) When we click the stop button in the JupyterLab menu bar, after exiting the loop, stop the PWM and clear the GPIO.
5. Running code
Click the button shown in the figure below to run the program on the Jupyter Lab interface
6. Experimental phenomena
We will hear the buzzer whistle.
Path: /home/pi/Yahboom_project/Raspbot/2.Hardware Control course/1.Drive buzzer/buzzer sing.ipynb
1) Import time and RPI.GPIO library
2) According to the frequency list, define the note and beat list of the song by comparing the numbered musical notation of the song.
3) Define the initialization function--setup to set GPIO to BOARD coding mode, and set buzzer pin to PWM output.
Running function loop to play music according to the frequency list.
4) Call the initialization loop function to play music, and exit the loop when the square stop button is clicked, and clear the GPIO.

After running the program, you can hear the buzzer play music