Setup the PIR Motion Sensor on the Raspberry Pi

In this tutorial we will setup the PIR (Passive Infrared) Motion Sensor on the GPIO pins on the Raspberry Pi to detect movement.


We will also create a Python Script to trigger a event once we detect movement


Referenced from this post, the left pin (red / input power), the middle (brown pir output) and right (black / ground):

Connects to the GPIO pin on the raspberry pi:

  • Left Red -> GPIO Pin 2
  • Middle Brown -> GPIO Pin 26
  • Right Black -> GPIO Pin 6

Referenced from this post, so we will have something like this:

Interact with the PIR Motion Sensor

Let's use Python to interact with our PIR Motion Sensor, so whenever we detect movement, that we react on that event by calling a basic print function.

Our code for

# colors

import RPi.GPIO as GPIO  
import time

PIR_PIN = 7  

def react_to_event():  
    timestamp = time.strftime("%F %T")
    print('{red} {text} {default} [{t}] motion detected'.format(red='\33[31m', text='WARNING', default='\x1b[0m', t=timestamp))
    return True


while True:  
    if GPIO.input(PIR_PIN):

When we run it, and then make movement in front of the motion sensor:

$ python
 WARNING  [2020-06-26 16:21:58] motion detected
 WARNING  [2020-06-26 16:21:59] motion detected
 WARNING  [2020-06-26 16:22:00] motion detected

Although, this is just a basic example, you can use the raspberry pi camera to take photos when you detect movement as an example.

I have a post on how to setup the raspberry pi camera if you would like to extend to this.


I stumbled upon these great resources which is related to this post: