Detect motion with a Raspberry PI

It’s relatively easy to detect motion with a PIR sensor connected to your Raspberry pi’s GPIO.

This is merely an overview of how to write the python script. For more details on how to connect the sensor, read this.

When you have Python V2 you can follow this, check with python -V.

sudo apt-get update
sudo apt-get install python-gpiozero python-pkg-resources python-dev

Now that you have all dependencies you can create a python script like:

from gpiozero import MotionSensor

pir = MotionSensor(4)
while True:
    if pir.motion_detected:
        print("Motion detected!")

Or you could just print if there is motion once:

from gpiozero import MotionSensor

pir = MotionSensor(4)
print pir.motion_detected

Now make the script executable:

chmod +x

Now you can execute the script:


Leave a Reply

Your email address will not be published. Required fields are marked *