Introduction: Fully-featured Outdoor Security Camera Based on Raspberry Pi

If you had disappointing experiences with cheap webcams, their poorly written software and/or inadequate hardware, you can easily build a semi-professional webcam with a Raspberry Pi and a few other electronic components easy to find on which running PiWebcam, a free and dummy-proof piece of software which turns with just one click your device in a powerful and fully-featured webcam.

Step 1: Inspiration

After fighting against the limited software of most of the cheap security cameras on the market (e.g. poor night vision, obscure app for the configuration, no offline recording, inaccurate motion detection, etc.), I decided to build something by myself and Raspberry Pi looked to me the most suitable platform.

Even if there are already around a good number of projects for using a Raspberry Pi as a webcam I personally find them too complex and generally speaking more ad-hoc solutions for advanced users rather than finite products.

Nevertheless, most of them focus only on the software rather than the hardware, which is equally important for the security camera use case.

Step 2: Hardware

For building an indoor webcam, a simple Raspberry Pi (any model) and an attached camera (any model) with IR LEDs for night vision would work just fine. There are already plenty of kits available with this combination so if this is what you want to achieve, buy one of those and skip to step 12.

The same hardware though would not fit for an outdoor camera: the picture taken from the IR-capable Raspberry camera outside your house would look mostly pinky (due to the infrared light being captured by the camera) and with the small out-of-the-box IR LEDs you would not be able to see anything past 3 feet / 1 meter.

To solve the first problem, we need something called mechanical IR CUT filterwhich basically gives you back the true colors in day light but still allowing to capture the IR lights during the night. Most of the devices in the market have two wires: one short pulse on one wire will move the IR filter in front of the sensor (day mode), one short pulse on the other wire will remove the filter (night mode). They usually operate between 3v and 9v and if attached to our Raspberry, we can have full control over when to toggle night mode. However, the IR Cut filter cannot be controlled directly from a pin of the Raspberry since the mechanical part inside it requires much more current than the one the Pi can supply. We will work around it by using a H-Bridge powered by the Raspberry's 5v and controlled by two pins.

To address the second problem, we would need a more powerful IR LEDs board to achieve a decent night vision. Boards with fewer but bigger leds are preferred over those with a lot of tiny leds. Most of the boards in the market have also a LDR (Light Dependent Resistor) attached which is used to determine when to turn the LEDs on if dark. They usually operate at 12v and have a small plug (labelled "IRC") that can be used to connect an IR cut filter. However, no pulse is sent out directly through this plug but during the night (LEDs on), a (usually) 5v voltage drop between both the wires and ground is created. If attaching one of the wires to our Raspberry and monitoring the signal of the pin, we can determine if we are entering or leaving night mode (which is exactly what PiWebcam does)

One last thing to consider regarding the hardware is how to power the Raspberry Pi. Since we have a 12v power supply and we need 5v to feed the Pi, a voltage regulator is needed.

Step 3: Software

The idea behind PiWebcam was to provide a powerful imaging platform for everyone, regardless of his/her previous knowledge. An installation script will take care of fully configuring the system with reasonable default settings, letting the user customizing through a clean and mobile-friendly web interface only a very limited number of relevant parameters. Nevertheless, thanks to its powerful motion detection feature augmented by object recognition capabilities powered by an artificial intelligence model, PiWebcam can notify the user of any detected motion by sending a snapshot to an e-mail recipient or on posting it the user's favorite Slack channel.

Step 4: Bill of Materials

The following bill of materials is for the outdoor webcam built in this tutorial:

Step 5: Preparing the Components

The buck converter (voltage regulator) is responsible for converting the 12v power supply to the 5v the Raspberry Pi requires. Most of the components in the market are adjustable (e.g. you can change the output voltage by turning a screw). Since inside the webcam the screw might be accidentally moved, to ensure a fixed and constant 5v output put some tin in the 5v slot to solder together the two edges and cut the wire on the PCB (with a knife) which goes into "ADJ" (top left of the picture)

Since we want to have full control over the IR Cut filter through the Raspberry (whether or not the filter is embedded with the camera like in the picture), we need to get rid of the small connector. Cut the two wires and connect a female dupont cable for each wire. Do not throw away the small plug since we need to use it to receive the status of the LDR mounted on the IR Led Board. Connect another female dupont cable on one of the two wires (doesn't matter which one).

Step 6: Connect to IR Led Board to the Power Supply

Let's start by connecting the 12v power supply input entering our bare camera housing to the components.

Connect to the negative (black) wire the following:

  • Negative wire of the IR Led board
  • Negative wire of the buck converter
  • Negative wire to the USB male connector

Connect to the positive (red) wire the following:

  • The positive (12v) wire of the IR Led board
  • The Vin wire of the buck converter

Step 7: Power the Raspberry Pi

Connect the Vout wire of the buck converter to the USB plug which will powers the Raspberry.

After connecting all the wires, solder them together or just fix tight them with some insulating tape.

Step 8: Connect the IR Cut Filter

Since the IR Cut filter cannot be controlled directly from a pin of the Raspberry we will use a H-Bridge powered by the Raspberry's 5v pin and controlled by two pins.

  • Connect pin 4 (5v) of the raspberry to "+" of the H-Bridge
  • Connect pin 5 (GND) of the raspberry to "-" of the H-Bridge
  • Connect pin 39 (BCM 20) of the raspberry to INT1 of the H-Bridge
  • Connect pin 36 (BCM 16) of the raspberry to INT2 of the H-Bridge
  • Connect the two wires of the IR Cut Filter to MOTOR1 and MOTOR2 or the H-Bridge

In this way, when a pulse will be sent through e.g. pin 39, 5v will be supplied to MOTOR1 making the filter toggling.

Step 9: Connect the IR Led Board to the Raspberry

To know when it is getting dark we are leveraging the LDR mounted on the IR LEDs board. Use the small plug cut out of the IR filter in the previous steps, connect one side to the connector labelled "IRC" of the IR LEDs board and the other to pin 40 (BCM 21) of the Raspberry.

Step 10: Mount the Camera on the IR Led Board

Fix the camera on the dedicated slot of the IR LEDs board with an insulating tape or other means. Things to take into consideration at this stage:

  • The IR LEDs board gets very hot when on so protect the camera accordingly;
  • Ensure no IR light can enter the slot where the camera is located; IR light reflection is one of the most common reason why night vision is poor (blurred);
  • Ensure there is some space left between the lens and the glass of the camera housing otherwise reflection or image distortion could take place;

Do NOT close the camera housing yet :-)

Step 11: Option 1 - Flash a Pre-configured PiWebcam Image (recommended)

Step 12: Option 2 - Build a PiWebcam Image

Building a PiWebcam image requires a fresh installation of Raspbian and a SD card. Please do not re-use an existing installation but start from scratch:

Step 13: Option 2 - Copy PiWebcam to the SD Card

Downloadthe latest release of PiWebcam (PiWebcam_vX.X.zip), extract and copy the "PiWebcam" directory into the boot partition.

For a headless setup place into the boot partition also an empty file called "ssh" and a "wpa_supplicant.conf" with your network configuration. In this way the Raspberry will start connecting to your WiFi network upon startup and you would not need the HDMI cable at all but can connect directly through SSH to it.

Step 14: Option 2 - Power on the Raspberry and Connect to It

Plug the SD card on your Raspberry Pi, power it on and with a SSH client (or PuTTY on Windows) connect to it:

  • Hostname: raspberrypi.local
  • Username: pi
  • Password: raspberry

Step 15: Option 2 - Configure the System for PiWebcam

After ensuring the Raspberry is connected to Internet run the following command:

sudo /boot/PiWebcam/PiWebcam.sh install

This will fully configure the system and install the required dependencies.

At the end of the installation you will be requested to reboot the device to make the changes fully effective.
All the credentials will be summarized on the screen.

Please note the last 6 characters are random (e.g. PiWebcam-e533fe) and vary from device to device.

Step 16: Post-installation Tasks - Connect to PiWebcam's WiFi Access Point

Once powered on, the device will start acting as an Access Point.

Connect to the WiFi network created by the device. The passphrase of the network as well as the password of the admin user (for both the web interface and SSH) is the same as the SSID (e.g. PiWebcam-XXXXX). Point your browser to http://PiWebcam.local and authenticated with username "admin" and with password the same as the name of the network.

Step 17: Connect the Webcam to Your WiFi Network

I you want to connect the webcam to an existing WiFi network, go to Device / Network, select "WiFi Client" and fill in your "WiFi Network" and "Passphrase".

Wait for 1-2 minutes, connect back to your network and point your browser to http://camera_name.your_network (e.g. http://PiWebcam-e533fe.local)

Step 18: Close the Webcam Case

Once tested that the webcam can be reached through the network and performed the basic configuration presented in the previous step, it is now time to close the case.

Step 19: Getting Started With PiWebcam

PiWebcam already comes with reasonable default settings. Once installed, no additional configuration is required; PiWebcam will start taking snapshots and record videos, whether is connected or not to the network.

The entire device configuration (camera, network, notification and system settings) can be performed through the web interface. The configuration file can be easily exported and imported under Device / System.

When a motion is detected, PiWebcam will start recording a video (which will then be made available through the "Playback" menu of the web interface). Once there will be no more motion, a picture highlighting with a red box the detected motion will be stored as well. If the object detection feature is enabled, any motion not containing the configured object will be ignored so to lower down false positives (e.g. if a motion is detected but no person is identified).

When notifications are enabled, the snapshot will be sent to the user's e-mail address and/or posted on the configured Slack channel. If an Internet connection is not available, the notification will be queued and released when the connection is restored next.

A detailed summary of all the available settings is reported in the project page.

Step 20: Remote Internet Access

Optionally the web interface can be reached from the Internet without any additional configuration in your network or home router. To enable this functionality, check the appropriate box under Device / Network.

If remote Internet access is enabled, the device initiates a SSH tunnel through serveo.net, without the need to configure any NAT or UPnP in your router. The device name is used as hostname and both the web and ssh services are exposed.

Step 21: Technical Details

All PiWebcam files reside in the boot partition of the SD card, in a directory called PiWebcam. This includes a single bash file, PiWebcam.sh and the PHP pages for the admin panel.

During the installation process, a very basic system configuration is performed, an initramfs image is created and the PiWebcam.sh script is added to /etc/rc.local so to be executed at startup with the "configure" parameter.

At the first reboot, the initramfs image will shrink the root partition (previously expanded to fill the entire SD card by the Raspbian installer) and create a data partition just after.

Both the boot and root filesystems are mounted read-only and an overlay filesystem is created by the initram image upon the root filesystem so that any change to the system is stored in memory only and get lost at the next reboot. In this way the device will be more robust to misconfigurations, can be easily restored to factory defaults and can survive to any power outage since no system file is ever written to the SD card during normal operations. The data filesystem is instead formatted with F2FS (Flash-Friendly File System) which takes into account the characteristics of flash memory-based storage devices.

During startup, PiWebcam reads its configuration file stored at /boot/PiWebcam/PiWebcam.conf, configure the system, the camera, the network and the notifications based on the settings found there and deploy the web interface from /boot/PiWebcam/web into the web root location.

Both motion pictures and movies are stored into the data filesystem and grouped in folders by year/month/day/hour so to allow an easier access. All the recordings can be reviewed through the web interface with h5ai a modern file indexer which allows files and directories to be displayed in a appealing way and providing picture and video previews without the need to download the content beforehand.

When a motion is detected, PiWebcam.sh is invoked with the "notify" parameter through the on_picture_save/on_movie_end motion's event. If object detection is enabled for further analyzing the image, the picture is sent to Clarifai to recognize all the objects within the image. This would work great to lower down false positives e.g. if you are interested to know if there is somebody stealing in your house and not just a sudden light change.

After that, PiWebcam check if an Internet connection is available and if so, sends out the notification. In addition to traditional e-mail notifications, sent out with ssmtp, with the detected motion picture attached, PiWebcam can also upload the same picture to a Slack channel. If you don't know Slack, check it out (); it is a great collaboration tool but can also be used to create a group dedicated to your family, grant access to your family members, chat with them and allow PiWebcam or Home Automation utilities (like e.g. eGeoffrey) to post updates over there. If there is no Internet connection, the notification does not get lost but it is queued and sent out when the connection is restored.

An upgrade functionality is provided through the web interface as well.