Raspberry Pi Pico Buzzer Simulator: Wiring, Code and a Live Circuit
This is a passive piezo buzzer wired to a Raspberry Pi Pico, playing a four-note arpeggio with tone() live in your browser. The simulated piezo measures the frequency on its legs, so a wrong note is a wrong number.
Two wires: GP16 (physical pin 21) to the buzzer’s plus side and GND to the other. No library is needed.
- 3.3 V logic
- No library: tone()
- Signal on GP16 (physical pin 21)
- Passive piezo
Wiring the Buzzer to the Raspberry Pi Pico
Every connection in the circuit above, with the board’s own pin names.
| Buzzer pin | Raspberry Pi Pico pin | Why |
|---|---|---|
| Pin 1 | GP16 (physical pin 21) | tone() puts a square wave at the note’s frequency on this pin. |
| Pin 2 | GND | Ground. A passive piezo has no polarity. |
A small passive piezo runs straight from a 3.3 V pin; it is quieter than at 5 V. On a Raspberry Pi Pico tone() makes the square wave in hardware (LEDC), so the core is free while a note plays. A magnetic buzzer draws more than a pin should give, so drive one through a transistor.
Pins to leave alone on the Raspberry Pi Pico
The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.
On a real bench
Solder headers on the Pico and press it into the breadboard across the center channel; the Buzzer goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int BUZZER_PIN = 16; // GP16 (physical pin 21)
// C, E, G, C: an arpeggio, a quarter second a note.
const int NOTES[] = {523, 659, 784, 1047};
void setup() {
Serial.begin(115200);
Serial.println("Playing");
}
void loop() {
for (int i = 0; i < 4; i++) {
tone(BUZZER_PIN, NOTES[i], 200); // returns at once; the note plays on
delay(250);
}
noTone(BUZZER_PIN);
Serial.println("Again");
delay(500);
}
tone(pin, frequency, duration) starts a square wave and returns at once; the note stops on its own after duration milliseconds. That is why the loop still needs delay(250): without it all four notes would start within microseconds of each other.
The frequencies are the notes of a C major arpeggio, from 523 Hz (C5) to 1047 Hz (C6). #include "pitches.h" gives names like NOTE_C5 for the same numbers.
Serial.begin(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.
Common mistakes with the Buzzer on the Raspberry Pi Pico
An active buzzer
An active buzzer has its own oscillator and beeps at one pitch whenever it has power, so tone() cannot change its note. For melodies you need a passive one; the two look the same, but the active kind usually has a sticker on top.
No delay between notes
tone() does not wait for the note to finish. Call it twice in a row and only the second note plays. Wait at least as long as the note, as the sketch does.
Counting physical pins instead of GP numbers
The sketch says 15, which means GP15, and that is physical pin 20 on the header. Counting 15 pins down the side lands on a different GPIO, and the part never answers.
Questions and answers
Can I simulate a buzzer with a Raspberry Pi Pico?
Yes. The circuit at the top of this page is a Raspberry Pi Pico with a piezo buzzer, running in your browser on a simulated RP2040. The simulated piezo reports the frequency it is driven at, and the editor’s scope can show the square wave on the pin. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Can I play two notes at once?
Not with tone(): it drives one pin at a time. Two buzzers on two pins with your own timing can, or a DFPlayer module for recorded sound.
How do I make it louder?
Drive the piezo from two pins in opposite phase, or through a transistor from a higher voltage. A magnetic buzzer is louder but needs the transistor.
Where do I find the note frequencies?
#include "pitches.h" in the editor gives NOTE_ names from NOTE_B0 (31 Hz) to NOTE_DS8 (4978 Hz), the same list the Arduino toneMelody example uses.
Build your own Buzzer project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.