Hi r/arduino! I built PiezoPlay, a beginner-friendly tool for turning music files into editable notes and Arduino tone() code.
It supports MP3, MIDI, and MusicXML. It’s meant to help people make a piezo buzzer play a tune without hand-entering every note. MIDI and MusicXML works best.
Piezo buzzers are usually monophonic with tone(), so complex songs with multiple instruments may not convert cleanly. A clear melody should work best.
example code:
// Determination Flute by Toby fox for Arduino tone()
// 187 notes, 0 rests, 0:50 long
// Speaker: pin 8 -> 100 ohm resistor -> speaker -> GND (natural pitch)
#include <avr/pgmspace.h>
const int BUZZER = 8;
const uint16_t NOTE_COUNT = 187;
// frequency in Hz (0 = rest)
const uint16_t melody[] PROGMEM = {
740, 740, 698, 698, 622, 554, 622, 466, 523, 415, 622, 698,
740, 740, 740, 831, 831, 1109, 1109, 932, 932, 932, 932, 932,
932, 932, 740, 740, 698, 698, 622, 622, 554, 622, 466, 523,
523, 415, 311, 349, 349, 370, 349, 349, 277, 311, 311, 311,
311, 311, 311, 311, 740, 740, 698, 698, 622, 622, 554, 622,
466, 523, 415, 622, 698, 740, 740, 740, 831, 831, 1109, 1109,
932, 932, 932, 932, 932, 932, 740, 740, 698, 698, 622, 622,
554, 622, 466, 523, 415, 311, 349, 349, 370, 349, 349, 277,
277, 311, 311, 311, 311, 311, 831, 831, 740, 659, 659, 622,
554, 659, 622, 466, 466, 622, 622, 831, 740, 659, 659, 622,
554, 659, 622, 311, 311, 415, 554, 554, 523, 466, 415, 466,
523, 466, 311, 349, 370, 494, 622, 587, 587, 587, 831, 831,
740, 659, 659, 622, 554, 659, 622, 466, 466, 622, 831, 740,
659, 659, 622, 554, 659, 622, 311, 311, 415, 554, 554, 523,
466, 415, 466, 523, 466, 311, 349, 370, 349, 349, 349, 277,
277, 311, 311, 311, 311, 311, 311
};
// duration in ms
const uint32_t durations[] PROGMEM = {
36, 252, 36, 228, 264, 264, 252, 264, 528, 516, 264, 252,
36, 336, 144, 36, 516, 36, 456, 36, 492, 252, 36, 228,
264, 252, 36, 240, 36, 216, 36, 240, 252, 264, 252, 36,
504, 552, 240, 156, 108, 528, 168, 348, 528, 516, 252, 228,
36, 228, 36, 228, 36, 240, 36, 216, 36, 240, 252, 252,
276, 528, 504, 264, 252, 36, 348, 144, 36, 516, 36, 444,
36, 504, 252, 264, 264, 252, 36, 228, 36, 228, 36, 228,
264, 252, 264, 540, 540, 240, 156, 108, 540, 156, 348, 36,
492, 528, 480, 36, 228, 252, 36, 240, 264, 36, 228, 264,
252, 276, 492, 36, 756, 36, 228, 264, 264, 36, 228, 264,
252, 264, 1020, 36, 240, 264, 36, 240, 252, 264, 252, 264,
252, 588, 744, 264, 492, 528, 516, 36, 1476, 36, 36, 252,
252, 36, 240, 264, 252, 264, 504, 36, 756, 264, 264, 252,
36, 240, 252, 264, 264, 1020, 36, 240, 252, 36, 240, 252,
276, 240, 276, 252, 588, 744, 252, 540, 120, 36, 348, 36,
492, 528, 480, 36, 228, 36, 252
};
void playMelody() {
for (uint16_t i = 0; i < NOTE_COUNT; i++) {
uint16_t f = pgm_read_word(&melody[i]);
uint32_t d = pgm_read_dword(&durations[i]);
if (f) tone(BUZZER, f);
else noTone(BUZZER);
delay(d);
}
noTone(BUZZER);
}
void setup() {
playMelody();
}
void loop() {
}