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:


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.


About Author

Profile photo of Vincent Willcox

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.


  1. 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.”

    • Profile photo of Vincent Willcox

      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.

  2. Matthew Butler on

    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 …
    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 ?

  3. 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?

    • 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.

Tell us what you think!