Build a Motion Capture Security System Using a Raspberry Pi

Christian Cawley 11-10-2013

Of the many projects that you can build with the Raspberry Pi, one of the most interesting and permanently useful is the motion capture security system.


All you need is the usual Raspberry Pi setup, a webcam and an optional battery pack, along with a nondescript case for concealing the security system.

With this setup, you can keep an eye on your property from a remote location, receiving alerts if anything is captured by the webcam and a link to the footage if necessary.

Compared with the price of off-the-shelf security systems, this is remarkably affordable and portable, and as you’ll see below can be setup in minutes.

What You Need for Your Motion Capture Security System

There are several approaches for building a motion capture security system for the Raspberry Pi. I initially followed blogger Kean Walmsley’s suggestion to use an ARM-based distro of Arch Linux rather than the usual Raspbian.

However, this just didn’t seem to come together, so I reverted to the Raspbian OS, and made use of the camera drivers and motion detection package, which we’ll come to in a bit.



In addition, you will also need a Raspberry Pi, a formatted SD card, a webcam (see for compatible devices) and a power supply; you will probably need a powered USB hub too, as many webcams need their own power supply when connected to the Pi. This might be a powered USB cable, or a battery solution for the Raspberry Pi, of which there are many options, most of which you can find via the eLinux wiki. Note that there are alternatives, however, such as 4x or 6x rechargeable AA batteries. Some portable iPad chargers will also work – but check your charger’s model number online before attempting to connect to your Raspberry Pi)

If you don’t already have a webcam, you might prefer to spend a few dollars on the official Raspberry Pi webcam.

Follow the instructions outlined previously on MakeUseOf to install Raspbian What You Need to Know About Getting Started with Raspberry Pi The Raspberry Pi computer is the latest in a line of compact, multi-function devices, shipped as a barebones package so that end users can use the hardware for virtually any computing project that comes to... Read More , and for the best results, ensure that it is set up with SSH Setting Up Your Raspberry Pi For Headless Use With SSH The Raspberry Pi can accept SSH commands when connected to a local network (either by Ethernet or Wi-Fi), enabling you to easily set it up. The benefits of SSH go beyond upsetting the daily screening... Read More . You should also change the password, using the raspi_config menu Optimize The Power Of Your Raspberry Pi With Raspbian As befits a man with too much tech on his hands, I’ve been playing with my Raspberry Pi recently, configuring the device so that it works to its fullest potential. It continues to run as... Read More .


Install the Required Software

With the Raspberry Pi booted up and with an SSH connection open from your main computer How to Use SSH in Windows: 5 Easy Ways SSH is an encrypted network protocol used for remote access. Here's how to use SSH in Windows using native and third-party apps. Read More (you can use a keyboard on your Pi, but SSH is better as future communication with the security setup will need to be performed this way) you should begin the process of downloading the latest update:

sudo apt-get update

Next, install the emacs editor, agreeing to the prompts:

sudo apt-get install emacs

Following this, you will need to install a VNC server. This will enable you connect to the security system and view the output. Observe and agree to any prompts.


sudo apt-get install tightvncserver

After installing VNC Server, enter the command vncserver – you will be required to set a password to use the system. You should also agree to the subsequent request to enter a view-only password.


The next step is to edit the VNC Server initialising script. Enter the following to switch to the correct directory:

cd /etc/init.d

You can then run emacs:

sudo emacs tightvncserver

With the console open, copy the following:

#! /bin/sh
# /etc/init.d/tightvncserver

# Carry out specific functions when asked to by the system
case "$1" in
su pi -c '/usr/bin/vncserver'
echo "Starting VNC server "
pkill vncserver
echo "VNC Server has been stopped (didn't double check though)"
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1

exit 0

Right-click to paste it into emacs. A few moments later, it should auto-save. Exit by pressing Ctrl+X and agreeing to save the file upon exiting.

You should then modify the permissions for the VNC Server directory, making the new script executable:

sudo chmod +x tightvncserver

The following command will instruct the VNC Server to load when you boot the Raspberry Pi Security Centre:

sudo update-rc.d tightvncserver defaults

We’re nearly done, so end the current VNC session:

sudo pkill Xtightvnc

You are then ready to restart it:

sudo /etc/init.d/tightvncserver start

On your main device – the computer with which you plan to observe your webcam – you can now open your VNC client. Various suitable clients are available – I’ve used the TightVNC client for Windows.

Configuring Your Webcam


We’re now at the stage where we can begin to prepare the Raspberry Pi for your webcam, which means installing drivers, the necessary libraries and capture software. Enter the following commands in turn, paying heed to the prompts.

First, it’s the motion detection software:

sudo apt-get install motion

Next, install the required libraries:

sudo apt-get install libv4l-0


UVCcapture is software designed to capture the feed from a webcam:

