Post

Detecting Motion with a Raspberry Pi

featured_image

Receive alerts from your Raspberry Pi when motion is detected.

Setting up a motion sensor on your Raspberry Pi is a fun and easy project, and can be used for many different applications. You can push alerts to your phone, activate lights, or even set off a siren when motion is detected. Here I will show you how I setup a simple PIR motion sensor in my garage which will push alerts to my phone if motion were to be detected.

Tools for this project:

  • Raspberry Pi (I used an old Raspberry Pi Model B)
  • PIR motion sensor
  • Breadboard (optional, but makes setup and testing easier)
  • Jumper wires (I love these for interfacing with the GPIO pins)
  • Basic Linux command line/text editor knowledge

Wiring Schematic

wiring_schematic

For this project I would like alerts to be sent to my phone. There are several ways to do this, such as email, text, or pushing notifications through a third party app like Pushbullet or Pushover.

To get started you’ll need a Raspberry Pi running RaspiOS which comes with Python and all the required modules. You will also need to create an account with whatever third party app you wish to use, I will be using Pushover.

Pushover Configuration

Step 1:

After creating an account you will be given a “User Key” as shown blurred out in the top right of the picture below. The Pushover application on your mobile device will ask you for this key once you sign in.

pushover_setup

Step 2:

Register the device that will be running the API (the Raspberry Pi). Scroll to the bottom of your dashboard and click “Create an Application/API Token”.

pushover_token

Step 3:

Enter your preferred details, and then click “Create Application”.

create_token

Step 4:

After creation you will be presented with a token for your API.

test_token

Step 5:

Implement your user and API tokens into a Python script which will react based on the input on the Raspberry Pi’s GPIO pins. Create a new text document and view/copy the example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import http.client, urllib
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)                                      # Note - using the BCM naming method
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)          # GPIO 4 on the board is the PIR input
var=1
while var==1 :
#IF THE PIR IS ACTIVATED
        if  GPIO.input(4):
#PUSHOVER API BELOW
                conn = http.client.HTTPSConnection("api.pushover.net:443")
                conn.request("POST", "/1/messages.json",
                  urllib.parse.urlencode({
                    "token": "YOUR PI API TOKEN HERE",
                    "user": "YOUR PUSHOVER USER TOKEN HERE",
                    "priority": "1",
                    "message": "MOTION DETECTED!",
                  }), { "Content-type": "application/x-www-form-urlencoded" })
                conn.getresponse()
#PAUSE SENDING NOTIFICATIONS FOR 10 SECONDS

Make your new script executable by running “chmod u+x filename” and then run it “python3 filename” Your script is now running, time to trigger the motion the sensor.

alert

Everything looks to be working! I recommend running the script as a service that way if your Pi loses power it will re-start the script while booting.

This post is licensed under CC BY 4.0 by the author.