Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building a Home Security System with Raspberry Pi

You're reading from  Building a Home Security System with Raspberry Pi

Product type Book
Published in Dec 2015
Publisher
ISBN-13 9781782175278
Pages 190 pages
Edition 1st Edition
Languages
Author (1):
Matthew Poole Matthew Poole
Profile icon Matthew Poole

Table of Contents (16) Chapters

Building a Home Security System with Raspberry Pi
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Setting Up Your Raspberry Pi Connecting Things to Your Pi with GPIO Extending Your Pi to Connect More Things Adding a Magnetic Contact Sensor Adding a Passive Infrared Motion Sensor Adding Cameras to Our Security System Building a Web-Based Control Panel A Miscellany of Things Putting It All Together Index

Chapter 4. Adding a Magnetic Contact Sensor

Now that we have built our port expander hardware, we need to learn how to program it so that our Raspberry Pi can detect the things that we connect to it as part of our home security system. We will begin by connecting switches to our system in the form of magnetic sensors—the most common component used in home security systems to detect intrusions through doors and windows.

In this chapter we will cover the following topics:

  • Learning about reed switches and how they work as door sensors

  • Enabling and setting up the I2C bus on the Raspberry Pi

  • Connecting our sensor to an input on our port expander

  • Learning how to access our I2C port expander from a Bash script

  • Writing a script that will detect the state of our door sensor

  • Looking at other types of contact sensors that can be connected and programmed in the same way

Prerequisites


You'll need the following parts for the exercises in this chapter:

  • Our Raspberry Pi and Port Expander board

  • 8 x 10K ohm resistors

  • A magnetic door sensor and magnet

  • A hook-up wire

  • A 4-core alarm wire

The working of magnetic contact sensors


A reed switch is essentially what makes up our magnetic contact sensor. A reed switch comprises two metal contacts made of magnetic material (called reeds) placed inside a glass envelope. When the contacts touch, the switch is on, and when they spring apart, the switch is off and the circuit is broken. The way to control these contacts is by means of a magnetic field that makes or breaks the circuit when it is near to the switch.

A normally open (NO) type of reed switch is normally switched off until a magnet comes close to the switch, which then pulls the contacts together.

A normally closed (NC) variety works the other way with the switch being normally on until the magnet comes close to the switch, pulling the two contacts apart.

A typical type of reed switch

You can now see how a magnetic reed switch can be a useful sensor in security applications, and in particular for our home security system, to detect when doors and windows are opened and closed...

Setting up the I2C port expander


Now that we have built our port expander, we need to get it ready to connect our sensors to. First, we need to install the tools on the Raspberry Pi to allow us to use the I2C bus and program devices connected to it, including the MCP23017 chip that makes up our port expander.

Note

Don't connect your port expander to the Raspberry Pi until after you've set up the I2C bus on your system.

Enabling the I2C Bus

It's highly likely that the module for using the I2C bus hasn't been loaded by default. Fortunately, doing this is fairly straightforward and can be done using the Raspberry Pi configuration tool. Perform the following steps:

  1. Launch the Raspberry Pi configuration tool with the following command:

    $ sudo raspi-config
    
  2. Select option 8: Advanced Options.

  3. Select Option A7: I2C.

  4. Select <Yes>.

  5. Reboot your Raspberry Pi for the setting to take effect.

Now that the I2C bus has been enabled, we need to set up the operating system so that the required modules are loaded...

Connecting our magnetic contact sensor


Now that we've got our port expander working with the Raspberry Pi, we can start connecting things to it and create the scripts that will monitor the sensors on the input pins.

Let's go back to our port expander stripboard that was built in the previous chapter and connect our magnetic sensor. But first, we need to ensure that all of our inputs are pulled low by default using 10Kohm resistors. This prevents them from being in a floating state and giving us spurious data when we read the port's data.

Note

In the following diagram, I've connected the pull-down resistors externally, but you may want to include them directly on the stripboard. Toward the end of this book, we'll have a new board layout that brings everything that we've been prototyping so far together in a single solution.

To check the port's input value, we use the i2cget command:

$ sudo i2cget –y 1 0x20 0x12

This should return 0x00, which means all inputs are off (binary %00000000).

Note

What...

Monitoring the sensor


Now that we have everything in place and our magnetic sensor is detecting whether the door is closed, we can monitor this sensor with a simple Bash script that uses the I2C tool commands that we installed earlier.

The code listing for poll-magnetic-switch.sh is as follows:

#!/bin/bash
sudo i2cset –y 1 0x20 0x00 0xFF

# loop forever
while true
do
  # read the sensor state
  SWITCH=$(sudo i2cget –y 1 0x20 0x12)

  if [ $SWITCH == "0x01" ]
  then
    #contact closed so wait for a second
    echo "The door is closed!"
    sleep 1
  else
    #contact was opened
    echo "The door is open!"
  fi
done

When you run the script and then push the button, you should see "The door is open!" scrolling up the console screen until you stop pressing it.

By combining this with our elaborate light switch project in chapter 2, we can switch on the LED connected to GPIO17 when the door is opened:

#!/bin/bash

#set up the LED GPIO pin
sudo echo 17 > /sys/class/gpio/export
sudo echo out >...

Anti-tamper circuits


If you take a closer look at our system, you might realize that depending on whether you are detecting normally open or normally closed sensor switches, it is possible to tamper with the sensor channel by simply cutting the wire. So, in the case of a normally open switch, it wouldn't activate the monitoring system if the wires were cut, as it would always appear to be open, even if the switch was closed.

To mitigate this, most alarm systems feature a 4-core wiring system to connect the sensor devices to the main control board—two cores are used to connect the sensor and two are used to create an anti-tamper loop, which then itself forms a sensor input for monitoring.

4-core alarm cable

Take a look at the following circuit so that you see what I mean:

In this circuit, we have two sensors: one for monitoring a window and one for monitoring a door. These are connected to the I/O BUS A inputs, 0 and 1 (or GPA0 and GPA1, as we like to call them). As before, they are pulled down...

Getting into the zone


It may have occurred to you by now that even a modest-sized property could require plenty of door and window sensors; thus, if we used one input for each sensor, we'd soon run out unless we put more and more port expanders onto the system. The same is true for commercially available security systems.

So, the way this is dealt with is by creating zones, with each zone containing a group of sensors. A bedroom, for example, may be defined as one zone with a window sensor, a door sensor, and movement detector forming that zone. In this scenario, each sensor is connected to the next in a series (or daisy-chained); if one of them triggers, it will alert the monitoring system that there was a trigger in the zone. Obviously, though, it may not necessarily be the actual detector, which in most applications isn't really an issue.

However, this can introduce some challenges when we're considering mixing normally open and normally closed type sensors within a zone, but this is something...

Summary


In this chapter, we got our I2C-based port expander configured and working, and we experimented with it by connecting a magnetic sensor—one of the most commonly used sensors in security systems. We've also learned how to interact with I2C devices using Bash scripts, and how to read and write data to and from these devices.

In addition, we should now be beginning to understand the various elements and building blocks of a security system, including anti-tamper loops and zones. These are concepts that will prepare us for later on in the book, when we start to piece all of this together and build our final, all-encompassing system.

In the next chapter, we will look at passive infra-red motion detectors, how they work, and how we can connect the wired and wireless types to our home security system. We'll also learn how to create log files based on events using Bash scripts so that we can maintain a history of detector states as they change.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building a Home Security System with Raspberry Pi
Published in: Dec 2015 Publisher: ISBN-13: 9781782175278
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}