sudo apt-get install uvccapture

You can now attach your webcam and check that all is working. Enter the following command:

dmesg | tail

This will output the details about your camera model, as below.

Build a Motion Capture Security System Using a Raspberry Pi muo rpi secsystem camera

We then need to set the motion capture, so run:

emacs /etc/default/motion

…and set start_motion_daemon to yes.

If you have problems with permissions here, use:

sudo chmod 777 /etc/default/motion

Remember to undo this after the edit has been saved.

With this change saved (CTRL+C, following the on-screen prompts to save), you will then need to edit the motion.conf file.

emacs /etc/motion/motion.conf

The script below (taken from a Raspberry Pi forum thread “Installation Guide for motion detection with webcam“) should be added to motion.conf:

set "daemon on"
set "minimum_frame_time 5" (this can be modified, depending how often you want to take picture)
set "pre_capture 2"
set "post_capture 2"
set "output_normal on"
set "quality 100"
set "ffmpeg_cap_new on"
set "ffmpeg_timelapse 30"
set "ffmpeg_variable_bitrate 2"
set "get_dir /media/webcam/motion"
set "webcam_port 8080"
set "control_port 8081" (important)
set "webcam_localhost off"
set "width 320" (important)
set "height 240" (important)

Note that these settings can be configured as necessary. Those labelled “(important)” should be left as they are.

Configuring Media Settings for Motion

We’re almost done, with just a few media settings and configuration of your wireless dongle to go.

Motion detection images need to be saved to the media folder – but first, you will have to change read/write permissions:

sudo chmod 777 /media

Next, launch the motion software:

sudo /etc/init.d/motion start

Motion can be stopped with:

sudo /etc/init.d/motion stop

You should see a message confirming that the software is running. Is it talking to your webcam? Run the following command to check:

tail -f /var/log/syslog

The output should confirm that the device is live and receiving data. The final step here is to boot the VNC client on your desktop computer and connect to your Raspberry Pi (run ipconfig to check the IP address) using the :1 suffix, which indicates X session number one.

Boxing Up and Positioning the Motion Sensor Security Camera

Although you might prefer to remove the webcam from its stylised case for improved portability and easier secretion, it’s best to assess the options for positioning before doing this.

Indoor testing will help you to establish the camera’s range and motion capture sensitivity, useful for when it comes to setting the device up outside.

For external use, you’ll need to use a suitable waterproof case, perhaps an old ice cream tub or a hobby box from an electronics store. Some drilling may be required in order to run your power cable to the mounted Raspberry Pi-powered security camera if you want to position it outside. However, you may also consider a battery-powered solution for your little computer (note that it will need regular recharging, however). Ethernet may be preferred under the circumstances, so you might want to go into full DIY mode and apply some mastic gun waterproofing, but if you want to use wireless networking, you can. Using a wireless repeater might assist with signal strength outdoors.

Post-Positioning Checks

With the RPi in position, you should head back to your computer and check everything is as it should be.


Begin by checking that the network connection is up and running. Although this should have been checked earlier, your wireless dongle may have trouble connecting in windy weather or if you have some unusual architectural structures or materials Wireless Feng Shui: How to Optimize Wi-Fi Reception in Your House Setting up a Wi-Fi router for optimum coverage isn't as easy as you think. Use these tips to cover your whole house with Wi-Fi! Read More .

With the connection established, you should also head outside and run through several motion capture scenarios to ensure that your movement is detected and alerted.

Finally, for the first day of testing, keep an eye on the battery use if you have chosen a portable power source, as you will need to know how much recharging is required. Be aware that if you have the camera positioned indoors or relatively close to a powered out-building that using mains power is a better solution.

Conclusion: Home Security Solution with an Educational Tool!

Who would have thought that you could do so much with a case-free, stripped down mini-computer ostensibly designed to teach children programming with tools like Scratch? (The possibilities of the Raspberry Pi are seemingly endless. Find out more in our Raspberry Pi Unofficial Manual.)

Like the media centre solution and the other remarkable uses, setting up your Raspberry Pi as a home security solution with motion capture is that it is something that can be done either with a single device co-opted into security whenever you head out, or with an affordable dedicated device. But keep in mind that security cameras can be hacked, so you’ll need a bit of prep work to keep your home security system safe Wyze Exposed Customer Data: 6 Ways to Keep Your Home Security System Safe A data breach hit security cam maker Wyze, leaking the data of millions of users. Here's how to secure your home security cams. Read More .

