Tutorials
August 12, 2024

Build your own security camera system: A step-by-step guide

Written by
Matt Vella
Developer Relations

Create a DIY security camera system that allows for highly customizable detections, triggers, and actions without coding for each scenario.

I have a basic security camera that I don’t use. Solutions like Ring and Blink lack the features I need, and I dislike the idea of live video streaming to the cloud and paying additional monthly fees.

So, as an engineer, I did what I do best: I designed a security camera solution tailored specifically to my own needs. And you can too!

Whether you’re looking to build your own security camera system that notifies you when a UPS delivery arrives, automatically lets your kids in when the camera recognizes their faces, or alerts you when children are unattended in a pool area, this Raspberry Pi security project is for you.

In this step-by-step guide, you’ll learn how to:

  1. Determine your DIY security camera's goals
  2. Gather the hardware for your DIY security camera
  3. Install viam-server to connect your DIY security camera
  4. Add capabilities to your DIY security camera
  5. Add triggers for your DIY security camera
  6. Build a mobile app to monitor your DIY security camera 
  7. Plan to enhance your DIY security camera

Now, let’s get building!

Step 1: Determine your DIY security camera's goals

Before you begin your build, stop and think, "What would make a security camera useful for me?" And get specific!

Whether it's monitoring package deliveries at your front door, keeping an eye on your backyard, or ensuring 24/7 surveillance of your garage, this clarity will guide your project and ensure that your efforts align with your needs.

If you're unsure how specific to get, check out my list—it's a bit unusual, but here it is:

This image gives context to the requirements of his DIY security project. An image of ducks is shown alongside the text "protect our ducks," a picture of a dog is shown alongside the text "monitor our dog Luna" and a picture of a cat is shown alongside the text "deter boo, the neighbor's cat."
My requirements when building my own security camera.
  • Protecting our ducks: We have egg-laying backyard ducks that have been attacked by bears and foxes at night. I’d like to set up a camera specifically aimed at their coop to detect and alert me when any wildlife comes within a certain distance.
  • Monitoring our dog Luna: Our dog, Luna, is a “southern girl” hailing from Tennessee and, after 5 years, still dislikes being outside in the cold northern winters. I’d love a camera by the back door that alerts us when Luna is waiting to come inside before she gets too cold.
  • Detering Boo, the neighbor’s cat: Our neighbor’s big, bad Maine Coon cat, Boo, loves to come to our porch to taunt our indoor cat, saying “A pity you can’t be out in the glory of nature with me,” by staring through the glass. If a camera could detect Boo’s presence on our porch and trigger a deterrent, like a sound or light, to scare him away, that would be nice.

You're unlikely to have the same security camera needs as I do, but I'd bet your list isn't fully covered by standard motion and person detection features on today's cameras

Step 2: Gather the hardware for your DIY security project

For this project, you’ll need: 

Choose the right camera type

When selecting a camera, make sure it has specific features that ensure it operates correctly in different environments. Select a security camera that’s waterproof, has night vision, and has a two-way speaker system.

For this tutorial, we’ll be using an inexpensive RTSP IP camera.

A close-up image of a security camera.
Building your own security camera system requires selecting the right type of security camera.

Choose the best Raspberry Pi for your security camera

For this project, we’ll be using a Raspberry Pi 5

Our team found it has twice the performance of a Raspberry Pi 4, making it the perfect choice for a project featuring object detection and object recognition.

Image of the Raspberry Pi 4 with a SD card in frame, on top of a computer keyboard.
An image of the Raspberry Pi 4, which, while reliable, has less performance than the Raspberry Pi 5.

Step 3: Install viam-server to connect your DIY security camera

To start setting up your project:

  • Install viam-server on the computer of your choice.
  • Head over to the Viam App, name your new machine (whatever you’d like), and add a new machine instance. For this build, I called mine “smart-cameras."
A close-up image of the Viam app interface where the box labeled new machine is filled in with smart-camera.
Creating the new machine within the Viam app interface. 

Step 4: Add capabilities to your DIY security camera

Capabilities fall into two categories: 

  • Those that are core to the project (like the camera).
  • Those that are contextual to our use cases (like the specific ML models you want to use). 

These can be added to or changed over time without writing new code.

Add the camera to your Raspberry Pi security project

One camera is essential to the project, but we could add any number of cameras. 

If you are using a webcam, like the one on your laptop, this is as simple as:

  1. Choosing the model ‘webcam.’
  2. Selecting the camera from the component dropdown.
  3. Saving your configuration.

If you are using another type of camera, configuration is the same process, though you would choose a different model and might need to add some other attributes.

I configured my RTSP camera using these instructions found in our documentation.

An image of the Viam app interface, Showing the built-in camera components—from webcams to ultrasonic to transform cameras—found within Viam’s app.
Showing the built-in camera components found within Viam’s app, allowing for no code configurations.

Add machine learning (ML) models to your Raspberry Pi security project

Deploy your own ML model to your Raspberry Pi security project

With Viam, you can easily upload your custom ML models to the Viam Registry for use on your smart devices. Our platform supports TensorFlow Lite, TensorFlow, PyTorch, and ONNX frameworks. 

To upload your model, simply follow these step-by-step instructions.

Train your own ML model

You can also use Viam to train your own custom ML model on images captured by your smart machine. Our platform allows for the training of Single Label Classification Models, Multi-Label Classification Models, or Object Detection Models. 

To train your own ML model, follow these step-by-step instructions.

A video showing an object detection model being created within the Viam app. A person is titling the model "pet-feeder," selecting single label classification, and the starting to train the model.
A single label classification model being created within the Viam app.
Use a pre-trained ML model for your Raspberry Pi security project

For my project’s purposes — detecting wildlife and pets around my premises — I decided to use some pre-trained ML models to work alongside Viam’s computer vision service. 

Just as you’re able to import your own ML models into our app, you’re also able to add others from other repositories, like HuggingFace, Model Zoo, or Kaggle.

To upload your model, simply follow these step-by-step instructions.

With that, I was able to add models for:

A view of the Mobilenet wildlife detector in action, with a fox being identified as an animal with 92% accuracy.
The Mobilenet wildlife detector in action.
  • An Efficientnet detector that has been trained on the popular COCO dataset. I’ll use the “dog” label to detect when my dog, Luna, is waiting outside my back door and the “cat” label to detect when my neighbor’s cat, Boo, is on my porch.

Both of these models (and more, including YOLO models) can be found on the Viam Registry, a community-contributed repository of modules, and can be configured on your DIY security camera system in just a few clicks.

An image of the open-source Viam Registry, filtered for ML models, on a desktop screen. The title text reads "Viam Registry" with the sub-header reading "Explore community packages to extend the possibilities of Viam." The remainder of the screen shows some ML models that are available on the registry.
The open-source Viam Registry filtered for ML models. With this, you can quickly deploy an ML model to your machines.

Step 5: Add triggers for your DIY security camera

Configure the event manager and customize solutions

With our camera, ML models, and vision service in place, you're ready to start using them.

To get started, you can create highly customized solutions tailored to your specific use case using any of the Viam SDKs.

Alternatively, explore the Viam Registry to find modules that match your needs.

In this case, I wanted to create a solution that both I and others could use to:

  1. Monitor for configured "events" to trigger.some text
    • For instance, an event called "Poor Luna got left outside" that detects if a dog is seen by my back door camera.
  2. Act on configured notifications when events are triggered.some text
    • For example, when the "Poor Luna got left outside" event is triggered, an SMS message is sent to my phone.

Nothing on the Viam Registry met my needs, so I built it using the Viam Python SDK and uploaded it as a public module. The code is open source and available for review, but here’s a summary of the flow:

  1. An event loop starts.
  2. On each pass of the loop, every rule for each event is evaluated.
  3. If an event is triggered, the configured notifications are executed.
  4. A series of images, which can be viewed as a video clip, is captured in a folder with a timestamp and metadata.
  5. If an event is triggered, it won't trigger again for a configurable amount of time.
  6. Repeat.

