Reading PIR



  • Trying to read a PIR-sensor with the LoPy without success so far, I turn to this eminent group for help.
    I have tried the following code, with the pir (https://www.adafruit.com/product/189) connected to Vin, Grnd and G5, bridging v3.3 and G28 for pullup:

    What am I doing wrong?

    from machine import Pin
    from time import sleep_ms
    pir = ("G5", mode=Pin.INPUT, pull=Pin.PULL_UP)
    while True:
        sleep_ms(100)
        curr_val = pir.value()
        print(curr_val)
    


  • @Ralph Thanks, for the posting, I will definitely try it out.



  • I have the same sensor and got it running. I'll make a blog post of it soon. Here is some simplified part of my code that should be of use to you (it's untested though)

    from machine import Pin
    import time
    
    pir_triggered=0
    
    def pirTriggered(pin):
        print("Pir triggered")
        global pir_triggered
        pir_triggered = 1
    
    pir = Pin('GP4',mode=Pin.IN,pull=Pin.PULL_UP)
    pir.irq(trigger=Pin.IRQ_RISING, handler=pirTriggered)
    
    while True:
        time.sleep_ms(500)
        if pir_triggered == 1:
            pir_triggered = 0
            print("Sending some http requests to my domoticz server from here...")
    

    Notice I have it connected to GP4. I'm using a callback to set a global flag, then detect it in the main loop. I'm sending an http request to my domoticz server from there to turn on a lamp



  • Following digsatman's suggestion I found this posting

    http://www.instructables.com/id/Convert-a-5v-PIR-Motion-Sensor-to-33v-for-ESP8266/

    which seems to work.

    Thanks everyone for taking your time to reply.



  • @digsatman said in Reading PIR:

    bypass the onboard regulator

    I see what you mean, thanks for pointing it out. Will try this and let you know if it worked.


  • administrators

    I found my PIR module and gave it a go.

    Connected VCC/GND on the PIR module to VIN (5V) and GND on the LoPy.

    First I connected OUT to an LED to see that the module is working. LED would light up when something got close to it.

    Second I checked with a multimeter that OUT would not exceed 3.3V

    Then I connected it to P8 and used the simple input pin example from the quick reference (no pull-up needed). Values change as expected.

    from machine import Pin
    p_in = Pin('P8', mode=Pin.IN)
    p_in()
    


  • What PIR are you using as the one mentioned in the Adafruit example specifically states it works on 5V to 12V even though the output logic is 3.3V compatible. It states that you need to bypass the onboard regulator if you want to run it from a 3V power rail.



  • @livius I have used the adafruit example for the Raspberry PI as a reference and misread it. Nevertheless, also without the pullup I do not get a state change, the value remains 0 (zero).



  • i understand this:

     connected to Vin, Grnd and G5
    

    but for what you do this?

    bridging v3.3 and G28 for pullup
    

    will be better to show picture of your setup


Log in to reply
 

Pycom on Twitter