Related topics: Home Security, Raspberry Pi.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. Isaac
    April 27, 2016 at 10:21 pm

    Does anyone else have issues with the Pi shutting off after a while?
    It worked great, then after a couple days, it stopped working. The red light is on, but I can't access it.
    I was told that this may be a power issue. But I wanted a second oppinion.

    • Christian Cawley
      April 28, 2016 at 6:46 am

      Older Pis (and versions of Raspbian) often go into a low power mode after a while. Connecting a keyboard and tapping a couple of keys can help, as can finding a script to stop it changing modes.

      I haven't had this problem with RPi2 and 3 using SSH however, only on the original Model B
      Hope this helps.

  2. Austin
    March 12, 2016 at 10:26 pm

    hello i just did this project and i do not have any images. i have checked everywhere and still nothing, please help !

    • Christian Cawley
      March 12, 2016 at 11:23 pm

      what output does the command line give when you begin?

      Also, not that this project is two and a half years old. You should be able to find some far better solutions for a Pi security cam.

      • Austin
        March 12, 2016 at 11:56 pm

        everything works but there is no images. do you know where i could find another security camera project using a webcam and not the pi camera module?

  3. Philip
    December 20, 2014 at 2:12 pm


    I just did this and it works just don't know how to stop it. Have 11000 picture just tested it with both my webcams. Is it possible to do this as a program ? With option to delay the start or steam the video to your phone other computer or have email alerts etc ? Have just purchase my Raspberry pi this week and one of the project I got it for was to have a cctv type camera in my flat. But don't have a clue as to how to start writing programs yet !



  4. Tauriq
    February 20, 2014 at 9:48 am

    I can't password after write''vncserver''.. what is password? Thanks

    • Christian C
      February 20, 2014 at 10:46 am

      The password is user-configurable. When you first setup vncserver, it asks you to set a password. It's not asking you for an existing one :)

  5. John Fox
    February 1, 2014 at 7:34 pm

    Can't make it work.
    I followed the instructions but when starting the TightVNC Viewer in Windows, when connecting, it displays: "Xsession: unable to start X session --- no "/home/pi/.xsession" file, no "/home/pi/.Xsession" file, no session managers, no window managers, and no terminal emulators found; aborting."

    Where did it fail?

    • Christian C
      February 20, 2014 at 10:48 am

      Hi John

      It's come to my attention that some of this tutorial is no longer relevant, especially if you've run an update or installed a more recent OS build. For the best results I would suggest checking the RPi forums at or

  6. Alex
    January 1, 2014 at 9:54 am


    I have followed the instructions

    When i get to the:
    The next step is to edit the VNC Server initialising script. Enter the following to switch to the correct directory:
    cd /etc/init.d

    stage the error "bash: cd: /ect/init.d: No such file or directory

    Any help would be appreciated

    All the previous steps work exactly as described

  7. Akash
    December 7, 2013 at 8:03 am

    hi Christian,
    I had gone through your advice. But I got some error.

    I am using Logitech C210 webcam for motion software to make motion activated cam. After connecting I ran "dmesg" command. My Kernal version is 3.6.117. Please help me.

    [5.790301] Linux video capture interface: v2.00
    [6.213422] uvcvideo: Found UVC 1.00 device. (046d:0819)
    [6.610378] input: UVC camera (046d:0819) as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2:1.0/input/input2
    [6.856060] usbcore: registered new interface driver uvcvideo
    [6.996478] USB Video Class driver (1.1.1)
    [ 7.796439] usbcore: registered new interface driver snd-usb-audio
    [10.301633] EXT4-fs (mmcblk0p2): re-mounted.Opts : (null)
    [10.822316] EXT4-fs (mmcblk0p2): re-mounted.Opts : (null)
    [ 23.426901] Adding 102396k swp on /var/swap. Priority:-1 extents:1 across:102396k SS
    [ 26.301843] uvcvideo: Failed to query (SET_CUR) UVC control 3 on unit 2: -110 (exp.2)
    [ 26.601848] uvcvideo: Failed to query (SET_CUR) UVC control 7 on unit 2: -110 (exp.2)
    [ 26.601848] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 2: -110 (exp.2)
    [ 32.931814] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
    [ 37.931908] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
    [43.941808]uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
    [48.841809] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
    [540.830427] uvcvideo: Failed to set UVC probe control : -71 (exp. 26).
    [541.059081] usb1-1.2: USB disconnected, device number 4.
    [544.621932] usb 1-1.2: new low-speed USB device number 6 using dwc_otg.

  8. scar
    December 5, 2013 at 9:48 pm

    Does this work with the raspberryPi cameramodule?

    • Christian C
      December 5, 2013 at 9:56 pm

      In theory, yes. You'll have less messing around with camera drivers, of course, but a quick Google should point you in the right direction.

  9. Domobo
    November 14, 2013 at 10:39 pm

    Great tutoriol.

    Only i connot see the camera...
    Where do i have to look for the problem?

  10. James B
    October 12, 2013 at 8:38 am

    Use nano instead of eMacs - it's already installed, and standard copy paste should work over SSH. CTRL-X, Y to save the file.