Super Mario Melody Tone using ESP32 and Buzzer

Last Updated on November 11, 2024

This simple project can take you to the 90s era and you may feel nostalgic when you hear the Super Mario melody tone. Hope you have played this game. Lets build a melody tone player using ESP32 (DOIT Dev kit V1) and a buzzer. In this project we are going to use Arduino sketch to play series of melody notes that resembles super Mario theme. You can use any 5V buzzer or 8Ω speaker. If you want louder sound then use simple single transistor audio amplifier.




Main function of this setup is generating specific frequencies to produce different notes of a melody, and declaring the duration of melody. Then bring it through GPIO pin of ESP32. Which will give super Mario tone through a buzzer.

Wiring Diagram

Schematic

Components Required

  • ESP32 Development Board
  • 5V Buzzer
  • LED
  • Resistor 330Ω
  • Breadboard
  • Arduino IDE installed computer

Working Video

Construction & Working

Just connect positive terminal of buzzer to GPIO5 pin and another pin of buzzer to Gnd. Same way connect LED Anode to GPIO2 through 330Ω Resistor and cathode to Gnd, that’s all our hardware wiring done. Lets jump into code, before that its is must to know about how to create melody and duration array.

  1. We are going to make array contains the frequencies of the notes in Hertz (Hz) for each tone in the song. Each frequency value corresponds to a note, and 0 represents a rest. Then name it as melody[].
  2. Next array provides the relative duration of each note in the melody[] array. Shorter values create faster notes, and longer values produce slower notes, allowing us to create a rhythm. We will name it as noteDurations[].

The following code uses PWM signal from the ESP32 to produce tone, and may be not so professional.

Code

/* Code by theoryCIRCUIT */
#include <Arduino.h>

int buzzerPin = 5; // Buzzer connected to GPIO 5
int ledPin = LED_BUILTIN; // Onboard LED

// Combined melodies from both sections
int melody[] = {
  440, 440, 0, 440, 0, 349, 440, 0, 523, 0, 392, 0, // First melody, first line
  349, 392, 0, 294, 392, 440, 415, 392, 349, 440, 523, 587, // First melody, second line
  0, 349, 523, 440, 349, 392, 330, // First melody, finale
  659, 659, 0, 659, 0, 523, 659, 0, 784, 0, 392, 0, // Second melody, first line
  523, 392, 0, 330, 440, 494, 466, 440, 392, 659, 784, 880, // Second melody, second line
  0, 587, 784, 659, 523, 587, 494 // Second melody, finale
};

// Combined note durations for both melodies
int noteDurations[] = {
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4,  // First melody, first line
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4,  // First melody, second line
  4, 8, 8, 8, 8, 8, 4,                // First melody, finale
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, // Second melody, first line
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, // Second melody, second line
  4, 8, 8, 8, 8, 8, 4                // Second melody, finale
};

// Function to play a single tone with LED indication
void playTone(int frequency, int duration) {
  if (frequency == 0) {
    delay(duration); // Rest for the note duration
  } else {
    ledcAttachPin(buzzerPin, 0);   // Attach buzzer pin to channel 0
    ledcWriteTone(0, frequency);   // Set the tone frequency
    digitalWrite(ledPin, HIGH);    // Turn on the LED while playing
    delay(duration);               // Delay for the duration of the note
    ledcWriteTone(0, 0);           // Turn off tone
    digitalWrite(ledPin, LOW);     // Turn off the LED after playing
  }
}

// Function to play the entire melody
void playMelody() {
  int tempo = 900;  // Adjust this to control the speed of the melody
  int melodySize = sizeof(melody) / sizeof(melody[0]);
  for (int i = 0; i < melodySize; i++) {
    int noteDuration = (tempo / noteDurations[i]); // Calculate note duration
    playTone(melody[i], noteDuration);             // Play the note
    delay(noteDuration * 0.3);                     // Short pause between notes
  }
}

void setup() {
  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  playMelody();
  delay(2000); // Wait 2 seconds before repeating the melody
}

On breadboard




Leave a Reply

Your email address will not be published. Required fields are marked *