The README for this module describes how to configure rules and notifications. Here’s the configuration I used for the “Poor Luna got left outside” scenario I mentioned.

{
    "mode": "home",
    "events": [
        {
            "name": "Poor Luna got left outside",
            "modes": ["home", "away"],
            "debounce_interval_secs": 300,
            "rule_logic_type": "AND",
            "notifications": [
                {
                    "type": "sms",
                    "carrier": "att",
                    "phone": "9175550100"
                }
            ],
            "rules": [
                {
                    "type": "detection",
                    "detector": "coco-detector",
                    "confidence_pct": 0.7,
                    "class_regex": "dog",                    
                    "cameras": ["backdoor-camera"]
                }            
]
        }
    ]
}

Hopefully, by now, you can see why modular capabilities are so flexible and useful. Without any coding, I added support for various cameras, machine learning, and computer vision resources.

Next, I wrote code to leverage these modular capabilities in a flexible, generic way. I then added this code to the Viam Registry, allowing anyone to use these advanced features without writing a single line of code.

Note: All the features I've demonstrated are open source, so you can use them without any coding. You can also contribute to the Viam Registry.

Step 6: Build a mobile app to monitor your DIY security camera 

Leveraging ML with my camera to get notifications is great, and it took me less than a day to set up. (Using my event manager, it can be done in minutes.)

But I also wanted:

  • The ability to view and manage triggered event video replays.
  • An intuitive interface to create and modify rules and notifications (I'd rather not be typing JSON into a textbox).

How can I turn my smartphone into a security camera?

I knew there was a Viam Flutter SDK, which enables you to build applications for mobile, web, and desktop, so I decided to challenge myself. 

I hadn't worked with Flutter before and didn't have much experience in mobile app development, but I knew the same underlying APIs would be there. This meant that working with cameras and computer vision detectors in Flutter would be pretty similar to using them in Python or TypeScript.

After taking some time to get the hang of Dart (the programming language Flutter uses — everything is a widget? OK.), I developed a mobile app called SAVCAM (Smart AI Viam Camera). 

An image of the event configuration for the Raspberry Pi security project, showing the event name as "Poor Luna got left outside," the rules, and where to send the notifications.
The event configuration for the security camera app.

Like any security camera app, you can view a live feed from any of your cameras, and you can see notifications and video replays of events. But unlike any security camera app I’ve seen, this Viam-based camera offers much more flexibility in terms of what it's looking for and how it reacts.

To build your own Flutter application that integrates with Viam, see the full instructions.

As you can see, whenever my dog is detected by the camera, it sends an alert through the app, allowing me to check the camera from a distance.

Step 7: Plan to enhance your DIY security camera

I have more ideas for taking my system further, especially around other types of notifications:

  • I’ve added IFTTT support to trigger actions like turning on lights when we arrive home at night, based on detecting a car or person, but not rain or snow.
  • To deter wild animals (including Boo), I might use an ESP32 with Viam’s micro-RDK to activate erratic buzzers and LEDs when they are detected.
  • A colleague recently developed a Viam module that controls Yale Smart locks. I’m considering using it to let my teenage kids in when they forget their keys.

The possibilities are endless—consider what other enhancements or integrations could make your DIY security camera system even more effective and tailored to your needs.

Maybe you can implement time-based rules to receive alerts only during specific times of the day, expand your system by adding more cameras across your property, or integrate with voice assistants like Alexa or Google Assistant for voice-controlled security management.

Start building your own security camera

We've covered the essentials of creating a highly customizable DIY security camera system, from gathering the necessary hardware to installing viam-server and adding advanced capabilities and triggers.

Now it’s time to bring your project to life.

With Viam, you can easily integrate hardware, add machine learning models, and set up advanced triggers to tailor your security system to your specific needs—all without extensive coding.

Ready to build your own DIY security camera? Sign up for our free app, explore its many features, and join our community to get direct support from our engineers.

On this page

Get started with Viam today!