How To Make A Security Camera

Nowadays everything is caught on camera, from kids embarrassing themselves to shoplifters caught on CCTV. That is why today I am going to show you how you can turn your Raspberry Pi into your very own security camera.

It’s fine if you don’t have much experience with a raspberry pi, as we’re going to go through everything step by step.

The camera, when finished, will snap a picture of anyone who moves by the camera.

What You Need

A few modules are needed to make this security camera, however all are quite cheap and easily sourced online:

Lastly you need to have a python IDE downloaded onto your Pi. This generally comes with your OS. To ensure your version is up to date, and to make sure the camera libraries are installed, type this into the terminal:

sudo apt-get update
sudo apt-get install python3-picamera

Setting Up The Ultrasonic Distance Sensor And Camera

Setting up the electronics for this project is thankfully pretty simple.

The main problem we will encounter is that the Ultrasonic sensor gives us back a signal that is of a higher voltage than the Raspberry Pi can handle.

The GPIO pins on the Pi can safely handle around 3.3V, however we are given 5V. Using the jumper leads and the two resistors, we can set up the following circuit:

Voltage Divider Circuit. Picture by CamJam

This circuit is a simple voltage divider and will reduce the voltage from the ECHO pin to the PI to 3.3V. For a quick overview of the Raspberry Pi pins please look at the picture below:

Raspberry Pi 3b+ GPIO layout

Now that we have the electronics set up, we can now work on the program itself.

Writing The Code

To get this camera working, we need to think about how it will actually run. In essence the program will need to execute the following steps:

  • The ultrasonic sensor will send out a pulse and a timer will start.
  • The pulse will travel out and reflect off any object it hits.
  • After a set time the receiver will receive the pulse and the timer will stop.
  • We know the speed of the sound wave and the time taken. Using this we can find out the distance from the sensor and the object.
  • If the distance changes at all from normal, the camera will snap a picture of whats causing the change.
  • The picture will then be saved to a file and the camera will set itself back up.

Now lets start on the python code. This uses the GPIO pins 17 and 18 from the Raspberry Pi as the sender and receiver. All you need to do is set the sensitivity of the sensor.

import RPi.GPIO as GPIO
import time
from picamera import PiCamera

# Setup the camera
camera = PiCamera()
camera.resolution = (1024, 768)

# Set the GPIO Modes

# Define GPIO pins to use on the Pi
pinTrigger = 17
pinEcho = 18

# Have an extra variable to provide error checking
intruderPresent = False;

# This will be the current file number
fileNumber = 0

# Set pins as output and input
GPIO.setup(pinTrigger, GPIO.OUT)    # Trigger
GPIO.setup(pinEcho, GPIO.IN)        # Echo

    # We will have the camera run forever
    while True:
        # Set trigger to False (Low)
        GPIO.output(pinTrigger, False)

        # Allow module to settle

        # Send 10us pulse to rigger
        GPIO.output(pinTrigger, True)
        GPIO.output(pinTrigger, False)

        # Start the timer
        StartTime = time.time()

        # The start time is reset until the Echo pin is taken high (==1)
        while GPIO.input(pinEcho) == 0:
            StartTime = time.time()

        # Stop when the Echo pin is no longer high - the end time
        while GPIO.input(pinEcho) == 1:
            StopTime = time.time()
            # If the sensor is too close to an object, the Pi cannot
            # see the echo quickly enough, so it has to detect that
            # problem and say what has happened
            if StopTime - StartTime >= 0.04:
                print("Error, subject too close to camera")
                StopTime = StartTime

        # Calculate the pulse length
        ElapsedTime = StopTime - StartTime

        # Distance pulse travelled in that time is
        # time multiplied b the speed of sound (cm/s)
        Distance = ElapsedTime * 34326

        # That was the distance there and back so halve the value
        Distance = Distance / 2

        # Set the number below to your desired value, mine was 100cm as that was the distance of the nearest wall
        if Distance < 100:
            intruderPresent = True
            if intruderPresent:

                # Set the filename
                fileName = "picture" + str(fileNumber) + ".jpg"
                fileNumber += 1
                # Take the picture
                # Reset the failsafe
                intruderPresent = False
        # Otherwise 
            intruderPresent = False
            print("Nobody there")


# If you press CTRL+C, cleanup and stop
except KeyboardInterrupt:
    # Reset GPIO settings

By the end, your python program should be taking pictures of any movement by your camera and dumping them in the same folder as your python script.

To take a look at the finished results, leave the program running and check the folder where your script is; and see who it caught!

Keeping it running, you can see all kinds of interesting pictures:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.