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!
3.1 Make the Buzzer Beep!ļ
In this lesson, we will learn how to make a buzzer beep using the Raspberry Pi Pico 2. A buzzer is a digital output device, just like an LED, and itās very simple to control. Weāll use an active buzzer for this project, which generates sound when it receives a signal.
What is an Active Buzzer?
An active buzzer has an internal oscillator that makes it easier to use. You only need to send a signal to the buzzer to make it beepāno complex frequency control is required. This is different from a passive buzzer, which requires an external signal to generate sound.
What Youāll Need
In this project, we need the following components.
Itās definitely convenient to buy a whole kit, hereās the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Newton Lab Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1(S8050) |
||
6 |
1(1KĪ©) |
||
7 |
Active Buzzer |
1 |
Circuit Diagram
In this circuit, the buzzer is powered through a transistor (S8050 NPN). The transistor amplifies the current, making the buzzer sound louder than if it were connected directly to the Pico.
Hereās what happens:
GP15 outputs a high signal to control the transistor.
When the transistor is activated, it allows current to flow through the buzzer, making it beep.
A 1kĪ© resistor is used to limit the current to protect the transistor.
Wiring Diagram
Make sure you are using the active buzzer. You can tell itās the correct one by looking for the sealed back (as opposed to the exposed PCB, which is a passive buzzer).
Writing the Code
Note
You can open the file
3.1_beep.ino
fromnewton-lab-kit/arduino/3.1_beep
.Or copy this code into Arduino IDE.
Select the Raspberry Pi Pico 2 board and the correct port, then click āUploadā.
const int buzzerPin = 15; // GPIO pin connected to the transistor base
void setup() {
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(buzzerPin, HIGH); // Turn the buzzer on
delay(1000); // Wait for 1 second
digitalWrite(buzzerPin, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
}
After uploading the code: The buzzer should beep for 1 second, then stay silent for 1 second, and repeat this pattern continuously. If you do not hear the buzzer, check the wiring to ensure all connections are correct. Make sure you are using an active buzzer.
Understanding the Code
Defining the Buzzer Pin:
Assigns buzzerPin to GPIO 15, which controls the transistor and thus the buzzer.
const int buzzerPin = 15; // GPIO pin connected to the transistor base
Setting Up the Pin Mode:
Configures buzzerPin as an output.
void setup() { pinMode(buzzerPin, OUTPUT); }
Controlling the Buzzer: The
loop()
function repeats this process indefinitely, making the buzzer beep every second.digitalWrite(buzzerPin, HIGH)
: SetsbuzzerPin
HIGH
, turning on the transistor, which allows current to flow through the buzzer, making it beep.delay(1000)
: Pauses the program for 1000 milliseconds (1 second).digitalWrite(buzzerPin, LOW)
: SetsbuzzerPin
LOW
, turning off the transistor, stopping the current flow, and silencing the buzzer.
void loop() { digitalWrite(buzzerPin, HIGH); // Turn the buzzer on delay(1000); // Wait for 1 second digitalWrite(buzzerPin, LOW); // Turn the buzzer off delay(1000); // Wait for 1 second }
Further Exploration
Varying the Beep Duration:
Modify the
delay()
values to change how long the buzzer stays on and off.Experiment with shorter or longer durations.
Creating Patterns:
Create more complex patterns by adjusting the timing in the
loop()
function.For example, create an SOS signal in Morse code.
Using a Passive Buzzer:
Try using a passive buzzer and the
tone()
function to generate different frequencies.Note that the wiring and code will be different for a passive buzzer.
Conclusion
In this lesson, youāve learned how to make an active buzzer beep using the Raspberry Pi Pico and a transistor. By controlling the transistor with a GPIO pin, you can safely switch the buzzer on and off without overloading the Picoās GPIO pins. This basic concept can be expanded upon to create more complex sounds or to use buzzers in alarms, notifications, and interactive projects.