Hi guys in this tutorial we will see how to use buzzer with PIR sensor. So lets get started, for this you will need

  1. Piezo Buzzer,
  2. PIR sensor (HC-SR501),
  3. Arduino
  4. Breadboard
  5. Jumper Wire

If you like PIR sensor projects you can have a look on my previous turorial for PIR sensor in Arduino & PIR sensor as a counter. Do connection as shown in diagram.

skecth:

void setup() {
pinMode(3, INPUT);
pinMode(9, OUTPUT);
}

void loop() {
if(digitalRead(3) == HIGH){
tone(9, 400);
delay(300);
noTone(9);
delay(300);
tone(9, 400);
delay(300);
}
else {
noTone(9);
 }
}
Explanation:

Now let’s come to the programming part. Set pin number 3 for PIR sensor as an input pin. Set pin number 9 for buzzer output.

pinMode(3, INPUT);
pinMode(9, OUTPUT);

In loop function if digitalRead pin number 3 is equal to high then only buzzer will make sound.

if(digitalRead(3) == HIGH){
tone(9, 400);
delay(300);
noTone(9);
delay(300);
tone(9, 400);
delay(300);
}

Now tone will generate square wave of specific frequency set by pin. Syntax for this

 tone(pin number, frequency)

Pin number on which you want to make sound & frequency of tone in hertz it must be a positive value. It will dealy sound duration for 300 millisecond .

NoTone will not produce any sound to that pin number. It will delay no sound duration for 300 millisecond. Then It will again make sound. Instead of this you can use this syntax

tone(pin number, frequency, duration)

in which you can give duration of buzzer sound directly in the function but it will make only one sound. If digitalRead is equal to low then no Tone will be generated.

When I move my hand over the sensor it will detect my hand and make sound.

Till then keep learning keep making :).

Categories: Arduino