Hi everyone! I am creating a circuit that monitors a user's range from a heat signature. My goal is trying to improve the accuracy of actually following the 6 feet rule. Doing this, I am using a PIR sensor, a piezo electric buzzer, a breadboard, and an Arduino NANO. This is my code-
int beeper = 12; // LED
int pirPin = ; // PIR Out pin
int pirStat = 0; // PIR status
void setup() {
pinMode(beeper, OUTPUT);
pinMode(pirPin, INPUT);
}
void loop(){
pirStat = digitalRead(pirPin);
if (pirStat == HIGH) { // if motion detected
digitalWrite(beeper, HIGH);
tone(beeper, 5000, 70);
delay(1000);
}
else {
digitalWrite(beeper, LOW); // turn LED OFF if we have no motion
}
}
Thank you!!!
int pirPin = ; Oops.
Please remember to use code tags when posting code
PIR sensor only output HIGH/LOW right? Not ADC...So, in order for your system to detect a range of 6 feet, you have to test it yourself and adjust the sensitivity of your PIR module. Try this code:
const uint8_t PIRoutPin = 11; // PIR Out pin connected to digital pin 11 of Arduino Nano
const uint8_t PiezoPin = 12; // Piezo Buzzer pin connected to pin 12, other side to ground.
bool lastPirState; // Last state of PIR sensor for latching.
const uint16_t thisNote = 5000; //Change this value for four preferred sound, hertz.
const uint16_t noteDuration = 1000; //Change this value for your sound duration, millisecond.
void setup() {
pinMode(PIRoutPin, INPUT); //Set digital pin 11 as digital input.
//(No need set the PiezoPin as input/output, we're only generating a frequency prior to sound on that pin ).
lastPirState = LOW;
}
void loop(){
if( !lastPirState && isDetected() )
{
lastPirState = HIGH;
tone(PiezoPin, thisNote, noteDuration); //Generate tone to Piezo with 1 sec. duration.
}
if( lastPirState && isDetected() == LOW ) //to latch the code, in order to avoid multiple detection.
{
lastPirState = LOW;
}
return;
}
bool isDetected()
{
return (digitalRead(PIRoutPin) ? HIGH : LOW);
}
PS: Sir, if you have any question, just ask me here. And pls. consider following me on my new youtube (removed) Hope this helps.
Thank you iamecearchie and TheMemberFormelrlyKnownAsAWOL! It was a big help. But as I did this, I came up with another problem- my buzzer, after keeping my hand near the PIR sensor, it buzzes 3- 4 times before stopping, is there a way to make it buzz only while the heat signature is there? Thanks.
@adharsh2008, I think the code is working as per the plan. As you can see, I put a latch code in the program so that it'll buzz only one time. I suspecting that's already the hardware issue, your PIR sensor output PIN detects multiple HIGH and LOWs signal.
However, if you really want to buzz only while the heat is detected..try this code attached...
PIRsensor_withBuzzer.zip (749 Bytes)