Hi guys,

Today lets make a raspberry pi motion detector. i.e. The raspberry pi triggers an alarm when it detects motion through its camera

Let us begin

Parts you will need:

Raspberry pi

Raspberry pi camera

a buzzer

You will need to get the raspberry pi camera working along with the SimpleCV module. If you need instructions how to get this done check my previous post or go to this link.

The program sends output via the GPIO pins so install the GPIO module for python. To do this

open up terminal and type sudo apt-get update

now type sudo apt-get install python-rpi.gpio

wait for the module to get downloaded

open python and import the module by import Rpi.GPIO

if you get no errors the module has been successfully installed

the buzzer i am using is shown in the pic below. there are only two wires. The red one goes to pin 12 on the raspberry pi which we will be using to send GPIO high signals. the blue one is connected to ground pin

How it works

The SimpleCV module is the brains behind this project. what it does is it captures images continuously from the Raspberry pi camera module. It then subracts two consecutive images to check if there is any significant difference between images. lets call this the difference image.

SimpleCV module can perform arithmetic functions such as addition, subtraction etc. on Images. when it subtracts an image with itself we get a black image. i.e. all the pixels are black. This is the case when the camera detects no motion. There is not an entirely black image but it is a dark one.

When we have motion the difference image is not a black image (entirely). How can use this to detect motion. In SimpleCV we can view the histogram of the difference image. if the Image is a dark Image we will get mostly 0(zero) values in the histogram list. if there is some motion, the histogram list will have lesser no of 0 values.

We can count the no of times 0(zero) values are repeated in the histogram list by using the count(0) on the list. This function gives how many times a given element is repeated in a list. It can be used on any list in general. If the no of zeroes fall below a certain threshold the alarm is triggered. in our case it is 10 zeros.

So now that You know how this works lets get building.

The final part

Connect the buzzer to the raspberry pi along with camera

you will need to run this program on the raspberry pi. copy and paste the program in a new python window. save the program as motioncam.py

#6/2/15 from SimpleCV import Camera cam = Camera() lastimage = cam.getImage() difference = lastimage while not display.isDone(): img = cam.getImage() difference = img - lastimage histogram = difference.histogram() if histogram.count(0)<10: print histogram.count(0) lastimage = img

lets get rid of the screen and make the program run automatically at startup. to make the program run at startup by using the crontab module follow these steps

type Cntrl+X and Y to save new crontab

Reboot and check that it works

here is the video for the working of the motion detector

If the above video does not work properly go to this link on youtube

Thats it for this post. comment, like and share and follow for more such projects.