Making a Raspberry Pi Security Camera

Written by Jonathan Ashdown

This project supports a BATEC training for the 2016 Hi-Tec Conference in Pittsburgh.

Once the Raspberry Pi camera has been installed, you will connect a passive infrared sensor (PIR) to the GPIO pins.  When it detects a change in light, it will turn on the camera and record a short movie.  The Raspberry Pi Camera version 2 doesn’t have a red LED, like the previous camera, so if this is used as a security camera, the villain won’t know they are being recorded.

Connections to the Raspberry Pi

Make sure the Pi is turned off before making any connections to it.  The image below shows the proper connections.

PIR_Sensor

Connect a wire from GND to Pin 6.
Connect a wire from OUT to Pin 7 (GPIO 4).
Connect a wire from VCC to Pin 2 (5.0 Volts).

The GPIO pin map below may help. (NOTE:  The Pi2 and Pi3 have the same GPIO map.)

GPIO-labeled

Update and Upgrade the Raspberry Pi

It is always a good idea to update and upgrade the operating system before starting a new project.  This takes two lines of code (shown below).  For this training, we are going to ask you to do the update, and not the upgrade.  You can upgrade at a later time.  Depending on how new the operating system is, this could take more than an hour to complete.

To update

sudo apt-get update

To upgrade

sudo apt-get upgrade

Enable the Camera

In the terminal type:

sudo raspi-config

You will see a screen that looks like the one below.  Use the arrow keys to move down to option 6 (Enable Camera).

1-enable_camera

Hit the enter key, and you can enable the camera.

2-enable

Hit enter, and then us the right arrow key to finish.  You will be asked to reboot the computer, and you should.

3-reboot

So now the camera is set up and ready to go.  Lets start using it for security!

Test the Video

Lets make sure that the camera is taking video.  Use the following command to have the program raspivid take a 5 second video.  The output for it will be a file called video.h264

raspivid -o video.h264 -t 5000

You should see a window appear on your screen and a video is being taken.

To play the video, you will need to use the following command:

omxplayer video.h264

Assuming this works move on to the next step.

A program for the PIR

To write a new program use the following command.  It will use the text editor (nano) to write a new file called pirtest.py.

nano pirtest.py

Copy the code below into that file.


import RPi.GPIO as GPIO
import time

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

while True:
	time.sleep(0.1)
	previous_state = current_state
	current_state = GPIO.input(sensor)
	if current_state != previous_state:
		new_state = "HIGH" if current_state else "LOW"
		print("GPIO pin %s is %s" % (sensor, new_state))

Hit (Control O) to save or write it out, and (Control X) to quit nano.

Test the Motion Sensor

This will require us to run the program that was just written.

sudo python pirtest.py

Wave your hand in front of the sensor.  If things are working properly, you should see the following statements running in your terminal.

GPIO pin 4 is HIGH
GPIO pin 4 is LOW

When your hand is blocking the sensor the pin is set to HIGH.  When your hand is removed it is set to LOW.

Hit <Ctrl C> to kill the program.

Trigger the Camera on Movement

This will require us to use another program.  Again, we will use nano as a text editor.

nano pirCamera.py

In this file, copy the following code:


import RPi.GPIO as GPIO
import time
import picamera   

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

cam = picamera.PiCamera()

while True:
	time.sleep(0.1)
	previous_state = current_state
	current_state = GPIO.input(sensor)
	if current_state != previous_state:
		new_state = "HIGH" if current_state else "LOW"
		print("GPIO pin %s is %s" % (sensor, new_state))
		if current_state:
                        cam.start_preview()
                else:
                        cam.stop_preview()

Hit (Control O) to save the program and (Control X )to exit nano. You now have two files written.  One tests the sensor, and one triggers the camera to produce a preview.  Our last step will be to have a program that will create a video file that will be saved locally.

To test it, run the program

sudo python pirCamera.py

When you wave your hand in front of the sensor, you will see a short video preview.  To stop the program, type <Ctrl C>.

Record a Video Triggered by Movement

This program is really what we’ve been working towards.

nano pirCameraRecord.py

In that file, copy the following code:


import RPi.GPIO as GPIO
import time
import picamera
import datetime

def get_file_name():
        return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False
current_state = False

cam = picamera.PiCamera()

while True:
	time.sleep(0.1)
	previous_state = current_state
	current_state = GPIO.input(sensor)
	if current_state != previous_state:
		new_state = "HIGH" if current_state else "LOW"
		print("GPIO pin %s is %s" % (sensor, new_state))
		if current_state:
                        fileName = get_file_name()
                        cam.start_preview()
                        cam.start_recording(fileName)
                else:
                        cam.stop_preview()
                        cam.stop_recording()

Now when the sensor is triggered, the camera turns on and records a short video for as long as the the GPIO is reading HIGH.

This is a good stopping point, but it many ways it is a good starting point.  Depending on what the programmer wants, video could be converted to .mp4 files, uploaded to YouTube or DropBox, or sent via email.  Thats the great thing about projects, they are never really done.

Leave a Reply

Your email address will not be published. Required fields are marked *