Motion sensor via XBee connected Arduinos

From time to time I try to improve my technical installations at home. Sometimes due to real needs, sometimes just for fun. Last time when I was sitting in my basement office and my dog had raided my garbage can, I felt it would be great to have a little warning signal whenever my dog comes into my room and tries to mess things up. So I bought some gear, boards and XBee controllers which should do the trick. On my first shopping tour I bought an Arduino Leonardo, Yùn, two Wireless SD Shields, two XBee ZB modules and of course a battery connector. This should be enough, at least I thought so … But unfortunately I did not read the whole documentation carefully:

Due to the fact that the Arduino boards differ from their design (all examples were build with Arduino UNO), I was not able to connect to the XBee modules via my MAC from USB. This was necessary to program these units (like it was shown with the UNO board) with XCTU. Although I was very excited that the software now exists for my MAC, I still could not make any connection for changing the firmware, even when I swapped the Serial classes, which are needed to transfer the sensor electrical data to another Serial interface. After some research I found some useful documentation, but due to the fact that I didn’t want to solder on the XBee – Wireless SD shield I decided to go for a second round of shopping.

Finally I ended up with all this equipment:

Tools

The most important thing to start with XBee programming (at least in my case) was the USB-shield which can connect to the XBee modules (the red board on the left) from my computer via USB to the XBee. I had the NeoPixel ring and the PiR motion detection sensor as a leftover from one of my earlier projects. So right now everything was complete …

First step was to re-program the two XBee modules. So one of them must be a XBee Coordinator and the other one must be an End device or a Router. Every XBee network needs at least one Coordinator otherwise the whole setup does not work. My XBee modules were programmed as Router as I received them, so I had definitively to change the firmware and the operating modus. The MAC software looks promising and everything was really nice and easy (after I used the USB-shield and not the Yùn or Leonardo board):

Software

Autodiscovery worked and after the right firmware was in place I only had to program some connection parameters. In this case I had to set the End device’s DH and DL to the Coordinator’s SH and SL and vice versa. Other params which are necessary (like PAN ID were left as default values, just to make things easy). More detailed information can be found here. So it looked like the table below and this did the trick:

Coordinator End device
SH 13A200 SH 13A200
SL 40ACC0A8 SL 40ACBF9C
DH 13A200 DH 13A200
DL 40ACBF9C DL 40ACC0A8

The next step was: connect the XBee modules with the Wireless SD shield in the right way. This was not very difficult:

ToolsII

Due to the fact that I already programmed the XBee modules I set the jumper to „MICRO“ to connect to the controller via the serial interface. But now, after connecting the Wireless SD shields to my Arduinos I have to unreveal an additional information. If I had read the documentation of Leonardo and Yùn carefully I would have know the following fact:

To send and receive serial communication you have to use Serial1 as the programming interface, because with Leonardo and Yùn Serial class connects only to the USB connection (unlike the UNO).

After that experience it was quite easy :-).

I connected the XBee End device with the Leonardo, used PIN 2 for the PiR and tried to keep the code very simple. I just send H (movement) or L (no movement) over the wireless connection. This was enough to give the state information if something moves (or tries to sneak into my basement office):

int calibrationTime = 20;
int pirPin = 2; //the digital pin connected to the PIR sensor’s output

void setup(){

Serial1.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);

//calibration time for the sensor
for(int i = 0; i < calibrationTime; i++){

delay(900);
}
}

void loop(){

//if something moves
if(digitalRead(pirPin) == HIGH){

Serial1.print(“H”);
delay(50);
}

// if no movement is detected
if(digitalRead(pirPin) == LOW){

Serial1.print(“L”);
delay(50);
}
}

Next step: I put the Coordinator XBee to the Yùn and connected the NeoPixel ring to PIN 2 and I used the 5V and GND connector. Of course I included the programming library for NeoPixels which could be found here. My intend was to keep it simple. So I programmed the Yùn as receiver and just evaluated the character received via the wireless connection:

#include <Adafruit_NeoPixel.h>

// Digital send PIN
#define PIN 2
//24 Pixels in ring
int pixelNumber = 24;
char msg = ‘ ‘;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
Serial1.begin(9600);
strip.begin();
// strip.show(); // Initialize all pixels to ‘off’

}

void loop() {
while(Serial1.available() > 0) {
msg=Serial1.read();

if(msg==’H’) {
for(int i=0; i < pixelNumber; i++) {
strip.setPixelColor(i,255,0,0); //red
}
strip.setBrightness(255); //maximum brightness
strip.show();
}
if(msg==’L’) {
strip.setBrightness(0); //no brightness
strip.show();//off
}
}
}

Finally the Arduino boards, connected with sensors and XBee modules looked like this:

Pir

Now I need a placement for the PiR sensor. I decided to go for the ceiling of the stairs which leads to my basement office and the alarming light should be near the door (at least for a video test the optimal position):

Placement

Is it working? Of course :-) .. I added a video which shows my „Dog-motion-sensor“ ….

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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: