PiCamera & PIR Movement Detector – a surveillance system



From the previous articles, you saw how to individually use the PiCamera webcam and the PIR Motion Detector through the Raspberry Pi board. In this article you will see how to combine the two to get a simple example of a surveillance system: When motion is detected in the room, a picture is taken to see who is the intruder.

I suggest you review the following items.

Installing the PIR Motion Detector

In this article, you connect the PIR motion detector to Raspberry board using a simpler scheme, ie without using the breadboard or the pull-up resistor. In fact you can connect all the wire jumpers directly to the GPIO pin as shown in the diagram below.


Once you have connected the PIR, you can start the Raspberry Pi. Then open a command session from the terminal and open the file shown in the article PIR motion detector.

For convenience, the code is shown below, putting in bold the code to be added if you want to work  without a breadboard and the resistance.

If you want to run programs in Python using the GPIO pins, remember to do it as sudo.

The program immediately begins to write “No Motion”. Each time the PIR sensor detects motion, you’ll get the message “Motion Detected!”.

Meccanismo Complesso - PIR motion raspberry 02

A phot for each movement detected

Now you will modify the above example in order to take a picture every time the PIR detects motion in the room. This way you can understand what will be the origin of the movement (the intruder in your room!)

Copy the old file and rename pir02.py.

Now make the necessary changes to integrate the use of the Pi room with the previous program (additions in bold).

First, you’ve added the PiCamera form to use the webcam. Then hai0 you set the reference variable to your PiCamera. When the PIR sensor detects motion, it activates the webcam Raspberry, waits for just a second to adjust brightness and then takes the shot. Then turn off the preview of the camera.

Also you need to handle the exception (for example, when I interrupt the program execution with CTRL + C) to prevent the webcam remains active even after the program ends.

Recording a surveillance video

In the previous example you saw how to take pictures with every movement detected. A more advanced way is to record a short video instead of a picture to record the moving subject and understand better what he is doing. To test I set a duration of five seconds, but you can increase this time.

Remember to run the program like sudo, because you’re using the GPIO pins.

To stop, press CTRL + C.

A shrewdness: turn off LED activation of PiCamera WebCam

If you really want to activate a surveillance camera to find out stealthily moves, perhaps it would be better to disable the LED that lights up whenever the web camera is activated. This is possible by entering the following command:

and adding at the end of the file, the following configuration line

Save, and then reboot the system to set the new configuration.


With this article you saw the foundation to develop programs to control a video surveillance webcam to be installed in a room. If there had been an inspiration for a project do not hesitate to post it on Meccanismo Complesso.

Leave a Reply

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