Building a Smart Home Security System

And a guide on how you can too!

Avantika Chaturvedi
students x students

--

Photo by Jozsef Hocza on Unsplash

When I first got the pair of keys to my house, I was ecstatic. I finally felt like a big girl, but that great feeling came with quite a lot of nervousness along the way. I mean 12 years old me had never really gotten to take on this type of responsibility before. My mom would constantly be reminding me “Don’t forget to lock the door Avantika”, “Remember to take the keys out of the lock.” Sounds pretty simple, I know but I would actually be really paranoid and worrying constantly about any sneaky intruders entering my house if I left the door unlocked accidentally.

But this fear wasn’t irrational from any stand point, because on average, a burglary happens once every 30 seconds. That adds up to two burglaries every minute and over 3,000 burglaries per day. Thankfully after 4 years, we’ve seen lots of progress. Even though those numbers are pretty similar, we don’t really worry too much about it (12 years old me would have loved that). Why is that? Well, it’s because our world is now filled with smart home security systems that make sure our houses aren’t going to be messed with.

We’ve grown quite accustomed to the Ring doorbell or Google Nest and both of those fall under the category of being a “smart security system”. Now I was pretty interested in how these work and a quick google search would probably give me a brief understanding, but to understand how these things really work I needed to get some hand-on experience. I did that building a smart home security system! By smart home security system, I mean that I will be able to track and get notified if there is any movement happening or lights going off around a part of my house.

STEP 1: The Hardware

To start this project off I needed to get all the necessary hardware, all of which are available on Amazon, or by buying Arduino’s Oplà IoT Kit (highly recommended!)

Hardware used:

  • Arduino MKR WiFi 1010
  • MKR IoT Carrier
  • Micro USB cable
  • PIR sensor

(OPTIONAL: If you want to make this project more portable and compact, you can place the MKR IoT Carrier in a plastic casing)

How the hardware set up should look like. (learn how to do this here)

💡Spotlight: PIR Sensor

Passive Infrared (PIR) sensors are electronic components used to detect movement. They measure the infrared light that is radiating off of any object that is in front of them. This changes the output voltage which the sensor can measure. This is achieved through the use of something called a Fresnel lens, which allows the sensor to detect a much wider range than just a bare sensor.

Fresnel lenses are actually the technology behind lighthouses, where light can be amplified and focused on a specific point. PIR sensors basically work the same but in the opposite direction.

Source

Now apart from the PIR sensor that will detect any movement & set off a movement alarm, we also have 2 other sensors that we’ll be using.

Light Sensor: Now depending on where you want to place your security system you may or may not need this sensor. The light sensor on the MKR Carrier is more-so recommended to be placed in darker spaces, such as cupboards, drawers, closets, or basements, since when it is exposed to light it sends a trigger.

IMU sensor: This sensor is also on the carrier and allows us to track the movement of the carrier itself. For example, if we mount the carrier on a door, and the door opens, the IMU’s values will change, and we can act upon it accordingly.

This Home Security Alarm is designed so that we can choose the type of alarm we want to use. We’ll make all three types of triggers, as well as an activating switch. We may, for example, turn on the light alarm but turn off the shake alarm, or vice versa. As a result, you’ll have a versatile alarm that can be changed to suit any situation.

STEP 2: Setting up our IoT cloud

Now, this step is pretty lengthy so I made a separate document detailing every step so you can find out how to set up the IoT cloud. You can find it here. But after you’re all done that, we can go to the Sketch tab and start completing the code!

STEP 3: The Code

All the code I had done was on Arduino’s Web Editor. After making an Arduino account you can access the web editor here.

The first step to our code is the initialization. The Arduino MKRIoTCarrier library will be used to start the initialization process. After that, we’ll set the pir to A5 and make pirState, which will be used to store the PIR sensor’s state. We’ll also define the light variable and make three floats to store the data from the IMU sensor: Gx, Gy, and Gz (gyroscope).

Next, we will create three empty strings, light_alarm_state, movement_alarm_state, and shake_alarm_state. These will later update the display according to whether the alarm is enabled or not (ON or OFF).

Finally, we will create two unsigned integers to store a color combination for our RGB LEDs. Simply, we will store the color red in the redColor integer, and no color (off) in the noColor variable. This way, we can simply use these whenever we want to turn all RGB LEDs ON or OFF.

#include “thingProperties.h”
#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;
int pir = A5;
int pirState = 0;
int light = 0;String light_alarm_state = “”;
String movement_alarm_state = “”;
String shake_alarm_state = “”;
float Gx, Gy, Gz;uint32_t redColor = carrier.leds.Color( 0, 255, 0);
uint32_t noColor = carrier.leds.Color( 0, 0, 0);

The Loop

Inside the loop(), we will first make reading on the sensors used in this project. Here we will retrieve the value of, the state of pirState (high or low), and the value of Gx, Gy, and Gz.

For the next part, we need to create three main conditional statements, that can be activated through the cloud. These will basically unlock another condition, that will check whether the state or value of a sensor is over the threshold (trigger value). If it is, it will then unlock another conditional, that updates the message_update string, and calls on the alarm() function. If an alarm would be triggered, the only way to turn it off is through the Arduino IoT Cloud dashboard, which we will create later.

Functions

In this program, we will have several functions that we will call from, both from within the code but also from the Arduino IoT Cloud.

But let’s begin with alarm(). This function is very basic: every 0.5 seconds, the RGB LEDs turn red and the buzzer makes a sound, and then it switches off. If this function is continuously called upon, it will result in a blinking red light and a beeping sound.

void alarm() {
carrier.leds.fill(redColor, 0, 5);
carrier.leds.show();
carrier.Buzzer.sound(500);
delay(500);

carrier.leds.fill(noColor, 0, 5);
carrier.leds.show();
carrier.Buzzer.noSound();
delay(500);
}

We will then write similar lines of code for both our shake alarm and our light alarm.

Note: I chose to make this project wireless, to do so make sure you remove the line while (!Serial);, that is located in void setup()

FULL CODE AVAILABLE HERE.

STEP 3: The Data

Currently, when you run your code you’ll notice that nothing is really happening. This is because we still have a really important step left! We now have to visualize our data and get notified. To do this, we can navigate to the Dashboards tab. Here we need to create a new dashboard by clicking the Build Dashboard button, which will open a new window with an empty dashboard. Once we have a new dashboard, we will need to create 10 widgets that we will link to the variables that we created earlier when we configured our IoT cloud. We can follow the table below to create these widgets:

After setting all of those up your dashboard should now look something like this:

And you can now control all of your alarms and get a message sent to you if one of them has been breached/there is an alert!

That’s it! With this project we can see first hand how smart home security systems work and made our own.

Let’s Connect!

Feel free to contact me at avantikachat@gmail.com, or connect with me on my Linkedin. If you would like to receive monthly updates about what I’ve been up to and my future articles subscribe to my newsletter here!

If you liked the is article be sure to check out the one I wrote explaining IoT on a deeper level here.

Feel like you’re about to jump into a rabbit-hole of reading these incredible articles?
Don’t worry, we feel the same way.
Not only can you jump into the rabbit hole with us, but we’ve got more than enough articles that’ll help you jump out ;)
For some of the best ideas on Medium from the youngest minds of the generation, visit students x students.

--

--