Home>

I want to use a motion sensor to output to the terminal when motion is detected.

What i am using
・ Raspberry Pi ZERO WH
・ Motion sensor HC-SR501

In this question, as the title suggests, during the program

wait_for_motion ()
wait_for_no_motion ()


I would like you to teach me how to operate and how to use.
I hope the content i am trying is just for reference.

The program will be executed and it will be in a standby state.
If i move your hand in front of the sensor, the detection will be output to the terminal.
It seems that it is done correctly so far, but after that, even if nothing is moved in front of the sensor, the detection will continue to be output at regular intervals.

(It seems that the wiring etc. are done properly. Also, I tried multiple sensors and all the results are the same)
→ I thought there might be something wrong with the program.

Corresponding source code
from gpiozero import LED
from gpiozero import MotionSensor
green_led = LED (17)
pir = MotionSensor (4)
toggle_pin = 22
green_led.off ()
counter = 0
def main_loop ():
    global counter
    while True:
        pir.wait_for_motion ()
        print ("Motion Detected")
        green_led.on ()
        counter = counter + 1
        print (counter)
        pir.wait_for_no_motion ()
        green_led.off ()
        print ("Motion Stopped")
if __name__ =='__main__':
    main_loop ()
What I tried

I googled to death, but I couldn't find the information I was looking for because I couldn't search and speak English. (Reference etc.)

Supplementary information (FW/tool version, etc.)

Here(Completely reproduced) with reference to the video of
version: Python 2.7.16

  • Answer # 1

    I have solved it myself, so I will describe it.

    The video I referred to and the one on Amazon were different from the one I had.
    It seems that there are some pieces, and the pin and the trigger part of continuous detection and single detection are also slightly different.
    If you purchased from Amazon etc., you may not know which data sheet to look at, so you may have to do your best to find it.

    wait_for_motion (timeout = None)
    #Pause the script until the device is activated, or the timeout is reached.
    wait_for_no_motion (timeout = None)
    #Pause the script until the device is deactivated, or the timeout is reached.


    ↑ From the state of processing, probably wait for the detected or undetected state, or if you set a timeout, keep executing this state until that time is reached (?). I think that's what it means.

    Thank you to those who sent us the reference.

    I hope it will be helpful for those who are worried about the same part.