How to build a wireless motion sensor for your Raspberry Pi

How to build a wireless motion sensor for your Raspberry Pi

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

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 wires in the room, but no wires from the room to the receiver).

The receiver has 7 pins.

Pin 1 – GND

Pin 2 – 5V DC power

Pin 3-6 correspond to each button on the key-fob and will go high (5v) when the button for that pin is pressed on the key-fob. We’ll be using these signals to switch a Raspberry Pi GPIO pins.

Pin 7 – ignore

Figure 2

Figure 2 – Key-fob transmitter and receiver

Hack the key-fob

Unscrew the case of the key-fob and examine the contents. Each button will have pads either on the on the opposite side or some on the front that when bridged will send a signal. Use a piece of wire and short circuit a few pads until you find the two pads that associated with a switch. Solder two wires to each pad as per figure 3 and test that it is working by joining the two wires. If the remote’s light goes on when the two wires are joined then it’s working.

Figure 3

Figure 3 – Soldering two wires to the button pads of the key-fob

Now you are ready to connect the key-fob to the motion sensor. Figure 4 is the circuit diagram for the quick and dirty wireless motion sensor transmitter unit. The NPN222a transistor is the electronic switch that closes the circuit of the key-fob when a signal is sent from the out pin of the PIR sensor. The PIR sensor is powered by 2 AA batteries connected in series. The positive lead from the key-fob connects to the collector pin of the transistor and the negative lead from the key-fob connects to ground. The key-fob is powered by its own internal battery. Figure 5 is a close up of the transistor as it is very easy to mix up the collector and emitter on a transistor.

Figure 4

Figure 4 – Quick and dirty wireless motion sensor transmitter

Figure 5

Figure 5 – NPN 2222a transistor

Figure 6 shows the electronic circuit for the receiver. It is powered by a 5V DC power source. When a button on the key-fob is pressed the receiver will send 5V to one of the 4 signal pins (corresponding to the 4 button on the key-fob). The Raspberry Pi needs 3V on the GPIO pins so we will use the NPN 2222a transistor to switch a 3V circuit connected to one of the GPIO pins on the Raspberry Pi.

Warning: Be careful not to connect 5V to any of the Raspberry Pi GPIO pins!

There is a 10k pull up resistor that will set the GPIO pin high when the key-fob is not pressed and low when the key-fob is pressed.

Figure 6

Figure 6 – Quick and dirty motion sensor receiver connected to Raspberry Pi