Using Adafruit's PIR Sensor



  • I just wrote this in another topic:
    "I have a bunch of PyComs lying around and I tested this with one of my devices and Adafruit's PIR sensor. Worked fantastically"

    Sadly, it turns out that this is not the case.
    Here is what I have:
    I have a PIR sensor from Adafruit that I can read from with this code:

    def read_pir(): #Function for reading status from the PIR device
        pir = Pin("P2", mode=Pin.IN, pull=Pin.PULL_UP)
        if pir() == 1: #If the PIR sensor is detecting any motion
            motion = True
        else: #If the PIR sensor is not detecting any motion
            motion = False
        return motion #Return the read values
    

    However, what I need to do next is to make my PyCom wake up from deepsleep every time the sensor detects motion. I have tried this code:

    import machine
    pir = machine.Pin("P2", mode=Pin.IN, pull=Pin.PULL_UP)
    machine.pin_sleep_wakeup((pir,), mode=machine.WAKEUP_ANY_HIGH, enable_pull=True)  
    

    I have also tried:

    import machine
    pir = machine.Pin("P2", mode=Pin.IN, pull=Pin.PULL_UP)
    machine.pin_sleep_wakeup(["P2"], mode=machine.WAKEUP_ANY_HIGH, enable_pull=True)  
    

    But what I find strange is that (even if nothing is connected to P2), it wakes up from deepsleep immediately after I put it in deepsleep. That is obviously not the expected behaviour, I want the device to sleep until the sensor detects any motion.

    What happens is that when I run this code:

    import machine
    from machine import Pin
    pir = Pin("P2", mode=Pin.IN, pull=Pin.PULL_UP)
    machine.pin_sleep_wakeup((pir,), mode=machine.WAKEUP_ANY_HIGH, enable_pull=True)
    machine.deepsleep(10000000)
    

    The device restarts with a DEEPSLEEP_RESET immediately after I run the file, and like I said, this happens even when nothing isn't connected to the pin.

    Is there any way I can solve this?


Log in to reply
 

Pycom on Twitter