Raspberry Pi PIR Motion detection and alerting to SMS. #raspberrypi #sms #sensors

This is a real simple project to get started on. Partly inspired from Alasdair Allan‘s work in the book Distributed Network Data.  While the book concentrates on Adrunio I’m also interested in sensors from the Raspberry Pi side of things because the likes of storage and network is already there.

So, what I’m aiming to do is to wire up a basic passive infrared (PIR) motion detector to the Pi and with Python have it tell us when movement is detected. That’s easy enough. Broadcasting to the world, easy enough but I’m not really sure that all of Twitter really needs to know when there’s movement detected in your house, it’s the first step to being unfollowed.

To make this useful though I’ll make it send an SMS message direct to the phone instead. Then I’ll know when I’m being broken in to.

The hardware

photo copy


It’s a very basic setup, the PIR is wired to the breadboard and the Pi connects also:

photo copy 2


Pin 2 – 5v is connected to the red rail of the breadboard.

Pin 6 – Gnd is connected to the black rail of the breadboard.

Pin 26 – GPIO 7 is connected to my row 30/a of the breadboard.

The PIR motion sensor is connected very similar with the 5v going to the red rail, Gnd going to the black rail and the output pin (to trigger motion) is connected to 30/f on the breadboard (the same line as the GPIO7 pin from the Pi.

As hardware goes, we’re done. The rest is software.

The code

The basic code has been based on the work that Matt Hawkins has done, no point reinventing the wheel on this one. Using the RPi.GPIO library in Python he gets the basic PIR sensor talking to the Pi.

You can see Matt’s original blog post here.

I’ve made a couple of small amendments:

Firstly I’ve added a method that will send a text message to the TextLocal service. What we want to do is send an SMS message to a predefined number when the motion sensor is triggered.

So to Matt’s code we can add:

import urllib
def sendSMS(uname, pword, numbers, sender, message):
 params = {'uname':uname, 'pword':pword, 'selectednums':numbers, 'message':message, 'from':sender}
 f = urllib.urlopen('https://www.textlocal.co.uk/sendsmspost.php?' + urllib.urlencode(params))
 return (f.read(), f.code)

And within the if statement block where motion is detected, alter it so it reads:

if Current_State==1 and Previous_State==0:
 print " Motion detected!"
 sendSMS("jasebell@xxxxxxxx", "xx_password_xx", "447900xxxxxxx", "447900xxxxxx", "There's been movement detected in the house.")

Basically the sendSMS method expects the arguments of username, password, recipient number, sender’s number and the message you want to send.  This is then sent as a URL POST request to the textlocal.com server. You’ll need an account and credits setup first before you can try this out. Remember that SMS costs money.

Testing it out….

You’ll need to run this as the root user so Python can access the GPIO pins.

pi@raspberrypi ~ $ sudo python pir2sms.py 
Waiting for PIR to settle ...
 Motion detected!

When the script reports that the sensor is ready then try gently waving your hand in front of the sensor. A text message will be sent to you:


24 responses to “Raspberry Pi PIR Motion detection and alerting to SMS. #raspberrypi #sms #sensors”

  1. you forgot to drop down the voltage on your output signal into the raspberry pi. If you decide not to do that you can damage your little computer. There is no protection curcuit on those gpio pins. I would switch your VCC to 3.3V then use a 550 ohm resistor on your output signal. This will make sure you don’t burn up your raspberry pi.

  2. This is awesome, just got mine setup and running. Quick question, the black heads you have on the tip of your wire before it connects to the circuit, what are those called? Any idea where I could buy those online? They would come in tremendously handy.

    Thanks for the writeup!

  3. You could basically use any communication whether it be email, SMS, a Tweet or so on. For me I like immediacy (and I don’t depend on email/tweet notifications on my phone). I will, however, look at a text when it comes in.

  4. Daniel, if you have a Maplin store nearby go to the computer connector section, all the fans and cases and stuff. There’s a bunch of disk drive connector leads and so on. I use those as they have male to female connectors on and connect to the PIR motion sensor really well.

  5. “I would switch your VCC to 3.3V then use a 550 ohm resistor on your output signal. This will make sure you don’t burn up your raspberry pi.”

    Is there any chance that you could post a photo of this modification?

  6. I’ve been looking into this with regards to a few commments I’ve had. In previous examples of this I’ve seen no mention of having to use any resistor on the output signal pin. Looking at the Adafruit pages on the PIR motion sensor the output pin is rated at 3.3v and can be used directly into the GPIO pin.


    “Although the PIR sensor requires a 5V supply, its output is a Pi-friendly 3.3V, so it can be connected directly to a GPIO input.”


  7. New to python . Trying to figure out how to use smtp on my raspberry pi to send a text via my gmail account. also would you please post the finished activitiy so i can see where your amendments go? Thank you in advance

  8. Hi Pawel,

    I’m in the UK and textlocal.com have an easy to use account and API so I can send/receive SMS. It would be interesting to see how to do it with a proper GSM module though.

  9. I was thinking about plugin really cheap Android phone , (maybe even damged-> say with broken microphone or something) which you could get from few bucks on ebay or similar. And then run ADB (Android Debug Bridge) to run your App which would send appropriate message at appropriate time.

  10. Pawel, I’ve been giving this one some thought and I don’t know how if you can send data via ADB to the device? I know you could send an SMS via code from the device though.

  11. Thanks for the link, I’d never considered that approach to doing things. I usually have my Nexus S and Kindle Fire charged from the mains.

    I’ll give this a go the next time.


  12. All down to the PIR sensor you are using. Things will need tweaking, do you have two small screw like pots you can turn? One will change the sensitivity of the sensor and the other how long it will hold it’s “on” state.

  13. i am getting this error “ssl.CertificateError: hostname ‘www.textlocal.co.uk’ doesn’t match either of ‘www.txtlocal.co.uk’, ‘txtlocal.co.uk’” Any suggestions please

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: