System sleep on pin change keeps waking despite no pin change

Hi, I have a digital PIR sensor that I’m trying to use to wake my core from sleep…

void setup() {
    pinMode(D2, INPUT);
    pinMode(D7, OUTPUT);
}

void loop() {
    if (digitalRead(D2) == HIGH) {
        digitalWrite(D7, HIGH);
        delay(5000);
        while (digitalRead(D2) == HIGH) Particle.process(); // hang tight here until motion stops
        System.sleep(D2, CHANGE);
    } else {
        digitalWrite(D7, LOW);
    }
    
}

After motion triggers, the core falls asleep as expected but the problem is, a few seconds later, the core always wakes up again. i.e. its not staying asleep despite the PIR sensor staying low.

(At least I’m pretty sure it’s staying low otherwise I’d notice the LED turning on during normal operation.)

I’ve tried INPUT_PULLUP/INPUT_PULLDOWN, RISING/FALLING, etc etc.

The INPUT mode you chose via pinMode() is irrelevant for System.sleep() since that command overwrites the INPUT mode and CHANGE implicitly does not apply an internal pull-resistor.
If you wait for the D2 pin to go LOW before going to sleep, you should in fact use RISING instead of CHANGE.

Not knowing which exact sensor you’re using adding an external pull-down resistor (~4k7) may be worth a try.

And in order to track possible reasons for false triggers, you could have a look at the received signal with an oscilloscope - if you have one or know someone who has.

2 Likes

Thank you for your suggestions thus far.

I’m going a little crazy given that I’ve tried the below code when nothing is connected to D3. The symptom remains that the core will wake from sleep about 5 seconds later. I don’t see how this is possible if there’s no connection or sensor on D3 to trigger a change.

void setup() {
    pinMode(D3, INPUT);
}

void loop() {
    
    if (millis() > 20000) {
        System.sleep(D3, RISING);
    }
    
}

I’ve even tried powering via battery to ensure a ‘clean’ supply.

That would be what we call a floating pin. Although the internal pull-down should prevent that it’s still possible that the pull-down is either not applied, not working or to weak. Hence an external pull-down would be the first thing to try.

2 Likes