Musical Buzzer
A Piezo buzzer is a device that acts as a small speaker - it vibrates based on the signal it gets from the Ardino, which we control by...
A Piezo buzzer is a device that acts as a small speaker - it vibrates based on the signal it gets from the Ardino, which we control by our programming. By sending alternating high and low voltages, we push the piezo back and forth, forming sound waves. By doing this at the right time, we can produce music!
The Piezo buzzer should be hooked up to pin 12 on the Arduino and grounded to the ground pin on the Arduino. There are three pieces of code to use in this lesson. The section of code below is one, which can be used to write your own music. In the void loop(){} section, you can write your own melody using the notes defined in the program. Remember, if you want to add a pause in your music, use the delay() command. After this lesson, we will listen to a piece of more recognizable music in square-wave form.
int speakerPin = 12;
int time = 500;
//we define functions for each note. time is for note duration in ms
void C(time) {
tone(speakerPin, 261);
delay(time);
}
void D() {
tone(speakerPin, 294);
delay(time);
}
void E() {
tone(speakerPin, 330);
delay(time);
}
void G() {
tone(speakerPin, 392);
delay(time);
}
void A() {
tone(speakerPin, 440);
delay(time);
}
void highC() {
tone(speakerPin, 522);
delay(time);
}
void setup() {
// initially, start the LED in an off state.
C(250);
D();
E();
G();
A();
highC();
A();
G();
E();
D();
C();
}
// put your song below!
void loop() {
}