Monday, June 22, 2009

Google Talk XBee Motion Detector

I bought a PIR motion detector from SparkFun a while back. I played around with it a bit, and it works great (at 9V, not 5V), but being hardwired had its limitations. So recently I started a project to make the motion detector wireless, with an XBee radio, and send motion events over Google Talk. This allows you to easily monitor motion events while at work or away.

As for ideas on what to use it for: you could aim the PIR at your couch and see if your pets are violating the couch policy, get notified when your wife/kids get home, place by your front door and find out when a package arrives etc.

This solution uses the XBee I/O Line monitoring feature to send motion detection events to the XBee coordinator. The change detection feature is used to send an I/O sample whenever the PIR alarm pin goes low. With this configuration, the PIR alarm pin is connected directly to an XBee digital input pin, not requiring a micro-controller.

A Java class (XBeeMotion) uses XBee-API to receive motion events and send them to Google Talk. The Google Talk communication is provided by XBee-XMPP, which depends on the Smack library for XMPP.

XBee Configuration

This software works for both Series 1 and 2 XBees. For Series 2 you will need to load the API Coordinator and End Device firmware, if you don't already have it installed. Series 1 is nice in that one firmware image supports both AT and API mode and Coordinator/End Device.

Coordinator Configuration

Series 2 XBees only: place one of the XBees in the XBee Explorer and use X-CTU to flash the ZB Pro API Coordinator Firmware (version 2141 at this time).

Now apply the following configuration (both series, except where noted). Digi did a nice job providing a consistent command set across Series 1 and 2 XBees.

Click the "Restore" button to clear out any previous configurations.
You can skip this step if you just flashed the firmware. Note: for series 1,
this will change the API (AP) mode to 0 (disabled), so you will need to go back to
the "PC Settings" tab and uncheck the "Enable API" checkbox. Then, after you set AP=2
and click "Write", you'll need to re-enable this setting.

Set PAN ID to an arbitrary value. The End Device must also use this exact value

Set the node identifier to an arbitrary string.
This serves as a convenient way to identify your devices

Series 1 Only. Make this the Coordinator

Series 1 Only. Set the 16-bit XBee Address to any value

Set API mode to 2 (escape control bytes)

Click the "Write" button to save the configuration

End Device Configuration

Series 2 Only: place the other XBee in the XBee Explorer and apply the ZB Pro API End Device Firmware (version 2941)

Click the "Restore" button to clear out any previous configurations

Set PAN ID to an arbitrary value. The End Device must also use this exact value

Set the node identifier to an arbitrary string. This serves as a convenient way to identify your devices

Series 1 Only. Make this an End Device

Series 1 Only. Set the Destination Low Address to the Coordinator

Series 1 Only. Set the 16-bit XBee Address to any value

Set API mode to 2 (escape control bytes)

Set DIO4 as A digital input

Turn on change detection for DIO4 (bit 4 -> 16 = 0x10)

Since we reset the XBee to the factory default, the internal pull-up resistors (PR) are on. The internal pull-up will drive the pin high when left floating

Series 2 Only. Set the Sleep mode to Pin Hibernate. You will need to ground pin 9 to keep the XBee awake

Click the "Write" button to save the configuration


I know the SparkFun site says the motion detector works at 5V, but trust me it does not. It may seem like it's working but it will fire erratically at times. My guess is that it would be stable at 6 volts or more but I know it works at 9V. According to the SparkFun product description, the PIR can be powered at 3.3V by soldering a jumper. This would be ideal however I have not tested this.

Google Talk

You will need two Google accounts, one for sending and one for receiving. Note: if you have a Google Apps domain, you can create additional user accounts through the administration tool.

Software Installation

Download the software from Google Code, unzip and create an Eclipse project


Enter your Google username/password. Notice the username must include "", unless you have a Google Apps account in which case it must include ""

Specify the Google Talk user(s) to that will receive motion event messages.
You may enter one or more (comma separated list) GTalk users

Specify the COM port of the local XBee (Coordinator):


Enter the XBee Coordinator baud rate (XBee default is 9600):


Set the minimum number of seconds between motion events, or 0 if you don't want a delay


Wire it Up

The PIR has three pins:

Red (9V)
Brown (Ground)
Black (Alarm)

Place the XBee End Device in the XBee Explorer Regulated and power it by running the 9V supply through the 5V Voltage Regulator. Connect the 4.7K resistor from 3.3V (XBee pin 1) to the Alarm pin. This will pull up the Alarm pin to 3.3V when there is no motion. Connect the red wire to 9V and the brown wire to Ground.
Now wire the Alarm pin to XBee pin 11 (DIO 4). The Alarm pin will go low (0V) during motion and trigger an I/O sample to be sent to the XBee Coordinator.

Series 2 only -- Remember to connect pin 9 (Sleep Mode) to ground (pin 10) if you are using ZB Pro firmware.

* I'm using a USB Explorer for the Motion Detector XBee, but it's only using the USB connection for power -- I promise. I didn't have a XBee Breakout board on hand.

Start the Application

Connect the Coordinator XBee to your computer and start in Eclipse (Run->Run As->Java Application). You should see output in the Eclipse console. Login to the Google Talk with the account that you specified as a recipient, in (gtalk-recipient). Now power up the XBee End Device and PIR. Initiate furious hand waving over the PIR sensor and you start to see IM messages appear.

Next Steps

While motion is present, the PIR generates a steady stream of events, about 1 every 300ms. This is somewhat undesirable as it clutters the airspace with unnecessary transmissions. A better solution would be to connect the PIR alarm pin to an Arduino to "debounce" the signal. For example, only report motion event every x seconds. For this solution, an Arduino digital output pin would need to be connected to the XBee DIO4, to trigger an XBee change detection I/O sample. The problem here is Arduino is 5V and XBee is 3.3V, so a logic level shifter board would be necessary to drop the voltage to 3.3V.

A even more sophisticated solution would involve using serial communication between an Arduino and XBee to send motion events, instead of I/O line monitoring. To do this you could use the XBee Arduino library to send motion packets to the XBee Coordinator. With this solution you could be informed of motion delivery failures and attempt redelivery. It's also possible to send meta data along, for example if you wanted to indicate continuous motion for x seconds. A benefit of this approach is that we only require a single TX packet per motion event, whereas I/O change detection involves two 2 packets per motion event: motion on and motion off.



  1. Hi my friend, nice project!, i have a question.
    Have you tried on your xbee MOTION connecting the pin_alarm to the sleep PIN in a way that the Xbee MOTION remains sleeping all the time, and just awake to send the new status of DI4 when the pin_alarm wakes the Xbee and change the DI4 status. I think we can save energy on that way?

  2. Great suggestion! Currently I'm plugged into the wall, so power is not a consideration, but sleep mode would be key for a battery powered configuration.