Introduction: Raspberry Pi Motion-Activated Security Camera

In this instructable, we will build a simple motion-activation security camera. This project started as a stepping stone to a larger project, but it also has many benefits. I had noticed an increase in home security cameras over the year, but I was always surprised at the extravagant costs. So as I was working on my project, I realized that I had accidentally created a motion-activated camera! While simple, I realized that this is a fun project to learn about cameras and motion sensors that also result in a functional object. So, let's get started!

Supplies

For this project, you will need:

  • 1 Model B Raspberry Pi*
  • A computer
  • 1 Raspberry Pi Camera Module*
  • 1 AM312 Mini Pyroelectric PIR Human Sensor*
  • 1 breadboard
  • 1 100 ohms resistor
  • Jumper Wires galore!

*While other versions could work, note that some hardware and code could be different if a substitute is used

Step 1: The Hardware

The photos above provide enough information for you to get started on forming the circuit. However, if you prefer written instructions, here you go:


Connecting the Camera

  • Open the Camera CSI Port on the Raspberry Pi
  • Firmly connect the cord of the camera into the port
  • Then close the port

Congrats! One component down, one more to go!


Connecting the Motion Detector

  • Place the motion detector at the points on the breadboard closest to the middle.
  • Place the resistor so that one end is on the same horizontal as the middle pin of the motion detector and the other end is on its own horizontal.
  • Connect the lonely end to GPIO 4 with a jumper wire
  • Connect the positive pin of the motion detector with the 5V's on the Raspberry Pi
  • Connect the negative pin of the motion detector to the Raspberry Pi's Ground.

The hardware is all done!

Step 2: Software Setup

Enabling Camera & VNC

  • Open the command line and enter:
	sudo raspi-config
  • Open the Interfacing Options (use the arrow keys to scroll and enter to select)
  • Select and Enable Camera
  • This should exit back into the original menu that was opened
  • Open Interfacing Options again and select VNC

VNC, or Virtual Network Computing, gives you remote access and control over your Raspberry Pi's desktop. VNC is needed (as opposed to just SSH-ing to the Pi) because VNC provides a full view of the graphics while SSH only provides access to the Pi's terminal. As this project involves a camera, the need for graphics is self-explanatory.


Installing and Running VNC Viewer

Now that your Raspberry Pi can host VNC, you must ensure your computer can VNC too. The software that I will be using is VNC Viewer by RealVNC. One of the benefits of this software is that it works on a range of operating systems, including Mac, Linux, and Windows.

  • Go to https://www.realvnc.com/en/connect/download/viewer
  • There should be a row of operating systems. Click on yours, then download and install (don't be surprised if it takes up to six minutes!)

Now that VNC Viewer is downloaded let's run it!

  • Open VNC Viewer. A window should pop up with a box at the top; enter the IP address of your Pi here
  • In the verification pop up, fill in the login details of your Pi
  • A new window should appear; it should be identical to if your Pi were plugged into a monitor

Step 3: Taking a Picture

Take a Test Photo

In the manner of your choice, type into the Raspberry Pi's terminal:

raspistill -o test.jpg

This command will execute after five minutes. To check that it has worked, look in the home directory for a file called test.jpg.


Repeated Photos

Here is some code to run in a python file to take 5 pictures (numbered 0-4) and have them stored to the Pi's desktop. The code is courtesy of The Raspberry Pi Foundation (https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/5). The comments are mine.

from picamera import PiCamera 
from time import sleep

# Both are librarys that provide us with the functionalities of the camera and allow the code 
to stop

camera = PiCamera() # Defining the variable of camera

camera.start_preview() #this starts the moniter's entire screen being taken up by the feed from # the camera. The camera feed is now on the moniter's screen and the rest of the code concerns 
# when to store this feed 
for i in range(5): 
# i will start at 0,increment once each time the code runs and run until i=4, this 
#loop will occur five times
    sleep(5) 
    # provides a gap between pictures
    camera.capture('/home/pi/Desktop/image%s.jpg' % i) 
    #stores the pictures on the desktop numbered by 
camera.stop_preview() #The camera feed is no longer connected to the moniter

Step 4: Adding Motion Detection

Here is the final code that should create a camera that takes pictures when motion is detected:

from gpiozero import MotionSensor # Library import for the motion sensor functionalities
from picamera import PiCamera 
from time import sleep

pir = MotionSensor(4) # Defining the input variable

camera = PiCamera() 

camera.start_preview()

i = 0

while True:
  pir.wait_for_motion() # Everytime a motion is sensed, a picture will be taken
    i = i + 1
    camera.capture('/home/pi/Desktop/image%s.jpg' % i)
    sleep(1)
    
                        

Step 5: Installation (or Next Steps)

Now that all of the work is done, it's time to get creative with the installation and implementation! Where do you want to put your security camera? Generally, a Raspberry Pi is hardy enough to last outdoors and is water-proof, so this could be placed anywhere inside or outside your house. Once the placement is decided, you can design the setup. What angle should the camera and the motion sensor be at? Do they need to be in separate locations?

The next steps can go into the engineering of the setup. You can explore different ways of elevating your camera. If this were an actual security project, one consideration would be how to hide the camera. You could look into the relative pros and cons of different materials.

I hope that my instructions have been helpful. I'm sure that your security cameras will come out wonderfully!