Arduino Buzzer Mario Bros Code — Complete Song + Wiring (2025)
Play the Super Mario Bros theme on an Arduino with a passive piezo buzzer. This guide has the complete working code, wiring, and explanations — ready to copy and run.
Time to complete: 5 minutes
Required: Arduino Uno/Nano, passive piezo buzzer, one jumper wire
What You Need
| Component | Quantity | Notes |
|---|---|---|
| Arduino Uno, Nano, or Mega | 1 | Any 5V Arduino works |
| Passive piezo buzzer | 1 | Must be passive (not active) |
| Breadboard | 1 | Optional but helpful |
| Jumper wires | 2 | Male-to-male |
⚠️ Active vs Passive buzzer: An active buzzer has a built-in oscillator and only makes one tone. A passive buzzer requires an external signal to set the frequency — that's what you need for melodies. Passive buzzers are usually labeled with a "+" sign and no circuit board on them. When in doubt, test with
tone(3, 1000, 200)— if you hear a clear 1 kHz beep, it's passive.
Wiring
Simple 2-wire connection:
| Buzzer Pin | Arduino Pin |
|---|---|
| + (positive) | Digital pin 3 |
| – (negative) | GND |
Pin 3 is PWM-capable, which the tone() function requires. You can optionally add a 100Ω resistor in series to protect the buzzer at high volumes.
Complete Code — Super Mario Bros Theme
// Super Mario Bros Theme for Arduino + Passive Buzzer
// Pin: Digital 3
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_D1 37
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_G1 49
#define NOTE_A1 55
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_D2 73
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_G2 98
#define NOTE_A2 110
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_D3 147
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_D6 1175
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_G6 1568
#define NOTE_A6 1760
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_D7 2349
#define NOTE_E7 2637
#define REST 0
#define BUZZER_PIN 3
// Super Mario Bros main theme
int marioMelody[] = {
NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5, REST,
NOTE_G5, REST, REST, REST, NOTE_G4, REST, REST, REST,
NOTE_C5, REST, REST, NOTE_G4, REST, REST, NOTE_E4, REST,
REST, NOTE_A4, REST, NOTE_B4, REST, NOTE_AS4, NOTE_A4, REST,
NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, REST, NOTE_F5, NOTE_G5, REST,
NOTE_E5, REST, NOTE_C5, NOTE_D5, NOTE_B4, REST, REST,
NOTE_C5, REST, REST, NOTE_G4, REST, REST, NOTE_E4, REST,
REST, NOTE_A4, REST, NOTE_B4, REST, NOTE_AS4, NOTE_A4, REST,
NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, REST, NOTE_F5, NOTE_G5, REST,
NOTE_E5, REST, NOTE_C5, NOTE_D5, NOTE_B4, REST, REST
};
// Note durations: 4 = quarter, 8 = eighth, -4 = dotted quarter
int marioDurations[] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
4, 4, 4, 4, 8, 4, 4, 8,
4, 8, 8, 8, 4, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
4, 4, 4, 4, 8, 4, 4, 8,
4, 8, 8, 8, 4, 8, 8
};
void playMelody(int* melody, int* durations, int length) {
for (int i = 0; i < length; i++) {
int noteDuration = 1000 / durations[i];
if (melody[i] == REST) {
delay(noteDuration);
} else {
tone(BUZZER_PIN, melody[i], noteDuration);
delay(noteDuration * 1.3); // pause between notes
noTone(BUZZER_PIN);
}
}
}
void setup() {
// Optional: test the buzzer on startup
tone(BUZZER_PIN, 1000, 100);
delay(200);
}
void loop() {
int melodyLength = sizeof(marioMelody) / sizeof(marioMelody[0]);
playMelody(marioMelody, marioDurations, melodyLength);
delay(3000); // 3-second pause before repeating
}
How the Code Works
The code uses Arduino's built-in tone() function, which generates a square wave at the specified frequency on a PWM pin.
tone(pin, frequency, duration)
pin— the pin connected to the buzzer (pin 3)frequency— the note frequency in Hz (e.g., 523 for C5)duration— how long to play the note in milliseconds
Each note in the NOTE_X definitions is a frequency in Hz based on the equal temperament scale. Middle C (C4) = 262 Hz. Each octave up doubles the frequency.
Duration calculation: 1000 / duration_value converts the duration values (4 = quarter note, 8 = eighth note) into milliseconds based on a tempo reference.
Add More Songs
The code is structured to easily add more melodies. Here's a simple Happy Birthday:
int birthdayMelody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, REST,
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, REST,
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, REST,
NOTE_B4, NOTE_B4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4, REST
};
int birthdayDurations[] = {
4, 8, 4, 4, 4, 2, 4,
4, 8, 4, 4, 4, 2, 4,
4, 8, 4, 4, 4, 4, 2, 4,
4, 8, 4, 4, 4, 2, 4
};
To play it, call playMelody(birthdayMelody, birthdayDurations, sizeof(birthdayMelody) / sizeof(birthdayMelody[0]));.
Simulate on Wokwi
Test the code without hardware: open this project on Wokwi, add a passive buzzer, connect it to pin 3 and GND, paste the code, and click Play.
Troubleshooting
No sound at all:
- Confirm it's a passive buzzer (active buzzers won't work with
tone()) - Check the polarity (+ to pin 3, − to GND)
- Test with a simple tone:
tone(3, 1000, 500); delay(600);insetup()
Buzzing noise but no melody:
- You may have an active buzzer. Replace it with a passive one.
Melody plays too fast or slow:
- Adjust the multiplier in
delay(noteDuration * 1.3). Increase for slower, decrease for faster.
Sound is very quiet:
- Remove the optional resistor if you added one
- Try a different buzzer — quality varies significantly between brands
Code doesn't upload:
- Select Tools → Board → Arduino Uno (or Nano)
- For Nano clones: Tools → Processor → ATmega328P (Old Bootloader)
Frequently Asked Questions (FAQ)
What's the difference between active and passive buzzers for Arduino? An active buzzer has a built-in oscillator — it makes sound whenever 5V is applied. It only makes one fixed tone. A passive buzzer requires an external oscillating signal (from tone()) to produce sound, allowing any frequency and therefore melodies.
Can I use pin 8 instead of pin 3 for the buzzer? tone() works on any digital pin on Arduino Uno/Nano. Change #define BUZZER_PIN 3 to #define BUZZER_PIN 8 and update your wiring.
Does this code work on Arduino Nano or ESP32? Yes. For Arduino Nano, no changes needed. For ESP32, tone() is supported on ESP32 Arduino core v2+. Use any GPIO pin.
Can I play two notes at the same time? The standard tone() function only plays one frequency at a time. For polyphony on Arduino, you need a dedicated audio library or a DAC-based board.
How do I make the melody loop infinitely? The code already loops forever in loop(). Remove the delay(3000) at the end if you don't want the pause between repetitions.
Related Projects
- Wokwi Simulator Guide — Simulate this circuit online
- How to Connect a Relay with Arduino — Next hardware project
- Arduino vs ESP32 — Upgrade path for more complex projects
- Frequency Calculator — Understand note frequencies