YouTube player

I’m always on the lookout for those teachable moments. Inspiration struck me when my 5-year-old asked for help in keeping his little brother from sneaking into his room. I spotted the perfect time to teach him about inputs, outputs, and programming. Learning is easier when the lesson centers on solving a personal problem.

Alarm System Block Diagram

I could have built a simple alarm system any number of different ways, but I knew that I wanted the system to be an all-in-one unit for versatility beyond my son’s original request. I chose the Raspberry Pi as the controller because it is easy to connect to the internet, it can play MP3 files, it interfaces with USB peripherals like cameras, and it has general purpose inputs and outputs for connecting to simple electronic components like buttons, sensors, and lights.

My alarm system’s code, which you can view on GitHub, is rather simple. To take advantage of Linux’s multithreading, I split the tasks between two Python scripts. One python script ( monitors the keypad for valid codes. Upon boot, the system is set up as “disarmed”. Anytime the arm/disarm code is detected, it toggles the status of the system by flipping a byte in a text file called “armed.txt”. Flowchart

The second Python script ( monitors the passive infrared sensor via the Raspberry Pi’s GPIO. If motion is detected, it checks the armed.txt file to see if the system is armed. If the system is armed when motion is detected, the alarm is sounded. Flowchart

I built this alarm system to fit my own needs. Following are the step-by-step instructions for building one like mine. When you build yours, be sure to customize it to fit your own needs, maybe by adding a laser tripwire or a cellular radio.

Project Steps

Setup Raspberry Pi

Setting up a Raspberry Pi from scratch is beyond the scope of this project, but fear not, help awaits. You can find web tutorials but I recommend you read the book “Getting Started With Raspberry Pi” by Matt Richardson and Shawn Wallace. This book will help you not just with the PiLarm project, it will also help you discover the capabilities of the Raspberry Pi, so you can combine its various powers into your own new and interesting creations.

  • Thus guided, connect your Pi to the internet so you can download the libraries and code needed for the following steps, and so the Pi can tweet and email its intruder pictures.
  • Install GPIO

    This library will let you control the Pi’s GPIO pins to use the passive infrared sensor and the keypad as inputs and use the revolving light as an output. Use the following three terminal commands to install it:

    sudo apt-get update
    sudo apt-get install python-dev
    sudo apt-get install python-rpi.gpio

    Install fswebcam

    If you are using a third party webcam like the Playstation Eye, fswebcam will let you use it to take still photos. If you use the raspberry Pi camera, look at their instructions for installation instead. Use the command:

    sudo apt-get install fswebcam

    You can take a picture manually with the command:

    fswebcam -r 640×480 -d /dev/video0 testpicture.jpg

    Then use the “ls” command to see if your picture appeared in the working directory.

    Install mgp123

    mpg123 is a command line audio player that uses to play audio files such as, “System armed,” and “Motion detected. Please enter passcode.”

    sudo apt-get install mpg123

    Add PiLarm Code

    Now add the code that I created to perform the PiLarm functions. It is located at:

  • To put the code on a headless Pi (no monitor, SSHing in over the network), you can use git. The Pi doesn’t come with git, but you can install it:
    sudo apt-get install git-core

    And then use the “clone” command to download it to your Pi:

    git clone

    You’ll now have a directory called PiLarm containing the python files and audio files for the PiLarm.

  • Set and to run on boot by adding the following lines to /etc/rc.local just before the line that reads “exit 0”.
    python /home/pi/Alarm/ &
    python /home/pi/Alarm/ &
  • Familiarize yourself with the code. Compare it to the flowcharts above and see how the statements in the code work.
  • Line 96 in contains the passcode (1912) used to arm and disarm the system. Line 97 is the haltcode (5764), used to gracefully shutdown the Pi and prepare it to be unplugged.
  • If you are using the Raspberry Pi Camera Module, refer back to their instructions to see what commands you will need to put into in place of the fswebcam commands.

  • Setup Twitter

    Set up an “App” to get a Twitter API key:

  • For the “Access Level,” choose “Read and Write”. This will give you the “Consumer Key,” “Consumer Secret,” “Access Token,” and “Access Secret” that you will plug in as values for the function that creates the TweetPony API object (line 10 in, which currently reads “api = tweetpony.API(consumer_key = “abcd”, consumer_secret = “efgh”, access_token = “ijkl”, access_token_secret = “mnop”)”).

  • Solder Up Perma-Proto

    The Perma-Proto from Adafruit is great because it comes with a connector for the Pi’s GPIO. The pins are labeled on the board by the connector, which makes connecting things easy.

  • Connect the Pi’s 3.3V power pin to the Perma-Proto’s positive power rail, and the Pi’s ground pin to the Perma-Proto’s ground rail. Then its just a matter of neatly and carefully routing wires to get everything connected. I use male and female headers as connectors for things like the passive infrared sensor, but you can solder them directly if you like.
  • Notice that the speaker amplifier is on the Perma-Proto to get power from its power rail, but it is not connected to the Pi through the GPIO header. The audio cable connects the amp separately, and the speaker connects to the amp’s screw terminals.
  • The matrix keypad has a rather short attached cable, so be sure to plan your layout before soldering.

  • Full System Test

    You may be eager to get things into a box and finish the project, but you should test the components before mounting them in a cramped enclosure that will make troubleshooting difficult.

  • If your revolving light gives you trouble, it may be a polarity issue. I tested mine on a breadboard first, manually connecting the TIP120 to power or ground to see how to correctly connect it.
  • If you have trouble with the code, it may be because something is connected to the wrong pin, or because you forgot to set up a constant such as the Twitter keys. To troubleshoot the code, use Python’s “print” command to print the contents of variables to the console or to mark events in the code as having executed. To see the console for or, you’ll need to SSH into your Pi and run them yourself with a “sudo python” or “sudo python” command.

  • Mount in Enclosure

    Most alarm systems are installed as permanent fixtures, but I designed this one to be portable for added versatility and ease of repurposing. I chose the smallest box that would fit everything.

  • The Raspberry Pi Camera Module wasn’t available when I built the first PiLarm, but they are easy to come by now. If you use this module, figure out how you are going to mount it. If you mount it to the alarm system enclosure by just drilling a hole for the lens, you may limit aiming the camera. Consider either mounting the camera module on a swivel or making the whole enclosure aim-able with a small tripod.
  • If you use a USB webcam, just stick it to the box with double-sided tape, preferably covering most of the cable hole.
  • The box works better upside down, so treat the lid as the bottom.
  • Before mounting anything, drill holes for the Pi’s standoffs, the Perma-Proto’s standoffs, the speaker grill, LEDs, PIR, USB jack, webcam cable, and revolving light wires. You don’t want to get shavings in a connector or accidentally drill through something expensive.
  • For the keypad cable, drill a row of 1/8″ holes right next to each other. Use a utility knife to cut out the material between, creating a slot.
  • Double-sided tape will hold the camera and revolving light in place. The keypad has its own adhesive. The speaker bolts to the case while the Perma-Proto and Pi use the circuit board stand-offs.
  • When choosing places for the circuit boards, be sure to allow enough room for the cables. Don’t kink cables or put strain on connectors. Cables should have gentle curves and connectors should not be under any pressure. The cable on the PS3 camera is fairly long, but can be coiled neatly without much trouble.

  • Share, Inspire Others

    Congratulations on finishing! Send us pictures or video of your completed project. Let us know how you customized the design to suit different needs. Show your project to some folks whom you think might be inspired to become makers themselves.