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
blog comments powered by Disqus

Featured Products from the MakerShed

Follow

Get every new post delivered to your Inbox.

Join 25,434 other followers