Work with the passive buzzer. Create a melody using the “tone” command.
The circuit for a passive buzzer is the same as the active buzzer. The only difference is the leg lengths, a passive buzzer can be wired with either pin going to ground.
// Happy Birthday Melody using tone() and noTone()
int buzzPin = 8;
int dt = 500;
#define NOTE_C4 262
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D4 294
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4,
NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_B4, NOTE_B4,
NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4
};
int noteDurations[] = {
8, 8, 4, 4, 4, 2, 8, 8,
4, 4, 4, 2, 8, 8,
4, 4, 4, 4, 4, 8, 8,
4, 4, 4, 2
};
int songLength = sizeof(melody) / sizeof(melody[0]);
int thisNote;
int noteDuration = 1000/noteDurations[thisNote];
int pauseBetweenNotes = noteDuration *1.30;
void setup() {
// put your setup code here, to run once:
for (thisNote = 0; thisNote < songLength; thisNote++) {
noteDuration = 1000/noteDurations[thisNote];
tone(buzzPin,melody[thisNote], noteDuration);
pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzPin);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Try using the frequency chart to create a new song.