How to use a PIR sensor

Thanks Kevin… Do you know where I can find documents on the PIR settings for the Photon? I have the code finished and it’s notifying me but when it’s armed or even disarmed, it’s constantly sending me push notifications. I can’t seem to get it to stabalize. I’ve changed the PIR sensor out but it did the same thing. Any suggestions would be greatly appreciated. Here’s the simply code I used.

const String data = "";
int Arm = D1;
int Alarm = D0;
int led1 = D6;

void setup() {
    pinMode(Alarm, INPUT);
    pinMode(Arm, OUTPUT);
    pinMode(led1, OUTPUT);
    Particle.function("led",toggle);
    digitalWrite(led1, LOW);
    digitalWrite(Arm, LOW);
    digitalWrite(Alarm, LOW);
}



void loop() {

       if (digitalRead(Alarm) == HIGH && digitalRead(Arm) == HIGH) {
          Particle.publish("Motion Detect", data, PRIVATE);
          digitalWrite(led1, HIGH);
          delay (30000);
       }
       // hang tight here until motion stops

     else  if (digitalRead(Alarm) == HIGH && digitalRead(Arm) == LOW) {
         digitalWrite(led1, LOW);
         delay (30000);
       }

    else if (digitalRead(D0)== LOW){
        digitalWrite(led1, LOW);
    }
}

int toggle(String command){
    
    if(command == "AlarmOn")
    {
    digitalWrite(Arm, HIGH);
    return 1;
    loop();
    }
    else if (command == "AlarmOff"){
        digitalWrite(Arm, LOW);
        return 1;
        loop();
    }
}

Then I built a webpage that looks like this;

<form action="https://api.particle.io/v1/devices/my_device_id/led?access_token=my_token" method="POST">

            <input type="submit" name="args" value="AlarmOn">
            <input type="submit" name="args" value="AlarmOff">

Which sends to the Photon to arm or disarm it. i even placed a resistor to GND to try and stabilize the circuit but it still keeps sending me push notifications. Have any ideas of what I can do?

I split off this non-related question from here

You should add a flag that you set once you’ve dealt with a situation and subsequently only enter the same branche again when the flag got un-set again.

So did @BDub before you.

Must have been seconds before me :wink:
OK, minutes, but I didn’t see that.
Anyway I’ve added extra info in my reply so I’ll hide Brett’s thread :sunglasses:

1 Like

It looks like you might have copied some of my previous code and made modifications :slight_smile:

I recently updated my PIR motion sensor in my hallway to this code that uses the Debounce library. It does appear to be more stable now, but very occasionally gives a false signal. I think it may be due to sunlight and shadows hitting the sensor. One way to fix that is to put the sensor in a black tube.

One tip is usually to power the PIR sensor from VIN to get 5V. Also make sure not to enable PULLUP or PULLDOWN on the INPUT because enabling it will make the GPIO not 5V tolerant. Make sure that your PIR sensor supports 5V as well.

I have a webhook listening to “office-motion” and forwarding on the data to a push notification. More info from this post: [Webhooks] Super Simple Motion Activated Push Notifications

#include <Debounce.h>

Debounce motioninput;

void setup() {
    motioninput.attach(A0, INPUT);
    motioninput.interval(100); // interval in ms
}

void loop() {
    motioninput.update();
    if (motioninput.read() == HIGH) {
        if (Particle.connected()) {
            Particle.publish("office-motion", "HALLWAY 1", PRIVATE, WITH_ACK);
        }
        RGB.control(true);
        while (motioninput.read() == HIGH) {
            RGB.color(0,0,0);
            delay(500);
            RGB.color(255,0,0);
            delay(100);
            Particle.process();
            motioninput.update();
        } // hang tight here until motion stops
        RGB.control(false);
    }
}
2 Likes

@BDub Are you using PIR sensors with adjustable sensitivity?

I picked up some new Panasonic PIR sensors with 6uA low power idle current draw ratings but they are way to sensitive and to make them useful I need a way to cut down on false alarms.

I was wondering how good the debounce library is? Is it just for quick repeated signals or is it more advanced / configurable than that?

Not this one, well… it adjusts automatically over time. It’s tiny. I have about 10 of the adjustable ones though that are larger. I think those might be more stable but this one is doing well now. Shown here with the black tube removed.

It seems to work well, but I haven’t inspected it closely and tested it as thoroughly as I would if I had coded it. I’d like to re-write it though so that a .read() on an input also calls .update() so you can sit in a loop waiting for a pin to go back to the opposite state without having to call .update() as well. Would be nice to simplify the setup and just put everything in the constructor as well. Maybe use a software Timer to call .update().

2 Likes