Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

By Jeff Highsmith

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.

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 (keypadd.py) 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”.

keypadd.py Flowchart

The second Python script (alarmd.py) 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.

alarmd.py Flowchart

During the course of the project, I ran into a few challenges that influenced my design decisions:

  • My first prototype used an open source software package called Motion to detect movement with the USB camera and then trigger python scripts. The motion detection was slow and I found that I wanted more control over the motion filtering and event timing. A 3.3V-compliant passive infrared (PIR) sensor was simple to add and orders of magnitude more responsive.
  • The klaxon/siren may sound familiar. I obtained the alarm sound from Apple’s royalty-free GarageBand effects libraries.
  • I experimented with synthesizing the speech on the Pi on the fly, but the poor little computer had to think too long before talking, so I used the Text-to-iTunes-Track service on my Mac to create audio files of the Mac’s synthesized voice, which I copied over to the Pi. The Python scripts call the mpg123 MP3 player on the Pi to play them. If you want to use the Raspberry Pi to synthesis speech on the fly, I recommend that you do some reading.
  • Rather than code the keypad function from scratch, I used Chris Crumpacker’s library for reading my matrix keypad.
  • I tried many different libraries to interface with Twitter via Python code, but TweetPony is the only one I found easy enough to get working quickly.
  • In the future, I might add functionality by using different keypad codes to start different system functions. For example, entering 1234 might arm the system as an alarm, while entering 5678 might just tweet a picture of your foyer whenever motion is detected, so you can see when your kids get home. Right now, there is one code to arm/disarm, and one code to tell the Pi to shutdown so the power plug can be pulled safely.

There are more advanced Raspberry Pi alarm systems out there, but I hope I’ve showed you how easy, fun, and educational it can be to make your own. Now get to it, and report back.

See the entire series here.

Jeff Highsmith

I enjoy inventing new and fun gadgets. I pick projects that are challenging, fun, and educational.


Related

Comments

  1. Pato Perro says:

    Great looking project, Jeff. I really want to give it a try, but must confess my noobiness. Is there a more detailed description of the parts used and the process of setting it up somewhere?

    1. Pato Perro says:

      found BOM on GitHub, but specifically am referring to the circuit diagram: http://bit.ly/15eontZ

      1. Hi Pato, I’m happy to hear that you’re building one, too. The circuit diagram is just showing how the the TIP120 Darlington transistor is hooked up. I just pulled the power switch from the revolving light and ran a couple extension wires from those connections to the Perma Proto insided the enclosure. A fuller explanation and better diagrams of connecting a TIP120 to an embedded system can be found at: http://bildr.org/2011/03/high-power-control-with-arduino-and-tip120/
        I’m slowly working on a Make:Project writeup for this build, and will let you know when that is up. Hopefully the longer format will allow me to better explain the things I glazed over in the video.

        Have fun!
        Jeff :)

        1. Brandon Klen says:

          Hey Jeff, nice work! I have also been meaning to build something similar. I have some experience with building projects from just the schematic. I know you mentioned you are planning on putting up a longer explanation of the project but if you have the time it would be awesome if you can put up the full schematic diagram. Otherwise, I look forward to seeing what you post in the future!

          Thanks!
          Brandon

          1. Brandon, the more detailed project post is in the Make Projects section of the site at: http://makezine.com/projects/pilarm-portable-raspberry-pi-room-alarm/

  2. Mike says:

    This is awesome – nice work! I’d love to build something similar to this, but haven’t a clue when it comes to wiring. This will give me a good excuse to learn some of the basics. Looking forward to your more detailed writeup for the build to try it out!

  3. FarmerRem says:

    Hi, I am thinking about trying this for a 4-H project. I don’t have a pi, and was wondering if It required the newer model. $10 is a big difference when you’re thirteen.

    1. Howdy! Which model Pi you need may depend on what you tools you have available with which to set it up, as well as the end use. In my case, I don’t have an HDMI monitor or TV, and find the composite video out to be difficult to use. I use the composite video out to my TV to get SSH turned on with RaspPi Config, then I use the LAN port on the model B to SSH in and get my wifi adaptor working. After that, I just SSH in over wifi, and don’t need a monitor/TV or hardwired LAN. Keep in mind, though, that if you save the ten bucks by getting the Model A, you have to spend $12 to get a wifi dongle (from Adafruit) if you want to have the Pi connected to the Internet. Optionally, you could skip all of the picture emailing and tweeting abilities, but I think that’s a big part of the fun of this project.

  4. Mirza Dedic says:

    Hey Jeff,

    This is a great setup, I would love to build something like this.. any chance of that detailed write up to get us noobs started? :)

  5. Serenity says:

    This looks real fun. Any news on the detailed write up. I would like to make this as a Xmas present for my son. Thanks!!!

    1. Yes, Neo, that one has 3.3v logic and should work fine. Off hand, I can’t remember whether mine gave a logic high or logic low for movement detection. Keep in mind that you may have to edit that part of the code if yours is the opposite. Have fun!

      1. Pato Perro says:

        Hi Jeff,

        I wondered if the Make article you were working on is any closer to fruition? If you need a proofreader, I’d be happy to oblige.

        Thanks & Happy 2014!

        Pato

        1. Hi Pato,

          You can find the more detailed project post in the Projects section of the Make website at: http://makezine.com/projects/pilarm-portable-raspberry-pi-room-alarm/

          1. Pato Perro says:

            Hey Jeff,
            I’m not sure how I missed that – the skinny comment formatting didn’t help – but thanks anyhoo. I’ll get started right away and let you know how it goes.

            Pato

          2. Pato Perro says:

            Hi again, Jeff. I’ve been going through the tutorial – wonderful write up so far – but I’m stuck at Step 7 chiefly because I cannot read the schematic. The resolution is just not high enough to get a clear look at the pins. I’m also using a PiCobbler and a breadboard to wire it up before I start messing with the perma-proto. Any chance at getting a higher-resolution copy?

            The other foreseeable difficulty is that I’m using the PiCamera. I have generated a still using it and that works fine, but I’m unsure about the naming scheme you coded into alarmed.py. I assume that “%m-%d-%y-%H%M” is a part of fswebcam’s codebase, but couldn’t find anything like that in the raspistill extended docs.

            Any help you could provide would be greatly appreciated.

            Pato

          3. Pato, I uploaded a higher-res version of the schematic to my own blog at http://www.jeffhighsmith.com/wp-content/uploads/2013/08/PiNopticon-Circuit-Diagram-full-res.png

            As for putting the date in the filename, you could just use a single, static filename, but then you could only store the last picture. You could also generate the date using Python, and pass it to raspistill as the desired filename, as mentioned in this thread: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=51960

            I’m happy to see you are making good progress.

            Jeff :)