The buzzer is a small component with a piece of metal in it that moves when you apply a voltage across it. This motion causes a small sound, or “click.”
If you turn the voltage on and off fast enough, you get different beeps, squeals, chirps and buzzes. You will use PWM to control the speed of turning the piezo on and off — and, in turn, the audio frequency coming out of the buzzer. Adjusting the PWM enables you to get legitimate notes out of the buzzer.
If you flip the buzzer over and look at the bottom, you will see that one pin has a (+) next to it. That pin gets connected to a signal from the P0 pin. The other pin should be connected to ground.
Note: We use the buzzer module in this project.
Arduino IDE (version 1.6.4+)
First, please plug Osoyoo Magic I/O shield into UNO board:
Then connect the Passive Buzzer module to the D8 port of the Magic I/O shield with a 3-pin PNP cable as below:
After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.
You can download the sketch from this link or copy below code to your Arduino IDE window:
int buzzer = 8;//the pin of the passive buzzer
void setup() {
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop() {
unsigned char i;
while(1)
{ //output an frequency
for(i=0;i<80;i++) {
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
} //output another frequency
for(i=0;i<100;i++) {
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}
Open the Arduino IDE and select corresponding board type and port type for your Arduino board.
After compile this sketch, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.
A few seconds after the upload finishes, you should hear the buzzer beep.
Note again:
The active buzzer has built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.
DownLoad Url osoyoo.com