0

PIR sensor alarm
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: PIR sensor alarm

by adafruit_support_mike on Tue Jan 29, 2019 12:47 am

I'm here to help you get things working.

To do that, we have to know certain things about the hardware. It's possible that you had one dead PIR sensor, but the chance of getting four that are dead in the same way is pretty low. That's why I'm asking for multimeter readings. The more I know about the way the hardware in front of you works, the faster and better I can help you get a working system up and running.

I know the code Limor wrote works. I've tested it with my own microcontrollers and sensors. The fact that it doesn't work for you means something about your hardware and/or assembly is different from mine. I have to find out what that is, but I don't have physical access to the parts in front of you. I need you to collect information that I can use to help identify and solve the problem.

The information that will be most useful to me right now is a voltage reading on the PIR sensor's output pin, taken with a multimeter. That tool can give me more information than code running on a microcontroller.

adafruit_support_mike
 
Posts: 58911
Joined: Thu Feb 11, 2010 2:51 pm

Re: PIR sensor alarm

by lloydc on Tue Jan 29, 2019 6:08 pm

Greetings Keith,
I cannot begin to thank you enough for your kind assistance. I did as you suggested; I cleared all three of my boards and uploaded the Blink sketch; which worked great. I then; once more; line- by- line; copied Lady Ada's PIR code into the online editor and uploaded it to one of my boards. "SUCCESS!!!!!! "
I did discover that I had to remove the "IN" from printIn to remove that error message. I further discovered that I had to remove one of the curly braces (there were 3 on the bottom) from Lady Ada's code. After that, it complied, verified, and uploaded flawlessly. Now, how do I add the buzzer/siren to the code without the siren going off while the PIR is calibrating?
(I am using a very loud siren!)
I think I went wrong when I tried to fix the error messages that were popping up. I only compounded the errors. (when all along all I needed to do was remove one curly brace)
Again, thank you so very much for your patience and kind assistance. I shall continue to remain a stanch Adafruit customer/user and recommend Adafruit to one and all. I can see I have much to learn about writing sketchs for Arduino.
With gratitude and thanks,
LloydC

lloydc
 
Posts: 21
Joined: Sat Oct 29, 2011 1:47 pm

Re: PIR sensor alarm

by kcl1s on Tue Jan 29, 2019 11:26 pm

Lloyd,
Glad to see you were able to have success. It is a funny thing when learning something new like Arduino. You see a project that looks straight forward and not too difficult but really there are a lot of things that have work together just right to make it work (code, wiring, hardware). Being human it is easy to miss one little thing and just not see it. We have to start with the basics and learn from our mistakes and gain a bit more knowledge each time we try a project.

So you are right we need to add 2 things to the tutorial code. First add code for the buzzer, second add code so the buzzer does not drive us crazy while stabilizing.

The first part is easy. We want the buzzer to react the same way the led does. Since it is just another digital output we can just find where the led commands are and duplicate them for the buzzer. These are the ones I found.

int ledPin = 13;
int buzzerPin = 12;

pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);

digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);

digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);


For the second part you have to know a little about how Arduino code works. All sketches have 2 main parts. The void setup() function and the void loop() function. The thing you need to know is that the code in the setup happens just once on power up or reset. The loop code repeats over and over until we turn off the power. When your project is powered on we want the code to wait until the PIR stabilizes before we let the buzzer work so we do not get annoying false alarms. Since we know we want this to happen only once when we power it on we put that code in setup. We could just put a 60 second delay in and it would work but it would be better to have a visual indication that we are doing something. The blink code you got working is just what we need, only we just want it to blink for 60 seconds. We do this with a for or counter loop so this is what it would look like in setup

Code: Select all | TOGGLE FULL SIZE
  for (int x = 0; x < 60; x++) {  //blink led while stabilizing
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  }

The loop does 60 one second cycles (500 ms on, 500 ms off) then it goes into the void loop() code which repeats forever.

Here is the full modified code that will do what you asked.

Code: Select all | TOGGLE FULL SIZE
/*
   PIR sensor tester
*/

int ledPin = 13;                // choose the pin for the LED
int buzzerPin = 12;
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(buzzerPin, OUTPUT);
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
  for (int x = 0; x < 60; x++) {  //blink led while stabilizing
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  }
}

void loop() {
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    digitalWrite(buzzerPin, HIGH);
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  }
  else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    digitalWrite(buzzerPin, LOW);
    if (pirState == HIGH) {
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

You may need to develop this project a little more as I do not see how it will not sense you in the room and sound a false alarm. Anyway copy (click select all then hover the cursor over the highlighted code and right click and copy) and paste the code in the online editor and see if it works for you. The buzzer is on pin 12 in the code but you can change it if you like.

Keith
EDITED 9:30 AM EST 1/30/19 to correct some code errors.

kcl1s
 
Posts: 1256
Joined: Tue Aug 30, 2016 12:06 pm

Re: PIR sensor alarm

by lloydc on Wed Jan 30, 2019 5:46 pm

Hello Keith,
I made the revisions (that you so graciously gave me) and the circuit; and the code; worked great!!! I think I have resolved the sensor detection problem. I am going to mount the circuit in a project box and place a 1 inch diameter section of PVC pipe over the lense of the PIR sensor, thereby reducing its field of vision ( in essence giving it "tunnel vision". I am going to mount the box on the wall beside the door, so the motion would be confined to an area right in front of the door. I feel much more secure now with my door alarms in place. The siren I am using would wake the dead!!!!
Again, thank you so very much for your assistance.
Respectfully,
LloydC

lloydc
 
Posts: 21
Joined: Sat Oct 29, 2011 1:47 pm

Please be positive and constructive with your questions and comments.