Raspberry Pi: Vocal Intruder Warning System Project

Posted by: on Jan 12, 2014 | 13 Comments

raspberry-pi

Our resident coding guru, Vincent, walks us through a brilliant Raspberry Pi project that you can use in your classroom.

Thanks to the GPIO pins on the Raspberry Pi, you can attach a lot of external sensors. One such sensor that is excellent for learning with is a Passive Infra Red detector (or PIR). A PIR detects the heat given off from living beings (people, pets etc).

They are normally used to open doors and turn on lights, but what I am going to show you is how you can use a simple £3 module attached to a Raspberry PI and create a fun room entry warning system that will use text to speech to say a warning out loud.

What you will require

Getting Started

You can do this project either directly on the Raspberry Pi, or over a remote connection using SSH. I have used both methods while doing this project myself and both work exactly the same.

Firstly ensure that the Raspberry Pi is powered off before you attach the PIR module to the GPIO pins as you will be connecting to the 5v pin and doing it incorrectly may damage the Pi or the Module.

The diagram below shows the pins on the module connected to the pins on the Raspberry Pi. The colour of the wires does not matter, what is important is that the pins are connected correctly.

VCC to 5V, OUT to GPIO4 and GND to GND. Pin labels are screen printed on the PIR’s circuit board. The two dials on the top of the PIR allow you to change the sensitivity & dwell time of the PIR, leave them set at defaults for now.

Connecting the Pi to the PIR Module

Connecting the Pi to the PIR Module

Scripting it Together

Now you have the PIR connected to the Raspberry Pi, you can boot it up. Nothing will happen with the PIR until you write some code to check the status of the sensor on the PIR. So, here is a simple script that reads the status of the module written as a python script.

I have borrowed some of this code from the excellent Raspberry Pi Spy website and modified it to output speech when motion is detected.

Firstly open Nano on your Pi:

Now either copy and paste this in (you can paste in a remote shell session by right clicking), or type it all in:

Now press:

CTRL+O – to save the file.
CTRL+X – to exit nano.

Now you need the code to make your Pi Speak.

Now again, either copy and paste this in, or type it all in. This code is originally created by Dan Fountain.

To save the file and exit nano:

CTRL+O
CTRL+X

Running the code

Now you have all the code in place, you can run it by typing the following into the session:

You need to type “sudo” as the Raspberry Pi’s GPIO ports can only be used by the “Super User” and “sudo” tells the Raspberry Pi that this command can run as the “Super User” and python tells the Pi to run this through the default python interpretor.

Taking it Further

Currently my code will use Google to say something. You could change line 56 of “pir_1.py” code to play an MP3 that is in the file like so

This will play an MP3 called dogbark.mp3

You will need mpg321 installed on the Pi. You can do this as follows:

You will need to get an MP3 onto your Raspberry Pi to play through the command.

I have found a free one you can use and you can download it to your Pi with the following comamnd

Thanks to the SecuriPi Kit for the initial wiring set-up instructions.

Check out part 2 of this series where we add a Raspberry Pi camera to automatically take a snapshot of any intruders!

I hope you have fun with this script, let me know in the comments what you come up with.

Vincent is an IT Consultant who has been passionate about technology since lower school. He likes to pass this passion on to others through his posts.

Facebook Twitter LinkedIn Google+ 

13 Comments

  1. Milton
    10 March, 2014

    awesome!

    Reply
  2. Tim Rustige
    10 March, 2014

    How about a credit/mention for the securipi kit?
    This is lifted from our PDF manual, with one word changed:
    “VCC to 5V, OUT to GPIO4 and GND to GND. Pin labels are screen printed on the PIR’s circuit board. The two trimmers on the top of the PIR allow you to change the sensitivity & dwell time of the PIR, leave them set at defaults for now.”

    Reply
    • Karl Rivers
      10 March, 2014

      Hi Tim,

      Thanks for spotting that. I’ve added a mention at the bottom of the post.

      Karl

      Reply
    • Vincent Willcox
      10 March, 2014

      Hi Tim,

      Very sorry for the missed mention. Your manual was excellent and it was I mean no ill-will in not mentioning your kit.

      As you see from the rest of the post, I attribute as required normally.

      Reply
      • Tim Rustige
        10 March, 2014

        Hi Vincent, it’s all fine. There just seemed something strangely familiar when I read the post! Thank you for the credit that’s up now, it’s appreciated. Tim.

        Reply
  3. Brendan Gordy
    22 March, 2014

    What if you could get it to shoot you a text on alert! That would be great for home security!

    Reply
    • Karl Rivers
      22 March, 2014

      Get on it Vincent! ;-)

      Reply
    • Vincent Willcox
      24 March, 2014

      This is something that could be done yes! I have been looking at code to do SMS using a VoIP service. Will also see about SMS from an attached 3G modem/dongle.

      I do have code that uses “Notify My Android” on my personal blog (since the service costs money I felt it was above that of what schools would want to pay for). Please check my profile for a link!

      I will be putting one final part of this code up this week that will include some form of SMS and will email the photos/videos to you.

      Reply
  4. Matthew Butler
    24 March, 2014

    Should the beginning of line 32 of speech.sh read mpg321 instead of mpg123 ?
    Even with this change my RPi gives the following error:
    PIR Module Test (CTRL-C to exit)
    Waiting for PIR to settle …
    Ready
    Motion detected!
    http_open: HTTP/1.0 400 Bad Request
    http://translate.google.com: No such file or directory

    Any thoughts on what the problem might be ?
    Thanks.

    Reply
  5. Matthew Butler
    24 March, 2014

    Scrub my previous post …. speech.sh should contain mpg123 at line 32, not mpg321
    All working now.

    Reply
  6. John Hopkin
    27 April, 2014

    Thanks for the tutorial. I’ve tried following these instructions, but the python script stops, looping at the “Waiting for PIR to settle” stage, as if the PIR is always giving 1.

    I’ve double- and triple-checked the wiring, and tried another PIR in case it was a faulty unit. The PIR is the one you link to from Amazon.

    What else can I check?

    Reply
    • John Hopkin
      28 April, 2014

      After some research online I found it worked when I reversed the outer pins on the diagram above. I’d been interpreting the picture of the PIR as looking at the front of it, so the PIR is facing you, but it should be interpreted as if you’re looking at the back, despite the cover being visible in the picture.

      Reply

Tell us what you think!

x
The ClassThink Guide to Managing iPad in Education
Available on iTunes Soon!

If you'd like us to let you know when our guide is available, pop your email below: