How To's & GuidesHow to build a wireless motion sensor for your...

How to build a wireless motion sensor for your Raspberry Pi

-

- Advertisement -

Quick and dirty software

The Python code in the Code 1 block below will monitor pin 16 of the Raspberry Pi and print “Motion Detected” to the screen when the key-fob button is pressed. After the motion detector reset time limit has been exceeded it will print “Motion detector reset” to the screen.

#!/usr/bin/env python

“””

Wireless Motion Sensor: Quick and dirty keyfob

For the Raspberry Pi

“””

 

import RPi.GPIO as GPIO

from time import sleep

  

#main program

def main():

        GPIO.setmode(GPIO.BOARD) 

        GPIO.setup(16, GPIO.IN) #RF Remote Receiver

       

        motion = False

       

        while True:

                if GPIO.input(16) == False: #RF key-fob key pressed

                        if motion == False:

                                motion = True

                                print “Motion Detected”

                else:

                        if motion == True:

                                print “Motion detector reset”

                        motion = False

   

                sleep(.3)

               

if __name__ == “__main__”:

        main()

Code 1 – Quick and dirty wireless motion sensor Python code for Raspberry Pi

Wireless option 2 – Professional

This option is based on CISECO XRF module (figure 7) which has the Texas Instruments CC1110 at its core ($1.50) but CISECO have packaged it on a user friendly 20 pin module with all the software required to send and receive RF signals. CISECO (www.ciseco.co.uk) also have a range of wireless sensors, breakout boards and a Raspberry Pi Inventors Kit making interfacing wireless sensors a trivial task. Both the transmitter and the receiver are the same module except with different software loaded onto them. CISECO provide all the software, however loading the software does require moderate Linux operating system knowledge.

Figure 7

Figure 7 – XRF wireless RF radio UART RS232 serial data module

Professional

Figure 8 is the circuit diagram for the professional wireless motion sensor transmitter unit. As with the quick and dirty option this uses the NPN222a transistor as the electronic switch and the PIR sensor is powered by 2 AA batteries connected in series. The XRF is also powered by the 3V battery pack with the positive 3 volts connected to pin 1 and ground to pin 9. The XRF module needs the button firmware loaded. We’ll be using “type 8” of the button personality which sends a BUTTONON and BUTTONOF message when the PIR switches the circuit on and off. More details on this personality and how to configure the XRF can be found at:

· http://openmicros.org/index.php/articles/84-xrf-basics/215-xrf-firmware-upload-with-a-raspberry-pi

· http://openmicros.org/index.php/articles/87-llap-lightweight-local-automation-protocol/llap-devices-commands-and-instructions/121-xrf-firmware-button

· https://github.com/CisecoPlc/XRF-Firmware-downloads

Figure 8

Figure 8 – The professional wireless motion sensor transmitter

Figure 9 is the circuitry for the receiver that connects to the Raspberry Pi. The XRF pins 1 and 10 go to +3V and Ground respectively on the Raspberry Pi. XRF pins 2 and 3 go to pin 10 and 12 of the Raspberry Pi respectively. Pins 10 and 12 on the Raspberry Pi are the transmit and receive serial interface pins.

Figure 9

Figure 9 – Professional wireless motion sensor receiver

Professional wireless motion sensor software

The Python code in the Code 2 block below will monitor the serial port of the Raspberry Pi and print “Motion Detected” to the screen when motion is detected. After the motion detector reset time limit has been exceeded it will print “Motion detector reset” to the screen.

#!/usr/bin/env python

“””

Wireless Motion Sensor: Professional using XRF

For the Raspberry Pi

“””

 

import serial

from time import sleep

import sys

 

defmain():

        # loop until the serial buffer is empty

        currvalue=

        # declare to variables, holding the com port we wish to talk to and the speed

        port = ‘/dev/ttyAMA0’

        baud = 9600

       

        # open a serial connection using the variables above

        ser = serial.Serial(port=port, baudrate=baud)

       

        # wait for a moment before doing anything else

        sleep(0.2)   

 

        whileTrue:   

                while ser.inWaiting():

                        # read a single character

                        char = ser.read()

                       

                        # check we have the start of a LLAP message

                        if char == ‘a’:

                                # start building the full llap message by adding the ‘a’ we have

                                llapMsg = ‘a’

                               

                                # read in the next 11 characters form the serial buffer

                                # into the llap message

                                llapMsg += ser.read(11)

                               

                                # now we split the llap message apart into devID and data

                                devID = llapMsg[1:3]

                                data = llapMsg[3:]

                               

                                if data.startswith(‘BUTTONAON’):

                                        print“Motion detected”

                               

                                if data.startswith(‘BUTTONAOF’):

                                        print“Motion detector reset”

                                                               

                        sleep(0.2)

          

if __name__ == “__main__”:

        main()

 

Code 1 – Professional wireless motion sensor Python code for Raspberry Pi

Over to you

So now that you have built your wireless motion sensor you will want to put it into service and notify you, or sound an alarm when motion is detected. At PrivateEyePi we have many more tutorials like this that show you how to build your own alarm system (projects.privateeyepi.com).

For project parts used in this tutorial please visit www.privateeyepi.com/store

Andrew Edney
Andrew Edney
I am the owner and editor of this site. I have been interested in gadgets and tech since I was a little kid. I have also written a number of books on various tech subjects. I also blogged for The Huffington Post and for FHM. And I am honoured to have been a Microsoft MVP since January 2008, including as an Xbox MVP until 2023.

1 COMMENT

Comments are closed.

Stay connected

7,137FansLike
8,536FollowersFollow
27,000SubscribersSubscribe

LATEST REVIEWS

You might also likeRELATED
Recommended to you