Jonathan Evans, the founder of PrivateEyePi has written a tutorial on how to construct a wireless motion sensor and interface it to a Raspberry Pi. In this tutorial I’ll show you how to construct a wireless motion sensor and interface it to a Raspberry Pi minicomputer. I’ve detailed two options that both work very well and have long lasting battery power consumption. The first option involves hacking a key fob remote that has four buttons. We will use the remote to send a signal every time the motion detector senses motion. The four buttons of the remote gives you the ability to run 4 wireless motion sensors. The second option uses a much more sophisticated, but low cost, RF transmitter and receiver made by Ciseco. Ciseco have developed firmware that can be loaded onto the RF modules to give them different personalities (temperature sensor, button, hall affect, relay etc…). The XRF is more expensive than the key fob but it offers much better transmission distances that will penetrate walls a lot better than the key fob remote. The XRF also has the ability to send battery levels and operate different networks that give you an almost limitless amount of sensors that can be connected to the Raspberry Pi. Rough Cost comparison Quick and dirty | | | Professional | | Key fob | $7 | | XRF receiver | $17 | Momentary receiver | $5 | | XRF transmitter | $17 | Motion sensor | $10 | | Motion sensor | $10 | Total | $22 | | Total | $44 |
Motion sensor or passive infrared sensor (PIR) For the motion sensor I’ve selected the HC–SR501 sensor shown in figure 1. There are a number of these sensor available with varying reliability but I like this particular one because it has three adjustable controls allowing you to fine tune focal distance (3m to 7m) and the amount of time (0.5 seconds to 5 seconds)it waits before a reset. It also has a dip switch for the repeatable trigger mode which will send multiple triggers when motion is detected. It has three pins: Pin 1 : VCC (DC 4.5-20V) Pin 2 : Sensor Pin 3 : Ground
Figure 1 – HC SR01 motion sensor Wireless option 1 – Quick and dirty The first option uses an off-the-shelf key fob and receiver as shown in Figure 2. This is going to require a moderate level of electronics because you will need to hack into the key-fob for the electronics required to send the signal. It is available at most online electronic stores. With the receiver at around $5 and key-fob at $7 this is a very cost effective option. There are also other similar cheaper units available that do not have a key-fob (internet search OOK on-off-key RF transmitter/receiver). The reason I’ve chosen the key-fob is because it is battery operated and already optimized for sending a signal from a battery operated device. You can also buy very cheap RF transmitter and receivers, however you will need to develop the transmitter and receiver software which is complex and best avoided if possible. The key-fob in figure 1 has four buttons so you have four “channels” you can use. That’s useful if you have a room with multiple sensors (door sensors, motion detectors and window sensors). You can wire them all to one transmitter (yes you need
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 – 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
|