With the following sketch the buzzer sounds for 10 seconds immediately on boot up then works normally? Does anyone see problem in this code or circuit? This nubi welcomes Suggestions.
// SKETCH: PIR_HC-SR505_SIMPLE_SWITCH.ino
// HC-SR505 Motion Sensor with Arduino Pro-Mini to activate alarm buzzer & led
// https://forum.arduino.cc/t/pir-hc-sr505-usage/517617
#define PIR 2 // define pin D2 for PIR sensor (goes HIGH on movement)
#define BUZZER 10 // define pin D10 for buzzer
#define LED 13 // define pin D13 for built-in LED
boolean motion = false; // Motion sensor need to be read or not flag
void setup() {
pinMode(PIR, INPUT); // define pin as INPUT to PIR pin D2
digitalWrite(PIR, LOW);
pinMode(BUZZER, OUTPUT); // define pin as OUTPUT for buzzer pin D10
pinMode(LED, OUTPUT); // define pin as OUTPUT for on-board LED
// The following delays the sensor output for one minute,
// to wait for HC-SR505 PIR's internal 10sec delay to timeout and stablize.
delay(12000); // wait 12 Seconds (60000 ms)
}
void loop() {
int motion = digitalRead(PIR); // read the sensor
delay(10);
if (motion) {
digitalWrite(LED, HIGH); // turn the LED ON
buzz(); // run tone function
}
else {
digitalWrite(LED, LOW); // turn the LED OFF
noTone(BUZZER); // turn buzzer OFF
}
} // End of loop
void buzz() { // This Tone function only works with Piezo or Speaker
tone(BUZZER, 440); // Play music note A4 (440hz)
delay(100); // Narcoleptic delay does not work with tone
tone(BUZZER, 880); // Play music note A5 (880hz)
delay(100); // Narcoleptic delay does not work with tone
} // End of Sketch
Sorry groundFungus, KiCad has no symbols for the PIR or the Boost Converter, so I just quickly put them on the page, forgetting about the grounds. They are indeed there. The included diode is there to protect the Boost Converter when programming through the USB.
PROBLEM IS: The buzzer should sound ONLY when movement is sensed, not immediately. There is a 12 second delay in the Setup that waits past the 10 second timeout of the PIR sensor. It is weird because after that first false 10 second buzz, it is quiet until movement is sensed, and works fine after that? I don't know if the program has a bug, or the PIR is faulty, but I need help to get rid of the bootup buzz??
You cannot digital write a pin that was already declared as an input.
Also, you can’t re-declare a variable. The variable “motion” is stated as a Boolean at the beginning, but is then stated as an int in the loop. Remove the “int “ from the “int motion = digitalRead(PIR);”.
Does the buzzer buzz for 10 seconds immediately after powering up, or after the 12 second delay in setup?
Thanks ZX80, Setting the wait time for one minute (60000ms) solved the problem. I mistakenly changed it to 12000 to reduce my wait time when testing and was going to change it back later. I will reload the working sketch in my next comment. Thanks for all the comments friends. Mac
// SKETCH: PIR_HC-SR505_SIMPLE_SWITCH.ino
// HC-SR505 Motion Sensor with Arduino Pro-Mini to activate alarm buzzer & led
// https://forum.arduino.cc/t/pir-hc-sr505-usage/517617
#define PIR 2 // define pin D2 for PIR sensor (goes HIGH on movement)
#define BUZZER 10 // define pin D10 for buzzer
#define LED 13 // define pin D13 for built-in LED
boolean motion = false; // Motion sensor need to be read or not flag
void setup() {
pinMode(PIR, INPUT); // define pin D2 as INPUT
pinMode(BUZZER, OUTPUT); // define pin D10 as OUTPUT for buzzer
digitalWrite(BUZZER, LOW);
pinMode(LED, OUTPUT); // define pin as OUTPUT for on-board LED
// The following delays the sensor output for one minute,
// to wait for HC-SR505 PIR's internal 10sec delay to timeout and stablize.
delay(60000); // wait one minute (60000 ms = 60 seconds)
}
void loop() {
motion = digitalRead(PIR); // read the sensor (int motion?)
delay(10); // default 10
if (motion) {
digitalWrite(LED, HIGH); // turn the LED ON
buzz(); // run tone function
}
else {
digitalWrite(LED, LOW); // turn the LED OFF
noTone(BUZZER); // turn buzzer OFF
}
} // End of loop
void buzz() { // This Tone function only works with Piezo or Speaker
tone(BUZZER, 440); // Play music note A4 (440hz)
delay(100); // Narcoleptic delay does not work with tone
tone(BUZZER, 880); // Play music note A5 (880hz)
delay(100); // Narcoleptic delay does not work with tone
} // End of Sketch