Getting System.sleep) to work with PIR motion sensor

Hi all

I am trying to get my Photon to sleep so it can save powerbank/battery energy

However, I tried the following code and it does not seem to get my Photon to wake up or it triggers the notifications at a time interval.

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

void loop() {
    if (digitalRead(D0) == HIGH) {
        Spark.publish("office-motion", "OFFICE", 60, PRIVATE);
        delay(15000);
        while (digitalRead(D0) == HIGH); // hang tight here until motion stops
        System.sleep(digitalRead(D0),RISING);
    }
    
}

Interestingly, I am able to get this to work instead which is quite weird.

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

void loop() {
    if (digitalRead(D0) == HIGH) {
        Spark.publish("office-motion", "OFFICE", 60, PRIVATE);
        delay(15000);
        while (digitalRead(D0) == HIGH); // hang tight here until motion stops
        System.sleep(digitalRead(D0),CHANGE);
    }
    
}

I understand that when the PIR sensor detect something so it will be ‘HIGH’ then it will send a notification. With this, I assume that it will change the state from ‘LOW’ to HIGH’.

So by using this code, ‘System.sleep(digitalRead(D0),RISING);’. It should be correct but it does not seem to work.

Am I missing something or I just have a bad PIR motion sensor?

Any comments on this issue will be great!

Thank guys.

Source: [Webhooks] Super Simple Motion Activated Push Notifications

Try checking the docs for the correct syntax on the sleep modes, there’s something off there :wink:

Also, try setting your input to INPUT_PULLDOWN, to make sure it’s pulled low when it’s not detecting anything.

Hi @Moors7

I will check it out again tomorrow and see what is missing.

Thank you for your input.

First it’s not System.sleep(digitalRead(D0),CHANGE); but System.sleep(D0, CHANGE); (which might work out as the same if digitalRead() returns LOW [=0 =D0]) sorry @Moors7 I spoilt your little quiz, should have read your post more carefully :blush:

And second - most important - there is an open issue with wake-on-pin


BTW: If you are running a tight loop like while (digitalRead(D0) == HIGH); you might cause the device to drop out of cloud. You should rather do while (digitalRead(D0) == HIGH) Particle.process();

1 Like