Frequency Synthesis: "Home Alone" Piezo Theme
Most programmers simply download an MP3 module to play songs. The Auditory Synthesis projects teach you how music is physically constructed from raw math. By understanding pitch matrices and tempo intervals, you force the tiny, mechanical ATmega328P timers to painstakingly recreate John Williams' legendary "Home Alone" soundtrack using raw square waves.

Defining the Pitch Library
A C++ compiler does not understand the word "F-Sharp". You must define the physics of the musical scale using pre-processor directives (#define).
- The Header File (
pitches.h): You create a separate file in your IDE containing hundreds of definitions.#define NOTE_G4 392#define NOTE_A4 440#define NOTE_B4 494 - This massive library gives the Arduino a mathematical map. When you type
NOTE_G4, the compiler secretly injects the integer392(Hertz) directly into thetone()function.
Mapping Melody Arrays and Tempo Logic
A song is two arrays running simultaneously: The note you hit, and exactly how long you hold it.
- The Melody Array:
int melody[] = { NOTE_G4, NOTE_E5, NOTE_D5, NOTE_C5... }; - The Duration Array: (Represented as fractions of a whole note. E.g.,
4= a quarter note,8= an eighth note).int noteDurations[] = { 4, 8, 8, 4... }; - The Execution Loop: The Arduino iterates through the massive array.
for (int thisNote = 0; thisNote < songLength; thisNote++) {
int durationDelay = 1000 / noteDurations[thisNote]; // Calculate true milliseconds based on Tempo
tone(8, melody[thisNote], durationDelay); // Fire the Piezo!
int pauseBetweenNotes = durationDelay * 1.30; // Need silence between notes or they slur together!
delay(pauseBetweenNotes);
noTone(8); // Mathematically shut off the wave
}
Basic Noise Requirements
- Arduino Uno/Nano.
- A Passive Piezo Buzzer (A Passive buzzer is required. An Active buzzer has its own internal tone generator and will just emit an agonizing, flat continuous screech no matter what math you send it!).
- A 100-ohm Resistor in line with the buzzer to prevent burning out the Arduino's digital pin over prolonged use.