How to Play Melody Using Arduino: Simple Guide and Example
To play a melody on Arduino, use the
tone() function to send sound signals to a buzzer or speaker connected to a digital pin. Define the notes and durations, then call tone(pin, frequency, duration) for each note in sequence to create the melody.Syntax
The tone() function generates a square wave of the specified frequency (in hertz) on a pin, producing sound through a buzzer or speaker. The syntax is:
tone(pin, frequency): Plays a tone continuously on the pin.tone(pin, frequency, duration): Plays a tone for the given duration in milliseconds.noTone(pin): Stops the tone on the pin.
Parameters explained:
- pin: Arduino digital pin connected to the buzzer.
- frequency: Sound frequency in hertz (e.g., 440 for A4 note).
- duration: How long to play the tone in milliseconds.
arduino
tone(pin, frequency); tone(pin, frequency, duration); noTone(pin);
Example
This example plays a simple melody using a buzzer connected to pin 8. It defines note frequencies and durations, then plays each note in order.
arduino
#define BUZZER_PIN 8 // Notes in the melody (frequencies in Hz) int melody[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Note durations: 4 = quarter note, 8 = eighth note, etc. int noteDurations[] = {4, 4, 4, 4, 4, 4, 4, 4}; void setup() { // no setup needed } void loop() { for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZER_PIN, melody[thisNote], noteDuration); delay(noteDuration * 1.3); // pause between notes } delay(1000); // pause before repeating }
Output
The buzzer plays a sequence of 8 musical notes, each lasting a quarter note duration, then pauses 1 second before repeating.
Common Pitfalls
Common mistakes when playing melodies on Arduino include:
- Not connecting the buzzer or speaker correctly to the Arduino pin and ground.
- Using
delay()incorrectly, causing notes to overlap or sound cut off. - Forgetting to add a pause between notes, which makes the melody sound rushed.
- Using frequencies outside the audible range or invalid pin numbers.
Example of a common mistake and fix:
arduino
// Wrong: No pause between notes, notes overlap for (int i = 0; i < 8; i++) { tone(BUZZER_PIN, melody[i], 250); // Missing delay here } // Right: Add delay to separate notes for (int i = 0; i < 8; i++) { int duration = 250; tone(BUZZER_PIN, melody[i], duration); delay(duration * 1.3); // pause between notes }
Quick Reference
| Function | Description | Example |
|---|---|---|
| tone(pin, frequency) | Play tone continuously on pin | tone(8, 440); |
| tone(pin, frequency, duration) | Play tone for duration (ms) | tone(8, 440, 500); |
| noTone(pin) | Stop tone on pin | noTone(8); |
Key Takeaways
Use the tone() function with pin, frequency, and optional duration to play notes.
Add delays between notes to separate sounds and create a clear melody.
Connect the buzzer correctly to Arduino pins and ground for sound output.
Use arrays to store note frequencies and durations for easy melody playback.
Stop tones with noTone() if you want to silence the buzzer before playing new sounds.