Install a motion sensor and a camera on your Raspberry Pi [Full Guide]


First of all, you should know with the latest versions of Raspbian , installation, configuration and use of Sensing has improved significantly. Now there is an official package and setup is faster

Step 0 – Equipment

You must purchase all the necessary equipment … I think of the following minimum. Raspberry Pi, power cable, Ethernet cable (and / or WiFi dongle is compatible) any case .

Note that if you want to use your Raspberry Pi as a real security camera, you will necessarily invest in housing. You can take a “standard” housing as those I have presented in my previous articles but in this case, be aware that the web and the camera will be located outside the actual housing without protection.

Or, you can use this type of housing that I found on eBay for just under 20 euros (costs not included ports).


the advantage of this type of housing and protect your Raspberry Pi while making available 2 slot on the top for camera and motion sensor (locations which can also rotate). Caution, however, it must be noted that this case will be delivered in “kit” and that you belong to assemble the pieces (I say this because … I have read a little (too) quickly I thought he would get all mounted)! : P

But in truth, you will receive this instead:


And true, it’s a bit like Ikea , it mounts once is not dismantled is most at risk to break some plates. In short, for my part, I am happy and it makes perfect job for the price; you now know what to expect! 😉

And finally the material for the camera you’ll have no trouble connecting to the table … But for the sensor, I suggest you link and the diagram below that is better than long speeches:


this way, you know exactly which pins to use to connect your sensor! 🙂

Step 1 – Setting the sensor motion

From there, we’ll assume you have properly installed your Raspberry Pi (with or without housing) and you have properly installed Raspbian on the Micro SD card of the latter. You also set your Raspbian by setting your password, hostname, activated the camera and have also performed the last update (again, I return you to my previous articles ):

 sudo apt-get update && sudo apt-get upgrade -y 

We will now install the package motion available that will allow us to use our motion sensor .

to do this:

 sudo apt-get install motion 

Accept the installation of all dependencies course

Overall you. 2 config files to know later :

  • /etc/motion/motion.conf

  • /etc/default/motion

Pour check the status of the detector, as with any debian package, you can run the following command:

 sudo /etc/init.d/motion status 


You see that we have a warning message and we need to change a setting (the only one) that is in the file / etc / default / motion . Edit it with your favorite tool and change the start_motion_daemon to yes.


Effectuez then restart the process with the following command:

 sudo restart /etc/init.d/motion 

Overall, for our test, we’ll leave most of the settings to their default options . It will be for you to discover them according to your testing (I discuss some interesting options at the end of the article)

Change all the same the following parameters:.

  • stream_localhost off allow the video stream to be accessible from the Internet and not only on your local network) – Note: you will still need to do your flow openings at your box ! Internet

  • stream_authentication : Sets a login and password and protect and vote video stream

  • webcontrol_localhost off

Normally, if you have done everything right, you just have to make a new motion restart the process and you should be able to connect to the following URL on your Raspberry Pi: 8081 / (replace with the IP on your internal network Raspberry Pi)


And then … what are the drama! Personally, I initially had some problems with the video stream since everything was gray. While yet when I performed of basic scripts to test the latter camera was functional (but not coupled with motion)

I found the answer to the following address.: by running the following command:

 sudo modprobe bcm2835-v4l2 

Note that this command must be run on every restart therefore insert it necessary in the crontab of the root account. To do this:

 sudo crontab -e 

Then add the following line:

 @reboot sudo modprobe bcm2835-v4l2 

Try now the motion sensor by simply typing motion and shake a little before the latter, several image files should now have created in your user folder ( / home / ft ) by default.


You even see the little red light on your camera light 🙂 …


now Reissue the motion command to start the process and observe:


once the revived process (and blah past), I waited a few moments and when I passed my hand over the sensor, the sensor has been triggered and immediately took a picture ( seen in the logs in the last 2-3 lines )

step 2 -. Start motion via Screen (in the background)

to operate permanently motion sensor (not just in your user session when you are connected), I recommend you use the screen (to install).

 sudo apt-get install screen 

the latter allows you to launch a process background (even if you are not logged in SSH).

to do this, type screen and execute motion . After starting the process, detach the console with the keyboard shortened CTRL + A + D . Log out. Once reconnected, to return to this console type screen -r . To complete a screen, simply type Exit

Step 3 -. To go further

We managed to use our camera coupled with our motion detector and we able to automatically generate images when an event was detected.

It is now for you to push a little further tests including watching the various parameters that are available with the motion package (see configuration file )

I think of the following settings that might interest you.

  • width & height : Sets the height and width of the image / video that will be generated

  • pre_capture & post_capture : to configure automatic support files images / videos before and after detection

  • quality : to set the compression level of your jpeg file

  • location_motion_mode & locate_motion_style to draw a square around it which was detected

  • text_right & text_left : to customize the messages that are automatically added to your files

  • on_event_start & on_event_end & on_picture_save to set performing actions or scripts when an event is detected – this is where you will be able to automatically request the execution of your batch scripts or python when someting is detected! 😉

A little reading:

No comments:

Powered by Blogger.