How to use a PIR sensor

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