The Arduino Melody Player with Speaker project becomes even more engaging thanks to the speaker from Adafruit. For example, the Mini Metal speaker offers high quality sound, so your melody plays smoothly. As a result, it is an essential component of the project. This compact and reliable speaker is perfect because it provides clear and consistent audio output. To use it, simply connect the speaker’s wires to the Arduino to create a melody playback system. In this way, you can see how even a basic setup can produce impressive sound results.
In this project, the Arduino generates specific frequencies that match musical notes. This means you can bring your project to life with sound. The Arduino plays each note for a duration specified by another array, which sets the length of each note. Additionally, a short delay between notes ensures they remain distinct and do not overlap. Therefore, you get a clear and pleasant melody. Because of these simple concepts, such as arrays and loops, this project is a great learning tool for beginners who want to explore sound generation.
Moreover, understanding how to use this mini metal speaker with your Arduino opens up many possibilities for advanced audio projects. The skills you gain from this project can be applied to create even more complex ideas. By mastering this project, you build a solid foundation for exploring the broader world of audio electronics. Ultimately, this project is a crucial stepping stone for anyone interested in combining technology with music.
Components:

Connections:
- Speaker Positive (+) Wire to Arduino Pin 8: Connect the positive (usually red) wire of the M2850-8B-0L03R speaker to digital pin 8 on the Arduino. This pin will output the tone signals generated by the Arduino.
- Speaker Negative (-) Wire to Arduino Ground (GND): Connect the negative (usually black) wire of the speaker to one of the grounds (GND) pins on the Arduino. This completes the circuit and allows the speaker to function properly.
Additional Notes:
You might want to add a small resistor, if the sound is too quiet or distorted. Between the speaker’s positive wire and pin 8 to limit the current and protect the Arduino pin.
You can use a small amplifier circuit between the Arduino and the speaker for louder sound. For basic melody playback, direct connection should be sufficient.
Code:
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}