Use the Raspberry Pi as a DIY Surveillance camera

Although people have been toying with USB webcams on the Raspberry Pi for some time now, the release of the official camera module has reinvigorated interest in video related projects.

The official Raspberry Pi camera module is a Full HD camera that plugs into the Raspberry Pi via the Camera Serial Interface (next to the Ethernet port) on the device. The sensor on the camera is a 5MP with fixed focus lens. It can shoot still images with a maximum resolution of 2592×1944 as well as Full HD 1080p video @ 30 FPS and 720p video @ 60 FPS.

And you get all this in a module that’s only 25x20x9mm in size and weighs just 9 grams! This makes it ideal for projects that require a small steady camera, like surveillance.

Enable camera support

Before you attach the camera, locate the CSI and then pull the tab gently up. Now push the camera module’s ribbon cable into the slot, with the silver contacts on the cable facing away from the Ethernet port. Remember not to push the cable in very deep. Now hold it in place with one hand and push the CSI tab back down with the other hand to lock the camera’s ribbon. Checkout the video below for visual instructions

With the hardware in place, it’s now time to setup the software. Assuming you are using the Raspbian distro on the Raspberry Pi, boot the distro and log in.

Before enabling the camera, make sure you are running the latest version of the Raspberry Pi firmware with these commands:

Depending on how outdated your Rasbian installation is, and the speed of your Internet connection, these commands could take over half an hour to complete.

Once it’s completed, launch the “raspi-config” script helps you to configure your Pi:


Scroll down the list to the item that reads “Enable Camera” and press the Enter key. In the next screen, you’ll be asked to confirm your choice by selecting the “Enable” option. You’ll then be returned to main screen of the configuration utility. You can now select the “Finish” option and restart the Raspberry Pi.

Once you’ve restarted the Raspberry Pi, you can now use the two command-line utilities, raspistill and raspivid to capture still images and videos respectively. Both tools have extensive options and are well documented.

Capture motion

You can use the popular command-line Motion software to detect motion and capture video. Another option is to use the light-weight motion detection Python script written by Raspberry Pi community members.

The script relies on the Python Imaging Library which is a library for analyzing and manipulating images, so make sure you have it installed:

Now grab the script and make it executable:

The script is designed to store images in a directory named “picam” under your home directory, so make sure to create it before executing the script:

You’re now all set. Now run the script:

The script will turn on the red LED on the camera and start taking low-resolution images. It’ll then compare them and look for movement by comparing the pixels in the images. If it detects changes, the script will capture a higher-resolution image.

The script is very efficient and will automatically remove the low-res images it captures for comparison and only store the high-res images that have captured the motion. These images are saved in the ~/picam folder.

You’ll need to adjust some aspects of the script to make sure it works for you. For example, as per the default configuration, the script will even detect minute changes caused by wind.

To edit the script, open it in a text editor. The script is well documented so you shouldn’t have any issues in editing it. If you are using the camera to detect change in a windy area, set the “threshold” variable to a higher value than the default.

Start at boot

To run the script at boot, you’ll need to an init script that runs the “” script and kills it before shutting down the Raspberry Pi.

Again, the community has done all the leg work for you. Just grab their script:

Now move the script into the correct location, and make it executable:

Lastly, make the boot system aware of this script:

That’s it! The script will now start and shutdown along with the Raspberry Pi. You can also manually control it like any other daemon. For example /etc/init.d/picam stop will stop the script and /etc/init.d/picam start will start it.

Use Motion

We are big fans of Motion here at MTE and have previously featured an extensive tutorial on the utility. Motion offers several features over the Python script, such as the ability to monitor it from the local network.

If you decide to use the motion app instead of the Python script, remember that the standard motion packages doesn’t yet work with the Raspberry Pi camera. You’ll instead have to use a special binary, called “motion-mmal”, specially created for the Raspberry Pi by a community member.

To begin with, you’ll have to fetch a lot of dependencies:

Once they are all installed, download the modified version of motion and extract it:

This will extract the motion app as well as a configuration file. You can now run the extracted motion app along with its configuration file using:

While motion is running, you can watch the streaming video on another computer in the network over the 8081 port. So assuming the IP address of your Raspberry Pi running motion is, you can watch the video stream by entering the address on another computer in your network.


Again, just like the Python script, it’ll do you a lot of good to spend some time reading through the “motion-mmalcam.conf” configuration file and tweak it as per your environment. Also remember to take a look at our Motion guide to understand the various configuration parameters in this file.

Image credit: zigazou76

Mayank Sharma

Mayank Sharma has been writing on Linux for over a decade and is a regular contributor to Linux Format magazine.


  1. Here is a complete implementation of a home video surveillance system using multiple Raspberry Pi camera :

  2. As you mentioned: The camera is sensitive to electrostatic discharge (ESD). But:

    1. This is valid for the Raspberry Pi itself, too
    2. You touch the camera and the Raspberry Pi without any ESD precautions
    3. Proper ESD precautions are e.g.: ESD wristband, ESD gloves, ESD working mat, ESD shoes. For casual users the ESD wristband is sufficient.
    4. Touching the heating, water tap, etc. is discharging you, correct. But: It is a one-time-discharge. If you work with hardware afterwords you might re-charge later on again and discharge onto the hardware. So the only proper way is waring an ESD wristband which discharges you all over the time you wear it and touch the hardware.

    1. I get the following message…
      -bash ./ /usr/bin/python^M: bad interpreter: No such file or directory

        1. I am also getting errors when I try to execute the cmnd
          ./ although they are different than the ones previousely
          mentioned. In addition I notice that in addition to
          there was another file loaded called “raw.php?i=yH7JHz9w”.
          What exactly does the cmnd “dos2unix” do and why do you feel it will solve the problem ?


          Jerry Hoffman

          1. It solves the problem, the have dos/windows line-endings. “carriage return” followed by “line feed”.

            sudo apt-get install dos2unix

            From the package

            “Text files under Windows and DOS typically have two ASCII characters at the end of each line: CR (carriage return) followed by LF (line feed). Older Macs used just CR, while UNIX uses just LF.”

  3. What FPS are you achieving for motion-detected capture or are you capturing the full video recording FPS then post-processing what you captured to isolate motion? I have heard that Motion can only capture on Pi at 1 FPS (ref: see Motion header)

  4. I used this year ago.
    Work this email script with you software?

  5. I installed Motion placed at the end but I can not open the camera in the browser says that the page can not be displayed, but you can watch the video and image files generated when the camera captures motion.

    1. You need to set stream_localhost to “off” in the Motion’s configuration file.

      stream_localhost off

  6. the “start at boot” procedure is broke!

    wget -O picam_init

    This paste has been removed! is there somewhere else we can get the script?

  7. nevermind, found it here…..

Comments are closed.