One of the fun and engaging projects with Arduino is creating tones using a piezo buzzer. You can generate simple beeps, melodic tunes, or even popular themes like the Super Mario theme song. In this tutorial, we’ll explore how to generate tones, work with melodies, and build a simple sound generator using Arduino.
Materials Needed:
- Arduino board (Uno, Nano, etc.)
- Piezo buzzer or speaker
- Jumper wires
- Breadboard (optional)
What is a Piezo Buzzer?
A piezo buzzer is an electronic component that produces sound based on the voltage signal it receives. When used with an Arduino, it can produce various tones by sending different frequencies through the tone() function.
Wiring Your Arduino for Sound
First, connect the piezo buzzer to your Arduino:
- Connect one leg of the piezo buzzer to Pin 8 of your Arduino (or any other digital pin).
- Connect the other leg to the ground (GND) pin.
You can optionally use a breadboard for easy connections, but directly connecting the piezo buzzer to the Arduino works as well.
Understanding the tone() Function
The tone() function is the key to producing sounds with Arduino. Its basic syntax is:
tone(pin, frequency, duration);
- pin: The Arduino pin where the piezo buzzer is connected.
- frequency: The frequency of the tone in Hertz (Hz), which determines the pitch.
- duration: (Optional) The length of time the tone plays, in milliseconds (ms). If omitted, the tone will play indefinitely.
Example:
tone(8, 440, 500); // Play a 440Hz tone (A4) for 500 milliseconds
Simple Tone Example
Let’s start with a basic tone example that plays a middle C note (261 Hz) for 1 second:
void setup() {
tone(8, 261, 1000); // Plays middle C (261 Hz) for 1 second
}
void loop() {
// No code needed here
}
This will make your piezo buzzer produce a single tone.
Creating Melodies
To make things more interesting, you can string multiple tones together to form melodies. Let’s create a simple melody using an array of frequencies and durations.
Example: Playing a Simple Melody
int melody[] = {
262, 294, 330, 349, 392, 440, 494, 523 // C4, D4, E4, F4, G4, A4, B4, C5
};
int noteDurations[] = {
500, 500, 500, 500, 500, 500, 500, 500 // Duration of each note
};
void setup() {
for (int i = 0; i < 8; i++) {
int noteDuration = noteDurations[i];
tone(8, melody[i], noteDuration);
delay(noteDuration + 100); // Pause between notes
}
}
void loop() {
// No code needed here
}
This simple melody plays the C major scale, with each note lasting 500 ms.
Example: Super Mario Theme
Now let’s tackle something more exciting — a portion of the Super Mario Bros. Theme! Below is an Arduino sketch to play the first few notes of the iconic melody.
#define NOTE_E7 2637
#define NOTE_E6 1319
#define NOTE_FS6 1479
#define NOTE_GS6 1568
#define NOTE_A6 1760
#define NOTE_B6 1976
#define NOTE_E5 659
int melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0
};
int noteDurations[] = {
125, 125, 125, 125,
125, 125, 125, 125,
125, 125, 125, 125,
125, 125, 125, 125
};
void setup() {
for (int i = 0; i < 16; i++) {
int noteDuration = noteDurations[i];
if (melody[i] == 0) {
delay(noteDuration); // Rest for the duration of the note
} else {
tone(8, melody[i], noteDuration);
delay(noteDuration + 50); // Add a slight pause between notes
}
}
}
void loop() {
// No code needed here
}
Breaking Down the Super Mario Code:
- Notes: We define several musical notes using their frequencies. These are stored in melody[].
- Durations: Each note is given a specific duration, stored in noteDurations[].
- Rest: A 0 in the melody array signifies a rest, meaning no sound will be played for the duration of that note.
Here are two more examples: the Star Wars Theme and Harry Potter Theme. Let’s dive into each one and how to implement them with Arduino!
Star Wars Theme
Below is an Arduino sketch that plays a portion of the Star Wars Theme:
#define NOTE_A4 440
#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
int melody[] = {
NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F5, NOTE_C6,
NOTE_A5, NOTE_F5, NOTE_C6, NOTE_A5,
0, NOTE_E6, NOTE_E6, NOTE_E6, NOTE_F6, NOTE_C6,
NOTE_G5, NOTE_F5, NOTE_C6, NOTE_A5
};
int noteDurations[] = {
500, 500, 500, 350, 150,
500, 350, 150, 1000,
500, 500, 500, 350, 150,
500, 350, 150, 1000
};
void setup() {
for (int i = 0; i < 18; i++) {
int noteDuration = noteDurations[i];
if (melody[i] == 0) {
delay(noteDuration); // Pause for rests
} else {
tone(8, melody[i], noteDuration);
delay(noteDuration + 50); // Pause between notes
}
}
}
void loop() {
// No code needed here
}
Breaking Down the Star Wars Code:
- Notes: Frequencies of the notes in the Star Wars theme are defined in melody[].
- Durations: Each note’s duration is stored in noteDurations[]. A longer delay is used for more dramatic pauses in the theme.
Harry Potter Theme
Next, here’s the Harry Potter Theme:
#define NOTE_D4 293
#define NOTE_G4 392
#define NOTE_AS4 466
#define NOTE_A4 440
#define NOTE_F4 349
#define NOTE_C5 523
#define NOTE_G5 784
int melody[] = {
NOTE_D4, NOTE_G4, NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_D5, NOTE_C5, NOTE_A4,
NOTE_G4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, 0
};
int noteDurations[] = {
500, 500, 500, 350, 150,
500, 500, 500, 500, 500,
350, 150, 1000, 500
};
void setup() {
for (int i = 0; i < 14; i++) {
int noteDuration = noteDurations[i];
if (melody[i] == 0) {
delay(noteDuration); // Rest for pauses
} else {
tone(8, melody[i], noteDuration);
delay(noteDuration + 50); // Pause between notes
}
}
}
void loop() {
// No code needed here
}
Breaking Down the Harry Potter Code:
- Notes: Frequencies of the Harry Potter theme’s notes are defined in melody[].
- Durations: Each note’s duration is defined in noteDurations[]. A 0 is used to create rests where there is silence.
Conclusion
By using the tone() function and controlling the timing, you can create simple tones, scales, and even popular theme songs with your Arduino and a piezo buzzer. Experiment with different melodies, note lengths, and rhythms to make your projects sound fun and dynamic!
Next Steps
- Try adding buttons to trigger specific melodies.
- Use potentiometers to change the frequency dynamically.
- Expand the Super Mario theme or create your own tunes!
Have fun making music with your Arduino!