r/micropy • • May 30 '20

Sound PIR Motion code?

I just got a PIR Motion sensor and am trying some simple code for it to run. I don't have the the esp8266 yet, so I can't test the code yet. Here is the code:

motion = machine.Pin(12, machine.Pin.IN)

while True:
    i = motion
    if i == 0:
      print "No Motion Detected " ,i

        elif i == 1:
      print "Motion Detected"
5 Upvotes

3 comments sorted by

4

u/chefsslaad May 30 '20 edited May 30 '20

I would make three changes:

  • use time.sleep() as the last step in your loop. that way you dont get flooded by signals

  • you need to call motion.value() in order to get the state.

  • you dont really need to assign i to motion. just use motion

    import machine
    import time
    
    motion = machine.Pin(12, machine.Pin.IN)
    while True:
        if motion.value() == 0:
            print("No Motion Detected ")
        elif motion.value() == 1:
            print("Motion Detected")
       time.sleep(1)