{"id":130655,"date":"2014-07-19T12:06:26","date_gmt":"2014-07-19T12:06:26","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=130655"},"modified":"2024-09-09T20:35:20","modified_gmt":"2024-09-09T20:35:20","slug":"raspberry-pi-detect-motion-pir-python","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-detect-motion-pir-python\/","title":{"rendered":"Raspberry Pi: Detect Motion using a PIR Sensor with Python"},"content":{"rendered":"\n<p>Learn how to use a PIR motion sensor with the Raspberry Pi to detect motion. We&#8217;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&#8217;ll use the <span class=\"rnthl rntliteral\">gpiozero<\/span> interface.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Detect Motion using a PIR Motion Sensor with Python Script\" class=\"wp-image-130666\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<p>Throughout this tutorial, we&#8217;ll cover the following main topics:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#motion-sensor-intro\" title=\"\">Introducing the PIR Motion Sensor<\/a><\/li>\n\n\n\n<li><a href=\"#Raspberry-Pi-Motion-Sensor-circuit\" title=\"\">Wiring a PIR Motion Sensor to the Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/raspberry-pi-motion-gpiozero\" title=\"\">Detect Motion with the Raspberry Pi using gpiozero<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this tutorial, check the following prerequisites.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Get familiar with the Raspberry Pi board\u2014if you&#8217;re not familiar with the Raspberry Pi, you can read our <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\" title=\"\"><strong>Raspberry Pi Getting Started Guide here<\/strong>.<\/a><\/li>\n\n\n\n<li>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: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\" title=\"\"><strong>Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/strong><\/a>.<\/li>\n\n\n\n<li>Know how to use the Raspberry Pi GPIOs so that you know how to wire the circuit properly. Read the following tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\"><strong>Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?<\/strong><\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"motion-sensor-intro\">Introducing the PIR Motion Sensor<\/h2>\n\n\n\n<p>A PIR motion sensor detects changes in infrared light in its field of view. This makes it ideal for detecting humans or animals because it will pick up living things (or heat-emitting objects) that move within its range but not inanimate objects. <\/p>\n\n\n\n<p>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, <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-send-email-python-smtp-server\/\" title=\"\">sending a notification<\/a>, or, as we\u2019ll do in this project, simply printing a message in the shell.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/AM312-PIR-Motion-Sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"AM312 PIR Motion Sensor Pinout labeled\" class=\"wp-image-130668\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/AM312-PIR-Motion-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/AM312-PIR-Motion-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The PIR motion sensor outputs a <strong>HIGH <\/strong>signal on the Data pin when it detects movement, or a <strong>LOW <\/strong>signal if it doesn&#8217;t. It only has three pins: <strong>VCC<\/strong>, <strong>GND, <\/strong>and <strong>Data<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Raspberry-Pi-Motion-Sensor-circuit\">Wiring a PIR Motion Sensor to the Raspberry Pi<\/h2>\n\n\n\n<p>The PIR motion sensor has three pins: VCC, GND, and Data. You should connect VCC to the 3V3 pin, GND to a GND pin, and the Data pin to a suitable Raspberry Pi GPIO\u2014we&#8217;ll connect it to GPIO 18 (<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\" title=\"\">take a look at the Raspberry Pi pinout<\/a>).<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>PIR Motion Sensor<\/strong><\/td><td><strong>Raspberry Pi<\/strong><\/td><\/tr><tr><td><strong>Vin\/3v3<\/strong><\/td><td>3.3V<\/td><\/tr><tr><td><strong>Data<\/strong><\/td><td>GPIO 18 (or another GPIO of your choice)<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We&#8217;ll use the <a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">AM312 Mini PIR Motion<\/a> sensor because it works with 3V3 volts, which is perfect for the Raspberry Pi.<\/p>\n\n\n\n<p>Here&#8217;s a list of components you need for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Board<\/a> &#8211; read <a href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-starter-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Best Raspberry Pi Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mini-hc-sr505-pir-motion-sensor\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Mini PIR Motion Sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper Wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1200\" height=\"782\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-Motion-Sensor-Circuit.png?resize=1200%2C782&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Wiring a PIR Motion Sensor to the Raspberry Pi\" class=\"wp-image-130662\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-Motion-Sensor-Circuit.png?w=1425&amp;quality=100&amp;strip=all&amp;ssl=1 1425w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-Motion-Sensor-Circuit.png?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-Motion-Sensor-Circuit.png?resize=1024%2C668&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-Motion-Sensor-Circuit.png?resize=768%2C501&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"raspberry-pi-motion-gpiozero\">Detect Motion with the Raspberry Pi using gpiozero<\/h2>\n\n\n\n<p>After wiring the PIR motion sensor to the Raspberry Pi GPIOs, we can write a Python script to detect the state of the sensor, and thus check whether motion is detected or not. To write that script, we&#8217;ll use the <span class=\"rnthl rntliteral\">gpiozero<\/span> library.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library provides a collection of interfaces for everyday components like LEDs, buttons, potentiometers, sensors, and much more.<\/p>\n\n\n\n<p>It provides an interface called <span class=\"rnthl rntliteral\">MotionSensor<\/span>, designed especially for motion sensors. It comes with several properties and functions that are useful to use with this kind of sensor.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">gpiozero<\/span> library should already be installed if you&#8217;re running Raspberry Pi OS \u2014 if not, you can run:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span class=\"rnthl rntliteral\">python3 -m pip install gpiozero<\/span><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Detect Motion &#8211; Python Script<\/h3>\n\n\n\n<p>The following script prints a different message in the shell depending on whether the sensor is detecting motion or not. Create a new Python file called <em>motion-sensor.py<\/em> and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete Project Details: https:\/\/RandomNerdTutorials.com\/raspberry-pi-detect-motion-pir-python\/\n\nfrom gpiozero import MotionSensor\nfrom signal import pause\n\npir = MotionSensor(18)\n\ndef motion_function():\n    print(&quot;Motion Detected&quot;)\n\ndef no_motion_function():\n    print(&quot;Motion stopped&quot;)\n\npir.when_motion = motion_function\npir.when_no_motion = no_motion_function\n\npause()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi\/pir_motion_sensor.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing Libraries<\/h4>\n\n\n\n<p>First, you import the <span class=\"rnthl rntliteral\">MotionSensor<\/span> component from the <span class=\"rnthl rntliteral\">gpiozero<\/span> library to handle the pushbutton. Then, you also need to import the <span class=\"rnthl rntliteral\">pause()<\/span> function from the <span class=\"rnthl rntliteral\">signal<\/span> module to keep your program running so that it can detect motion events.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import MotionSensor\nfrom signal import pause<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring the Motion Sensor<\/h4>\n\n\n\n<p>Next, you create a <span class=\"rnthl rntliteral\">MotionSensor<\/span> object called <span class=\"rnthl rntliteral\">pir<\/span> that refers to <span class=\"rnthl rntcorange\">GPIO 18<\/span>, which is the GPIO that the motion sensor is connected to. Change the number if you&#8217;re using another GPIO.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir = MotionSensor(18)<\/code><\/pre>\n\n\n\n<p>When you create and use this <span class=\"rnthl rntliteral\">MotionSensor<\/span> object, your program knows that <span class=\"rnthl rntcorange\">GPIO 18<\/span> is connected to a PIR motion sensor.<\/p>\n\n\n\n<p>You can pass other useful arguments to the <span class=\"rnthl rntliteral\">MotionSensor<\/span> class, but the default ones should work just fine by default. If your PIR motion sensor is not working as expected, you might want to try to play with its properties.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>MotionSensor(pin, *, queue_len=1, sample_rate=10, threshold=0.5, partial=False, pin_factory=None)<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what these parameters mean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin<\/span>: the GPIO the button is connected to.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">queue_len<\/span>: 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 \u201ctwitchy\u201d you may wish to increase this value.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">sample_rate<\/span>: the default value is <span class=\"rnthl rntliteral\">10<\/span>\u2014this is the number of values to read from the sensor and append to the internal queue per second.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">threshold<\/span>: by default, it&#8217;s <span class=\"rnthl rntliteral\">0.5<\/span> seconds. When the average of all values in the internal queue rises above this value, the sensor will be considered active\u2014it means motion was detected.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">partial<\/span>: when <span class=\"rnthl rntliteral\">False<\/span> (the default), the object will not return a value for <span class=\"rnthl rntliteral\">is_active<\/span> until the internal queue has filled with values. Only set this to <span class=\"rnthl rntliteral\">True<\/span> if you require values immediately after object construction.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">pin_factory<\/span>: this is an advanced feature that you won&#8217;t probably need to use or worry about.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Detect Motion Events<\/h3>\n\n\n\n<p>You can use the <span class=\"rnthl rntliteral\">when_motion<\/span> and <span class=\"rnthl rntliteral\">when_no_motion<\/span> handlers to detect when the sensor detected motion or not and associate a function to run when each event is detected.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>when_motion<\/strong><\/h5>\n\n\n\n<p>In the following line, when motion is detected (<span class=\"rnthl rntliteral\">when_motion<\/span>), it runs the <span class=\"rnthl rntliteral\">motion_function<\/span> that will print a message on the shell indicating that motion was detected. You can call whenever function you want to do any other different task.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.when_motion = motion_function<\/code><\/pre>\n\n\n\n<p>Here&#8217;s the definition of the <span class=\"rnthl rntliteral\">motion_function<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def motion_function():\n    print(\"Motion Detected\")<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>when_no_motion<\/strong><\/h5>\n\n\n\n<p>You can do something similar for when the sensor stops detecting motion. In this case, when the <span class=\"rnthl rntliteral\">when_no_motion<\/span> event is detected, we call the <span class=\"rnthl rntliteral\">no_motion_function<\/span> function that prints a message on the shell.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.when_no_motion = no_motion_function<\/code><\/pre>\n\n\n\n<p>Here&#8217;s the definition of <span class=\"rnthl rntliteral\">no_motion_function<\/span>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def no_motion_function():\n    print(\"Motion stopped\")<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Keep the Program Running<\/h4>\n\n\n\n<p>In the end, we call the <span class=\"rnthl rntliteral\">pause()<\/span> function. It keeps the program running even after all the code has run through to detect events\u2014in this case, it\u2019s continuously checking the motion sensor state.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pause()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">In summary&#8230;<\/h4>\n\n\n\n<p><strong>1)<\/strong> To detect motion using a PIR motion sensor with the Raspberry Pi, you can use the <span class=\"rnthl rntliteral\">MotionSensor<\/span> interface of the <span class=\"rnthl rntliteral\">gpiozero<\/span> library. You need to import it first like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from gpiozero import MotionSensor<\/code><\/pre>\n\n\n\n<p><strong>2)<\/strong> Define the GPIO that the motion sensor is connected to:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir = MotionSensor(<strong>GPIO_NUMBER_OF_YOUR_CHOICE<\/strong>)<\/code><\/pre>\n\n\n\n<p><strong>3)<\/strong> Then, use the <span class=\"rnthl rntliteral\">when_motion()<\/span> and <span class=\"rnthl rntliteral\">when_no_motion()<\/span> event handlers to run a function when motion is detected or stopped:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.when_motion = FUNCTION_TO_RUN_WHEN_MOTION_IS_DETECTED<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>pir.when_no_motion = FUNCTION_TO_RUN_WHEN_MOTION_STOPS<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Save your Python file. Then run it on your Raspberry Pi. Run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python <em>motion-sensor.py<\/em><\/code><\/pre>\n\n\n\n<p>Move your hand in front of the PIR motion sensor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi PIR Motion Sensor Testing\" class=\"wp-image-130661\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You should get a message on the shell mentioning that motion was detected.<\/p>\n\n\n\n<p>After a few seconds, if it&#8217;s no longer detecting motion, you&#8217;ll a message saying that motion stopped.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"153\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing-shell.png?resize=481%2C153&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi with PIR Motion Sensor - Testing on the shell\" class=\"wp-image-130660\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing-shell.png?w=481&amp;quality=100&amp;strip=all&amp;ssl=1 481w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Testing-shell.png?resize=300%2C95&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><\/figure><\/div>\n\n\n<p>You can stop the execution of the program by pressing <strong>CTRL<\/strong>+<strong>C<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Useful Methods<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">MotionSensor<\/span> interface provides other useful additional methods.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_motion(timeout)<\/span>: it pauses the execution of the script until motion is detected or until the <span class=\"rnthl rntliteral\">timeout<\/span> is reached. By default, there isn&#8217;t a timeout, so the program will wait on that line of code indefinitely until motion is detected.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">wait_for_no_motion(timeout)<\/span>: pause the execution of the script until the motion sensor stops sensing motion or until the <span class=\"rnthl rntliteral\">timeout<\/span> is reached.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">motion_detected<\/span>: returns <span class=\"rnthl rntliteral\">True<\/span> if the current value exceeds the threshold (motion detected) or <span class=\"rnthl rntliteral\">False<\/span> otherwise.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>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 <span class=\"rnthl rntliteral\">gpiozero<\/span> library.<\/p>\n\n\n\n<p>We have another tutorial that shows how to send an email notification when motion is detected:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-motion-email-python\/\">Raspberry Pi: Motion Detection with Email Notifications (Python)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We hope you found this tutorial useful. If you&#8217;re a beginner to the Raspberry Pi, you can get started with the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-raspberry-pi\/\">Getting Started with Raspberry Pi<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pinout-gpios\/\" title=\"\">Raspberry Pi Pinout Guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-remote-ssh-vs-code\/\">Programming Raspberry Pi Remotely using VS Code (Remote-SSH)<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can check all our Raspberry Pi projects on the following link:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi\/\" title=\"\">Free Raspberry Pi Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use a PIR motion sensor with the Raspberry Pi to detect motion. We&#8217;ll show you how to wire the sensor to the Raspberry Pi GPIOs and write &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi: Detect Motion using a PIR Sensor with Python\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-detect-motion-pir-python\/#more-130655\" aria-label=\"Read more about Raspberry Pi: Detect Motion using a PIR Sensor with Python\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":130666,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-130655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-theory-and-blog"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/04\/Raspberry-Pi-PIR-Motion-Sensor-Guide.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/130655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=130655"}],"version-history":[{"count":12,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/130655\/revisions"}],"predecessor-version":[{"id":146492,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/130655\/revisions\/146492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/130666"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=130655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=130655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=130655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}