The big idea: A buzzer turns electricity into sound. In this lesson you will teach your Raspberry Pi Pico to switch a tiny speaker on and off thousands of times per second. How fast it switches decides the pitch (how high or low the note sounds), and by changing that speed in a pattern you can play real melodies, alarms and sirens.
What you will be able to do
- Tell an active buzzer apart from a passive buzzer, and know which one plays tunes.
- Wire a passive buzzer to a Pico GPIO pin safely.
- Use
PWM,freq()andduty_u16()to make a single note. - Write a reusable
play_tone(freq, duration)helper. - Store a melody as a list of
(note, duration)pairs and play a real song. - Build alarm and siren effects with loops.
- Understand why buzzers are quiet, and how a transistor makes them louder.
- Fix the two most common problems: total silence and wrong pitch.
1. Active vs passive buzzers
There are two kinds of small buzzer, and telling them apart is the single most important thing in this lesson. If you pick the wrong one, no code in the world will play a melody.
Active buzzer
An active buzzer has its own tiny electronic circuit built inside. Give it power and it makes one fixed beep on its own. That is all it can ever do.
- You give it: just ON or OFF (like an LED).
- It plays: one buzz at one fixed pitch.
- It cannot play different notes, so it cannot play a melody.
- Often taller, sealed on top, sometimes has a sticker.