Acoustic Frequency Basics: Interfacing Buzzer
An LED is binary: ON or OFF. A buzzer is entirely analog. The Interfacing Buzzer with Arduino project introduces the foundational concept of generating an audible "Square Wave" by violently oscillating an Arduino digital pin HIGH and LOW thousands of times a second, physically forcing a piezoelectric crystal to bend and snap, creating sound waves in the air!

The Physics of the tone() Function
If you just run digitalWrite(BuzzerPin, HIGH), the buzzer might click once and stay silent.
- To make a sound, you must vibrate the pin.
- The Arduino core team wrote the
<tone()>library, which hooks directly into the ATmega Timer 2 hardware interrupt register. - The Syntax:
tone(pin, frequency, duration);tone(8, 440, 1000);-> Blast a440Hzfrequency (The musical note 'A4') out of Pin 8 for exactly1000 milliseconds!
- The Arduino processor violently alternates Pin 8 between 5V and GND exactly 440 times per second! The piezo crystal physically flexes 440 times, matching the acoustic resonance of a tuned piano string!
Composing Array-Based Melodies
Playing one note is boring. Creating the Super Mario theme song requires parsing massive arrays.
- You must declare a massive array of integers representing the sheet music:
int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 }; // C4, G3, G3, A3, G3, Rest, B3, C4
int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };
- A
forloop cycles through the array. It calculates the physical duration(1000ms / noteDurations[i]). - Without
delay()between the notes, the song will slur together into an indistinguishable, horrible screech. You must insert a mathematical pause (duration * 1.3) between thetone()executions to allow the physical piezo crystal to return to its rest state!
Essential Chiptune Hardware
- Arduino Uno/Nano (Standard form factor).
- Passive Piezoelectric Buzzer (Do NOT use an "Active" buzzer. Active buzzers have their own internal oscillator and will only play one horrible, screeching 2kHz note no matter what frequency you send them from the C++ code!).
- A 100 Ohm Resistor physically wired in series to the buzzer pin to prevent drawing massive current and blowing the Arduino's logic gate during heavy square-wave pulsing.