Experiment 2 Experiment With Buzzer

by pcduino in Circuits > Arduino

2154 Views, 15 Favorites, 0 Comments

Experiment 2 Experiment With Buzzer

QQ截图20140217114316.png
The components are one buzzer,two jumper wires, and one breadboard.

Schematics

1.png
2.png
The long pin of the buzzer is positive,and the shorter one is negative. This is also marked on the bottom of the buzzer.

Wiring Diagram

3.png
The positive pin of the buzzer is wired to digital IO pin 7 of Arduino, and the negative pin is connected to GND of the Arduino.

Arduino Code

Arduino Code:

int buzzer=7;//Configure the control digital IO pin
void setup()
{
pinMode(buzzer,OUTPUT);//Set the mode of the digital IO pin to be
OUTPUT
}
void loop()
{
unsigned char i,j;//Defined a variable
while(1)
{
for(i=0;i<80;i++)//output a sound with a frequency
{
digitalWrite(buzzer,HIGH);//make a sound
delay(1);//delay 1ms
digitalWrite(buzzer,LOW);//silent
delay(1);//delay 1 ms
}
for(i=0;i<100;i++)//Output a sound with another frequency
{
digitalWrite(buzzer,HIGH);//make a sound
delay(2);//delay 2ms
digitalWrite(buzzer,LOW);//silent
delay(2);//delay2ms
}
}
}