Raspberry Pi Motion Sensor
Raspberry Pi Motion Sensor
com/raspberry-pi-detect-motion-pir-python/
Menu
Learn how to use a PIR motion sensor with the Raspberry Pi to detect motion.
We’ll show you how to wire the sensor to the Raspberry Pi GPIOs and write a
Python script that does a certain task when it detects motion. To write the Python
script, we’ll use the gpiozero interface.
Table of Contents
Throughout this tutorial, we’ll cover the following main topics:
Prerequisites
Before continuing with this tutorial, check the following prerequisites.
1. Get familiar with the Raspberry Pi board—if you’re not familiar with the
Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
2. You must know how to run and create Python files on your Raspberry Pi.
We like to program our Raspberry Pi via SSH using an extension on VS
Code. We have a detailed tutorial about that subject: Programming
Raspberry Pi Remotely using VS Code (Remote-SSH).
3. Know how to use the Raspberry Pi GPIOs so that you know how to wire the
circuit properly. Read the following tutorial: Raspberry Pi Pinout Guide:
How to use the Raspberry Pi GPIOs?
You can program the Pi to react to changes in infrared light by triggering an event
such as turning on a light, sounding an alarm, sending a notification, or, as we’ll do
in this project, simply printing a message in the shell.
The PIR motion sensor outputs a HIGH signal on the Data pin when it detects
movement, or a LOW signal if it doesn’t. It only has three pins: VCC, GND, and
Data.
Vin/3v3 3.3V
GND GND
We’ll use the AM312 Mini PIR Motion sensor because it works with 3V3 volts,
which is perfect for the Raspberry Pi.
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all
the parts for your projects at the best price!
detected or not. To write that script, we’ll use the gpiozero library.
pir = MotionSensor(18)
def motion_function():
print("Motion Detected")
def no_motion_function():
print("Motion stopped")
pir.when_motion = motion_function
pir.when_no_motion = no_motion_function
pause()
Importing Libraries
First, you import the MotionSensor component from the gpiozero library to
handle the pushbutton. Then, you also need to import the pause() function from
the signal module to keep your program running so that it can detect motion
events.
Next, you create a MotionSensor object called pir that refers to GPIO 18 ,
which is the GPIO that the motion sensor is connected to. Change the number if
you’re using another GPIO.
pir = MotionSensor(18)
When you create and use this MotionSensor object, your program knows that
GPIO 18 is connected to a PIR motion sensor.
You can pass other useful arguments to the MotionSensor class, but the default
ones should work just fine by default. If your PIR motion sensor is not working as
queue_len : length of the queue used to store values read from the sensor.
The default is 1 which disables the queue. If your motion sensor is
particularly “twitchy” you may wish to increase this value.
threshold : by default, it’s 0.5 seconds. When the average of all values
in the internal queue rises above this value, the sensor will be considered
active—it means motion was detected.
partial : when False (the default), the object will not return a value for
is_active until the internal queue has filled with values. Only set this to
True if you require values immediately after object construction.
when_motion
pir.when_motion = motion_function
def motion_function():
print("Motion Detected")
when_no_motion
You can do something similar for when the sensor stops detecting motion. In this
case, when the when_no_motion event is detected, we call the
no_motion_function function that prints a message on the shell.
pir.when_no_motion = no_motion_function
def no_motion_function():
print("Motion stopped")
Instead of simply printing messages on the shell, you can associate any other
function that you need to run when those motion events are detected, for example,
turning on/off and LED, sending a notification message, etc.
In the end, we call the pause() function. It keeps the program running even after
all the code has run through to detect events—in this case, it’s continuously
checking the motion sensor state.
pause()
In summary…
1) To detect motion using a PIR motion sensor with the Raspberry Pi, you can use
the MotionSensor interface of the gpiozero library. You need to import it first
like this:
pir = MotionSensor(GPIO_NUMBER_OF_YOUR_CHOICE)
pir.when_motion = FUNCTION_TO_RUN_WHEN_MOTION_IS_DETECTED
pir.when_no_motion = FUNCTION_TO_RUN_WHEN_MOTION_STOPS
Demonstration
Save your Python file. Then run it on your Raspberry Pi. Run the following
command:
python motion-sensor.py
You should get a message on the shell mentioning that motion was detected.
After a few seconds, if it’s no longer detecting motion, you’ll a message saying that
motion stopped.
Wrapping Up
In this tutorial, you learned how to use the PIR motion sensor with the Raspberry
Pi and run different tasks whether motion is detected or not using the gpiozero
library.
We have another tutorial that shows how to send an email notification when
motion is detected:
We hope you found this tutorial useful. If you’re a beginner to the Raspberry Pi,
you can get started with the following tutorials:
You can check all our Raspberry Pi projects on the following link:
Recommended Resources