Raspberry Pi motion detector and video image capture

Raspberry Pi motion detector & video  image capture

Raspberry Pi motion detector

Why a Raspberry Pi motion detector? Every now and then I like to just kick back and build something I want, and I have had a few email requests for builds for various simple concepts and starting points. This is great, totally what I want this site, blog and community to be about.

Young people, older folks, those with disability’s and so on; I feel most of us get off to a better start if the first thing we try in a new endeavour actually works, giving a sense of achievement as well as something to show off. It’s good to have a win now and then.

Raspberry Pi motion detector and image capture. I put my Pi in a clear case and mounted the camera module and PIR sensor directly to it. This allows me to simply run the Pi as Header-less WiFi on a USB Power Bank for completely remote operation within my WiFi area. This Step is optional, you can work out your own mounting system for the Camera and PIR.

What you will need to make this…

Raspberry Pi 3 Model B+ from AliExpress

Power Supply 5V 3A For Raspberry Pi 3 B+ from AliExpress 

Raspberry Pi 3 Transparent Clear Case from AliExpress

Camera Webcam Board Module for Raspberry Pi from AliExpress

PIR module Motion Sensor Detector from AliExpress

180Pcs/set Hex Nylon M3 Female Male Standoff  from AliExpress

DuPont Jumper Cable Set from AliExpress

Note: I am using my Raspberry Pi 3 as header-less, WiFi and booting from USB, not that it makes any difference for this project. Just wanted to let everybody know, because, being visually disabled and only seeing a small part of the screen at a time, I’m usually the one yelling where’s the screen keyboard and so on. So be cool”,if any of this is complete ‘GEEK’ to you, Google it or contact me.

First here are some photos of my rig for the Pi, I left off the top cover of course, it just gets in our way. The mount for the camera module is a bit of plastic cut from an Ice cream tub lid. A threaded nylon standoff holds the PIR module to the case.

There was only one hole to be drilled in the case, please start this with a small pilot hole and then enlarge to 3mm or it may crack. Same goes for the PIR and Camera Modules check you are not taking out important tracks or other areas as you enlarge or clean out the holes, be careful as we broke one whilst doing this project. Fortunately, it still works fine. Yes I had help with the drilling cutting etc (note:  the Blind Guy Tech!)

Raspberry Pi motion detector

Setup your camera

Turn off your Raspberry Pi, plug the camera ribbon into the Raspberry PI as shown in the above image.

Install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi.

The cable slots into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port.

Be aware that the camera can be damaged by static electricity. Before removing the camera from its grey anti-static bag, make sure you have discharged yourself by touching an earthed object

Boot up your Raspberry Pi,

Raspberry Pi motion detector

at the prompt, type  “sudo raspi-config“. then enter.

Raspberry Pi motion detector

Select No 5 – Interfacing Options,

Raspberry Pi motion detector

Select  P1 Enable Camera.

at a command prompt type “sudo raspistill -o image.jpg” to prove the camera is working OK.

Raspberry Pi motion detector

Setup your PIR

These 5V “Passive Infra Red” sensors are really simple to set up and use for this type of project. They can be powered from 5V and output 3V so can be connected directly to pins on the Pi’s GPIO header without any other components. The module sets a single output pin high whenever it detects movement within its field of view. It holds this pin High (3.3V) for a minimum period of time. If continuous movement is detected the output pin will stay High. When the time has elapsed and no more movement is detected the output pin returns Low (0V).

Raspberry Pi motion detector

Connect up the PIR as shown in the above diagram.

Raspberry Pi motion detector

If needed, the device has two variable resistors that you can adjust to tweak the performance of the module.

The first one (left-hand side on the photo) determines the sensitivity of the device. The default setting is usually 50%.

The second control (right-hand side on the photo and usually marked on the board as time) allows you to adjust the amount of time the output pin stays at 3V (high) when it is triggered by movement. This can be set from a few seconds to 200 seconds.  a couple of seconds is about right.

Let’s Code it and have some Fun.

Here is the python code, I used the Thonny IDE for Python. So go ahead open Thonny and paste this code in.

#Raspberry Pi motion detector and image capture
# Author : Al McDivitt iotXplain
# Date   : 05/05/2019
from gpiozero import MotionSensor
from gpiozero import LED
from picamera import PiCamera
from datetime import datetime

pir = MotionSensor(17)
camera = PiCamera()
camera.hflip = True

while True:
    now = datetime.now()
    filename = '/home/pi/{0:%H}-{0:%M}-{0:%S}.h264'.format(now)
    print('Motion Detected Recording')
    print('Motion Not Detected Not Recording')

Then save to something like /home/pi/rpi_mot_cam.py. just as long as you know where you put it.

Because to run the program we will open a  command window

Raspberry Pi motion detector

and type at the prompt.

sudo python /home/pi/rpi_mot_cam.py

 press enter.

Movement should be detected, video files will be recorded on movement and placed in your /home/pi/ directory

as .h264 extension files. The viewer I like for these is a VLC media player.

But that is another story. Again google it or contact me.

That’s about all there is, really not hard but a great starter for doing something with IOT and your Raspberry Pi. Hopefully everything worked as planned, but things can go wrong and even new parts can be faulty and that’s OK, just don’t give. Work through elimination to find the problem… it will work… you are the Master.