Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the buzzer
PB8 through 330 ohms, minus to GND.
Wire the button
PA0 to GND.
Upload the sketch
Press the button briefly.
Listen to tune one
A short press plays Ode to Joy.
Hold the button for a second
Tune two plays.
Add your own tune
Write a new table and call play with it.
How do you build a blue Pill melody player?
A Blue Pill plays a short stored tune on a passive buzzer each time a button is pressed, with a second tune on a long press. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.
- Level
- Intermediate
- Time
- About 120 min
- Parts
- 6
Parts list
- Development BoardsBuy
- InterfacesBuy
- Motors & ActuatorsBuyPassive buzzer × 1
- SwitchesBuy
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| Passive buzzer plus | 330 ohm resistor to Blue Pill PB8 | Tone output |
| Passive buzzer minus | Blue Pill GND | Return |
| Button | Blue Pill PA0 and GND | Short press for tune one, long press for tune two |
Code
A tune is just a list of pitches and lengths. Each note here is a frequency in hertz and a time in milliseconds stored in a table, and the player walks down the table sounding each note for most of its time and leaving a short gap so repeated notes can be heard apart. A passive buzzer has no tone circuit of its own, so the board supplies a square wave at the note frequency with the tone function. Doubling the frequency raises the pitch by an octave, and the notes in the table sit between middle C and the A above it. To add your own tune, write the notes as pairs and call the player with the table and its length. The sketch measures how long the button is held, and a press of 0.8 seconds or more plays the second tune. The 330 ohm resistor limits the current from the pin. Keep the volume modest and the buzzer away from your ear.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int BUZZER = PB8;
const int BTN = PA0;
// Note frequencies in hertz
const int N_C4 = 262, N_D4 = 294, N_E4 = 330, N_F4 = 349, N_G4 = 392, N_A4 = 440;
struct Note { int freq; int ms; };
// Tune one: the opening of Ode to Joy
const Note ode[] = {
{N_E4,400},{N_E4,400},{N_F4,400},{N_G4,400},{N_G4,400},{N_F4,400},{N_E4,400},{N_D4,400},
{N_C4,400},{N_C4,400},{N_D4,400},{N_E4,400},{N_E4,600},{N_D4,200},{N_D4,800}
};
// Tune two: the opening of Twinkle Twinkle
const Note twinkle[] = {
{N_C4,400},{N_C4,400},{N_G4,400},{N_G4,400},{N_A4,400},{N_A4,400},{N_G4,800},
{N_F4,400},{N_F4,400},{N_E4,400},{N_E4,400},{N_D4,400},{N_D4,400},{N_C4,800}
};
void play(const Note *tune, int count) {
for (int i = 0; i < count; i++) {
tone(BUZZER, tune[i].freq, tune[i].ms * 9 / 10); // sound for 90 percent of the note
delay(tune[i].ms); // the rest is a short gap
noTone(BUZZER);
}
}
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
}
void loop() {
if (digitalRead(BTN) == HIGH) return;
unsigned long t0 = millis();
while (digitalRead(BTN) == LOW) {} // wait for release
delay(40);
if (millis() - t0 >= 800) play(twinkle, sizeof(twinkle) / sizeof(twinkle[0]));
else play(ode, sizeof(ode) / sizeof(ode[0]));
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Short press | Tune one plays in full |
| Long press | Tune two plays |
| Repeated notes | Heard as separate notes |
| Notes in the table | Rise and fall in pitch correctly |
| No press | Silence |
If it does not work
No sound
Check the buzzer is passive and wired to PB8.
Notes run together
Reduce the sounding fraction in the play function.
Out of tune
Check the frequency numbers in the table.
Long press not recognised
Hold for a full second.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill melody player?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), Passive buzzer, Tactile push button 6 mm, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the blue Pill melody player take to build?
About 120 minutes for a intermediate maker, in 7 steps.
How do I know the blue Pill melody player works?
Short press: Tune one plays in full. Long press: Tune two plays. Repeated notes: Heard as separate notes.
No sound: what should I check?
Check the buzzer is passive and wired to PB8.
Notes run together: what should I check?
Reduce the sounding fraction in the play function.
Out of tune: what should I check?
Check the frequency numbers in the table.
Long press not recognised: what should I check?
Hold for a full second.
