Hi! In this tutorial we will be creating a motion detector with a raspberry pi and a normal USB webcam. There are lots of webcam and motion detector solutions out on the market today, but making one in a DIY fashion gives us a lot more control over how it operates. In the end, it will be capable of triggering any kind of action you’d like when a motion event is detected. At the end of the tutorial I’ll talk about some alternative webcam and motion detection solutions. Before you get started with this project, you’ll need to have several pieces of equipment handy:
- Raspberry Pi 3 (any model with a wifi adapter will do)
- 2.5A Power Supply for Raspberry Pi
- 8+ GB Micro SD card and Micro SD card reader
- A USB Webcam
- A monitor (anything that supports HDMI)
- An HDMI cable
- A USB mouse
- A USB keyboard
Step 1: Setting Up the Raspberry PI
The Raspberry PI's "Raspbian" operating system runs off of the micro SD card, so in order to have a functioning raspberry pi, we'll have to install the operating system onto our micro SD card before anything else. If you're very savvy with the command line, you can install Raspbian directly onto the micro SD card, but if you're like most of us mere mortals it will be much easier to install the NOOBS OS installer instead.
Putting the NOOBS (New Out Of Box Software) installer on your SD card is pretty simple: Download it on your computer and transfer it over to the SD card using your card reader. There are a couple snags that you should watch out for, though:
- The SD card must be formatted in the FAT (File Allocation Table) format. My SD card was already formatted as FAT when I got it, but you can format it yourself if this is not the case. This tutorial gives directions for formatting SD cards on Windows, Mac, and Linux.
- Make sure the write-lock is turned off on your SD card reader (It's a tiny physical switch on the card reader). I didn't know this was a thing when I started, so I was very confused when I couldn't copy any files onto the card.
If you're still struggling to get NOOBS on your SD card, this tutorial is pretty comprehensive and should answer most of your questions.
Once you have NOOBS on the SD card, it's time to plug in the Raspberry PI: first insert the micro SD card into its slot on the underside of the Raspberry PI. Next connect the monitor, mouse, and keyboard. Finally, plug in the power adapter. Once the Raspberry PI receives power, it will start up automatically.
On the monitor you'll see NOOBS initializing. Once it's ready, it will prompt you to select an operating system. Raspbian is the most popular OS, so let's stick with that for this tutorial. Select it and let NOOBS do its thing as it installs the Raspbian OS.
Eventually you'll see Raspbian's desktop view come up. Congrats! you now have a functioning Raspberry PI.
Step 2: Setting Up the Webcam
Even though we have Raspbian running on our PI, it's still a brand new installation. We'll need to install some additional software to be able to use our webcam and eventually turn it into a motion detector. This will involve performing a few simple commands in the command terminal.
First we'll need to get the software that supports USB webcams (UVC support). Open up the terminal and run:
sudo apt-get install rpi-update sudo rpi-update
Next, we need to update the software packages. This could take several minutes:
sudo apt-get update sudo apt-get upgrade
After this finishes running, we should test that the webcam works. Plug your webcam into the Raspberry PI and run these two commands:
sudo apt-get install fswebcam fswebcam test.jpg
This will install the fswebcam program which will detect the webcam, take a picture, and save it into the test.jpg file. Navigate to test.jpg and confirm that your webcam did indeed take a picture.
Step 3: Installing and Configuring Motion
In order to turn our webcam into a motion detector, we will be using the open-source "motion" library which was designed just for this purpose. It will use the webcam to continuously check for movement, and is capable of triggering an event when motion is detected. To get started, we'll install motion and simply display a running feed from the webcam. First, install motion by running
sudo apt-get install motion
One useful thing that motion does is set up a lightweight webcam server. This means that the input from the webcam can be streamed over the internet. In order to enable this, we'll have to do a bit of configuring. Open up the main config file for motion by running
sudo nano /etc/motion/motion.conf
This file contains all of the configuration parameters that describe how motion runs. These are the most important ones you might want to change:
- daemon on: Set this to ‘on’ if you want motion to be able to run as a background process
- stream_port: This is the default port that the webcam server will run on.
- webcam_localhost: Set this to off if you want to access the webcam stream remotely
There are also lots of configuration parameters that effect the framerate, behavior of motion capturing, and so on. Once you’ve had a good look at the config file, let’s turn on motion. Run:
Motion will initialize and either run in daemon mode (hidden from the console), or will start reporting a feed of its actions to the terminal. The webcam stream should be running now, so let's try to watch it. First we need to install software that can let us stream from the webcam server. Download and install VLC by opening up a new terminal and running:
sudo apt-get install vlc
Once VLC is set up, we can watch the webcam stream by running
This should pop up a window showing a current feed from the webcam. By default, the stream is one frame per second and has a few seconds of lag, but these parameters can be tweaked in the motion.conf file.
Step 4: Triggering Actions on Motion Events
The last matter of business is to actually use our motion detector to trigger cool events! In the real world you could use a system like this to trigger an alarm, count the number of people walking by, or to automatically turn on your lights. In this example we’ll just use the motion event to make a popup appear on the Raspberry Pi’s monitor.
In the motion.conf file, there is a configuration parameter called “on_event_start” that gets triggered every time a new motion event is detected. It is here where we can introduce the desired functionality into our motion detector. To make a popup, we’re going to need a small python script. Copy and paste this code and put it into a file named “motion_detected.py” in your home directory.
From tkinter import * import tkMessageBox tkMessageBox.showInfo(‘Motion Detected!’, ‘Images stored in /var/lib/motion’)
And then in the motion.conf file set the on_event_detected parameter to:
on_event_detected python /home/pi/motion_detected.py
Restart motion so that it reads the new configuration file. To do this, run
ps -aux | grep motion
to find the process id of the currently running motion instance. To kill it and restart, run:
kill <motion_pid> sudo motion
Now, when you pass your hand in front of the webcam, a motion event will be triggered and a pop-up window should be displayed on your monitor! Congrats, you’ve made a simple DIY motion detector alarm system!
Step 5: Alternatives
Building DIY projects can be a lot of fun and very educational, but before you devote a lot of time to a project It's always good to consider similar solutions on the market. Sometimes it makes sense not to reinvent the wheel. With that being said, here are some alternative motion sensing technologies out on the market now:
Z-Wave Smart Sensors
If you aren't so concerned about keeping video, then there are motion sensors on the market that work without taking a video. One example of this is Z-Wave's array of smart sensors. Z-Wave uses a proprietary "smart hub" which can integrate with your phone and laptop to control and monitor features of your home. The pro is that Z-wave sensors are easy to monitor and install, but obviously it's more expensive because it relies upon a proprietary technology. In addition, you'll be constrained by what the z-wave system allows you to do.
Streaming and IP Webcams
If you still want to set up a web-enabled video motion detector, but don't want to go through the hassle of manually setting one up for yourself, there are a lot of plug-and-play options available on the market. For instance, consider the EZVIZ Mini HD 720p WiFi Home Security Camera. It's a webcam that's already internet-enabled, has motion detection capabilities, integrates with a smartphone app, and can even trigger IFTTT actions. The only setup involved is to just plug it in. Clearly this is great because it's dead simple to set up your streaming motion detector webcam, but the drawback is that it's a bit expensive and is still less flexible than taking a DIY approach.
Better DIY Solution
In this project I used a Raspberry Pi 3 which was connected to a monitor, mouse, and keyboard so that setting up the webcam would be relatively simple. While this is convenient for setup, it's also more expensive than it has to be, and it becomes inconvenient if you want to remotely control your motion detector. It is possible to do all of the setup in this project without using a monitor, mouse, or keyboard, but you need to have some networking know-how and use SSH to communicate with the Pi remotely.
That's it for this tutorial! I hope you keep playing around with your new motion detector and find an awesome way to use it.