7. Chapter Buzzer
In this chapter, we will learn about buzzers and the sounds they make.
7.1. Project Doorbell
We will make this kind of doorbell: when the button is pressed, the buzzer sounds; and when the button is released, the buzzer stops sounding.
7.1.1. Component List
Raspberry Pi Pico x1
|
USB Cable x1
|
|
Breadboard x1
|
||
Active buzzer x1
|
Resistor 10kΩ x2
|
Jumper
|
Push button x1
|
Resistor 1kΩ x1
|
NPN transistor x1 |
7.1.2. Component Knowledge
7.1.2.1. Buzzer
Buzzer is a sounding component, which is widely used in electronic devices such as calculator, electronic warning clock and alarm. Buzzer has two types: active and passive. Active buzzer has oscillator inside, which will sound as long as it is supplied with power. Passive buzzer requires external oscillator signal (generally use PWM with different frequency) to make a sound.
Active buzzer is easy to use. Generally, it can only make a specific frequency of sound. Passive buzzer requires an external circuit to make a sound, but it can be controlled to make a sound with different frequency. The resonant frequency of the passive buzzer is 2 kHz, which means the passive buzzer is loudest when its resonant frequency is 2 kHz.
Next, we will use an active buzzer to make a doorbell and a passive buzzer to make an alarm.
How to identify active and passive buzzer?
Usually, there is a label on the surface of active buzzer covering the vocal hole, but this is not an absolute judgment method.
Active buzzers are more complex than passive buzzers in their manufacture. There are many circuits and crystal oscillator elements inside active buzzers; all of this is usually protected with a waterproof coating (and a housing) exposing only its pins from the underside. On the other hand, passive buzzers do not have protective coatings on their underside. From the pin holes viewing of a passive buzzer, you can see the circuit board, coils, and a permanent magnet (all or any combination of these components depending on the model.
7.1.2.2. Transistor
Because the buzzer requires such large current that GP of Raspberry Pi Pico output capability cannot meet the requirement, a transistor of NPN type is needed here to amplify the current.
Transistor, the full name: semiconductor transistor, is a semiconductor device that controls current. Transistor can be used to amplify weak signal, or works as a switch. It has three electrodes(PINs): base (b), collector (c) and emitter (e). When there is current passing between “be”, “ce” will allow several-fold current (transistor magnification) pass, at this point, transistor works in the amplifying area. When current between “be” exceeds a certain value, “ce” will not allow current to increase any longer, at this point, transistor works in the saturation area. Transistor has two types as shown below: PNP and NPN.
In our kit, the PNP transistor is marked with 8550, and the NPN transistor is marked with 8050.
Based on the transistor’s characteristics, it is often used as a switch in digital circuits. As micro-controller’s capacity to output current is very weak, we will use transistor to amplify current and drive large-current components.
When using NPN transistor to drive buzzer, we often adopt the following method. If GP outputs high level, current will flow through R1, the transistor will be conducted, and the buzzer will sound. If GP outputs low level, no current flows through R1, the transistor will not be conducted, and buzzer will not sound.
When using PNP transistor to drive buzzer, we often adopt the following method. If GP outputs low level, current will flow through R1, the transistor will be conducted, and the buzzer will sound. If GP outputs high level, no current flows through R1, the transistor will not be conducted, and buzzer will not sound.
NPN transistor to drive buzzer |
PNP transistor to drive buzzer |
|
|
7.1.3. Circuit
Schematic diagram |
|
Hardware connection. If you need any support, please contact us via: support@freenove.com |
|
Note
In this circuit, the power supply for buzzer is 5V, and pull-up resistor of the button connected to the power 3.3V. The buzzer can work when connected to power 3.3V, but it will reduce the loudness.
VBUS should be connect to the positive end of USB cable. If it connects to GND, it may burn the computer or Raspberry Pi Pico. Similarly, please be careful when wiring pins 36-40 of Pico to avoid short circuit.
7.1.4. Code
In this project, a buzzer will be controlled by a push button switch. When the button switch is pressed, the buzzer sounds and when the button is released, the buzzer stops. It is analogous to our earlier project that controlled an LED ON and OFF.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “07.1_Doorbell” and double click “07.1_Doorbell.py”.
7.1.4.1. Doorbell
Click “Run current script”, press the push button switch and the buzzer will sound. Release the push button switch and the buzzer will stop. Press Ctrl+C or click “Stop/Restart backend” to exit program.
The following is the program code:
1from machine import Pin
2import time
3
4button=Pin(16,Pin.IN)
5activeBuzzer=Pin(15,Pin.OUT)
6activeBuzzer.value(0)
7
8while True:
9 if not button.value():
10 activeBuzzer.value(1)
11 else:
12 activeBuzzer.value(0)
The code is logically the same as using button to control LED.
7.2. Project Alertor
Next, we will use a passive buzzer to make an alarm.
Component list and the circuit part is similar to last section. In the Doorbell circuit, only the active buzzer needs to be replaced with a passive buzzer .
7.2.1. Component List
Raspberry Pi Pico x1
|
USB Cable x1
|
|
Breadboard x1
|
||
Active buzzer x1
|
Resistor 10kΩ x2
|
Jumper
|
Push button x1
|
Resistor 1kΩ x1
|
NPN transistor x1 |
7.2.2. Circuit
Schematic diagram |
|
Hardware connection. If you need any support, please contact us via: support@freenove.com |
|
7.2.3. Code
In this project, the buzzer alarm is controlled by the button. Press the button, then buzzer sounds. If you release the button, the buzzer will stop sounding. Logically, it is the same as using button to control LED. As to the control method, passive buzzer requires PWM of certain frequency to sound.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “07.2_Alertor”, and double click “07.2_Alertor.py”.
7.2.3.1. Alertor
Click “Run current script”, press the button, and the alarm sounds; when the button is released, the alarm will stop sounding. Press Ctrl+C or click “Stop/Restart backend” to exit program.
If the buzzer sound is too loud or too low for you, you can modify duty cycle or frequency of PWM.
The following is the program code:
1from machine import Pin,PWM
2import math
3import time
4PI = 3.14
5button = Pin(16, Pin.IN)
6passiveBuzzer = PWM(Pin(15))
7passiveBuzzer.freq(1000)
8
9def alert():
10 for x in range(0, 36):
11 sinVal = math.sin(x * 10 * PI / 180)
12 toneVal = 1500+int(sinVal*500)
13 passiveBuzzer.freq(toneVal)
14 time.sleep_ms(10)
15
16try:
17 while True:
18 if not button.value():
19 passiveBuzzer.duty_u16(4092*2)
20 alert()
21 else:
22 passiveBuzzer.duty_u16(0)
23except:
24 passiveBuzzer.deinit()
Import PWM, Pin, math and time modules.
1from machine import Pin,PWM
2import math
3import time
Define the pins of the button and passive buzzer.
1from machine import Pin,PWM
2import math
3import time
Call sin function of math module to create the frequency data of the passive buzzer.
1def alert():
2 for x in range(0, 36):
3 sinVal = math.sin(x * 10 * PI / 180)
4 toneVal = 1500+int(sinVal*500)
5 passiveBuzzer.freq(toneVal)
6 time.sleep_ms(10)
When not using PWM, please turn it OFF in time.
1passiveBuzzer.deinit()













