Arduino and PIR Motion Sensor

This evening i have been playing with a PIR motion detector and connected it to my Arduino. I wanted to see how this motion detector behaved and performed. Maybe to create my own motion sensors.
Left=MS13, right=tested PIR:

Regular MS13 and PIR compared

So i hooked it up to the 5V output from the Arduino and connected the PIR output to one of the Arduino’s digital inputs and added a LED to have a visual confirmation of motion detection:

Arduino, XBee and PIR

And i wrote a sketch that switches the LED and writes some text to the XBee that was also connected to the Arduino.

//
// PIR motion detector with LED indicator
// Status info by XBee to PC
// 07 Oct 2009
//

#include <NewSoftSerial.h>

NewSoftSerial XBSerial = NewSoftSerial(2, 3);

int PIRpin = 4;		// digital pin 4 for PIR
int LEDpin = 9;		// digital pin 9 for LED
int PIRval;             // PIR HIGH or LOW value

void setup() {
  pinMode(LEDpin, OUTPUT);    // set the LED pin as ouput
  pinMode(PIRpin, INPUT);     // set the PIR pin as an input

  // Setup SoftwareSerial port
  XBSerial.begin(9600);
  // send informational message to terminal window
  XBSerial.println("Setup finished.");
}

void loop() {

  PIRval = digitalRead(PIRpin);    // read PIR
  if (PIRval == HIGH) {
    XBSerial.print(millis());      // show uptime
    XBSerial.println(" High!");
    digitalWrite(LEDpin, HIGH);    // LED ON
  }
  else {
    XBSerial.print(millis());
    XBSerial.println(" Low!");
    digitalWrite(LEDpin, LOW);     // LED OFF
  }
  delay(100);
}

And here some sample output, captured with Hyperterminal:

...
205564 Low!
205679 Low!
205793 High!
205908 High!
...
...
209485 High!
209601 High!
209716 Low!
209830 Low!
209945 Low!
...

(those numbers are milliseconds)

From what i have seen so far, detected motion results in a high output value for about 1.5 – 2 seconds minimal. And as long as there is motion detected, the output will remain high.

In the very short time i tested this PIR, sensitivity looks very good.

Actually this is very nice; with this PIR you have almost realtime information about whether there is motion detected or not. Hook it up to an Arduino which will do the polling and for example only forward changes in motion detection.

Perhaps even add extra functionality in when to forward changes, like motion has to have a minimal time of x seconds before it is considered motion. Anything is possible. 🙂

Next thing on the list is combining a PIR and a XBee module to make the PIR wireless.

Experimenting with XBee and Arduino

Recently i bought 2 XBee RF Modules from Digi.

For those who have no clue what these modules can do, here some specs:
– Power output: 1mW
– Indoor/Outdoor range: Up to 30/100 m
– RF data rate: 250 Kbps
– Interface data rate: Up to 115.2 Kbps
– 6 x 10-bit ADC inputs
– 8 x Digital I/O
– Configuration: API or AT (Hayes) Commands.
– Operating frequency: 2.4 GHz
– Networking topology: Point-to-point, point-to-multipoint, & peer-to-peer
– Addressing: 65,000 network addresses available for each channel
– Supply voltage: 2.8 – 3.4 VDC
– Transmit current: 35/45 mA @ 3.3 V for normal/boost mode
– Receive current: 50 mA @ 3.3 V
– Sleep current: <10 A
– Interface options: 3V CMOS UART
– Size: 24 x 28 mm
– Weight: 3g

Primary goal for getting these 2 XBee modules was to get familiar with the XBee/Zigbee concept and do some experiments with these modules. For example, i wanted to see if I could get my Arduino wireless. After a lot of reading I decided to give it a try; and here is a summary of how surprisingly easy it was to get the 2 XBee modules working and getting my Arduino wireless as an extra bonus.

Buying
The first experiment was to connect one Xbee module to my PC and another one to my Arduino.
For that I bought the following items:

2 x Digi XBee 802.15.4 (Series 1) module (Digi Article Number XB24-ACI-001)

2 x Xbee Adapter kit
This adapter is used to plug in the XBee modems and takes care of:
– Onboard 3.3V regulator to power the XBee;
– Level shifting circuitry that makes it easy to connect it to 5V circuitry such as an Arduino;
– Two LEDs, for activity (red) and power (green)
– 10-pin 2mm sockets included to protect the modem and allow easy swapping, upgrading or recycling
http://www.adafruit.com/index.php?main_page=product_info&cPath=29&products_id=126
– Specifically created for use with the FTDI cable mentioned below.

1 x USB TTL-232 cable – TTL-232R 3.3V
This is a cable with a FTDI FT232RL usb/serial chip embedded in it. It has a 6-pin socket at the end that connects perfectly to the Adapter Kit mentioned above.

Soldering & Configuration
After soldering the 2 Adapter kits and plugging the Xbee modules into the adapter, connecting the USB cable to an adapter and the PC, the green LED started blinking right away; so far so good! Make sure you have the latest FTDI drivers on your PC, otherwise the cable wont work.
After testing both combinations of adapter and module, I could have started working with the Xbee modules right away, but I chose to change a few things in the modules before doing that:
– updating the Xbee modules firmware to the latest version;
– selecting my own PAN ID instead of leaving it to the factory default.

Changing the baud rate is another useful option to consider at this point. I left the baud rate to 9600 for now.
Configuration can be done in different ways. Either by use of a terminal program (Hyperterminal or whatever your favorite is) or by using a (Windows only) tool made by Digi, called X-CTU. If youre using a terminal program make sure you set the COM port settings to 9600,8,N,1. Connect, and type +++. The Xbee module should respond with an OK. Sounds familiar, right? Yep ,the XBee module can be configured with AT commands. For example, ATBD 3 means setting the baud rate to 9600, ATBD 4 will set it to 19200 baud. ATWR saves the new settings to the flash memory.

If you dont want to look up all the AT commands you need to configure the Xbee module the way you want, the X-CTU tool from Digi is the easiest way. This is what X-CTU looks like:
XCTU Screenshot

Very easy to use; you can perform firmware upgrades with it, save configuration profiles and reload those into the Xbee module, it has some testing tools like a terminal window, etc. It works very easy; no need for F1. (maybe thats why it doesnt even have a help file). For Linux users, AFAIK, the only option is by means of a terminal program. More experienced users should be able to make a script to send all the necessary AT commands to the serial port.

XBee module connected to PC:

XBee Adapter Kit

“The other side”
Ok, now we have 2 Xbee modules, configured and ready to use. Now it’s time to prepare the other side, my new Arduino. So i placed 1 XBee adapter on the breadboard next to the Arduino that was still running its silly 3 blinking LEDs test sketch.

Now it was time to make that sketch a bit more interactive so i created a new sketch that will scan the Xbee Serial data arriving at the Arduino for ASCII characters R, Y, G and (space). The R will toggle the Red LED, the Y will toggle the Yellow LED, the G will toggle the Green LED and the (space) will toggle all LEDs. Just a simple sketch to start with.


//
// LED switching by means of XBee
// Sep 2009
//
#include <NewSoftSerial.h>

NewSoftSerial XBSerial = NewSoftSerial(2, 3);

int ledPinR = 10;    // Red LED connected to digital pin 10
int ledPinY = 11;    // Yellow LED connected to digital pin 11
int ledPinG = 12;    // Green LED connected to digital pin 12
int Rval = LOW;      // initial state of Red LED
int Yval = LOW;      // initial state of Yellow LED
int Gval = LOW;      // initial state of Green LED

void setup()  {

  // Set all pins for OUTPUT
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  pinMode(ledPinG, OUTPUT);

  // Set all LEDs to initial state
  SetPin(ledPinR, Rval);
  SetPin(ledPinY, Yval);
  SetPin(ledPinG, Gval);

  // Setup SoftwareSerial port
  XBSerial.begin(9600);
  // send informational message to terminal window
  XBSerial.println("Setup finished.");
}

void loop()
{
  if (XBSerial.available()) {
      // read serial data byte by byte
      byte inByte = XBSerial.read();
      // scan for specific characters: R,Y,G and space.
      switch (inByte) {
        case 82:    // R toggles Red LED
          Rval = Rval==HIGH?LOW:HIGH;
          SetPin(ledPinR, Rval);
          break;
        case 89:    // Y toggles Yellow LED
          Yval = Yval==HIGH?LOW:HIGH;
          SetPin(ledPinY, Yval);
          break;
        case 71:    // G toggles Green LED
          Gval = Gval==HIGH?LOW:HIGH;
          SetPin(ledPinG, Gval);
          break;
        case 32:    // Spacebar toggles all LEDs
          Rval = Rval==HIGH?LOW:HIGH;
          SetPin(ledPinR, Rval);
          Yval = Yval==HIGH?LOW:HIGH;
          SetPin(ledPinY, Yval);
          Gval = Gval==HIGH?LOW:HIGH;
          SetPin(ledPinG, Gval);
          break;
      }
      // echo the inByte
      // (no specific purpose)
      XBSerial.print((char)inByte);
  }
  delay(10);
}

void SetPin(int pin, int value)
{
  digitalWrite(pin, value);
}

Verify, change code, Verify, change code, Verify, change code, Verify, (hey, were talking C here 🙂  )
Another cup of coffee and finally: Compile, Upload, Running!

Here’s the Arduino board with the other XBee module:

The Xbee adapter is now powered with 5V from the Arduino (red and black wires) and the adapters RX/TX lines go to pins 2 and 3 (green and white). Normally you would use pins 0 and 1 for that, cause those 2 pins are designated for serial I/O, but that would mean I would lose the USB connection to upload sketches to the Arduino. And with very little programming experience on the Arduino that didnt look like a good idea. Fortunately theres a library called NewSoftSerial that makes it possible to do Serial I/O on other pins as well.

Now, when I have a terminal window open and connected to the Xbee module, I can use the keyboard to toggle the 3 LEDs. Yeah, I know, this wont win any “most innovative project” price, but at least I can see my 2 Xbee modules communicating with each other, and my Arduino is wireless. Mission accomplished!
And what has actually been created here? The basics for a wireless, programmable interface with 14 Digital I/O Pins (of which 6 can provide PWM output) and 6 Analog Inputs! If that aint nice

Total costs (in EUR):


Arduino                  22,50
USB cable                18,50
2 XBee modules           31,30  ($ 46,00)
2 XBee adapters          13,60  ($ 20,00)
Total                 85,90

Whats next?
– Getting hold of another kind of Xbee adapter that will Ethernet-enable a Xbee module so I can talk to it from my HA system;
– Integrating Xbee/Arduino combo in my HA system as an additional Interface so I can make use of all of the capabilities the Arduino has to offer from within my HA system;
– Buying a Pan & Tilt mechanism (to cheap to DIY) and hooking it up to the Arduino to learn more of the I/O capabilitys of the Arduino;
– And probably the most important one to result in more new ideas: exploring the capabilities of a stand alone Xbee module (as in: what can you do with the onboard Analog and Digital I/O an Xbee module has to offer)!

To be continued…

My first steps with Arduino

Last weekend i ordered a Arduino Duemilanove.

The Arduino platform is a real easy to use platform. It is intended for those who want to work with microcontrollers; especially those who have no experience in that area and need an environment that deals with all the ‘hard stuff’ to get things working.
The Duemilanove is the latest model in the Arduino product range.

Arduino Duemilanove

It comes on a small board that fits in the palm of your hand. As reference i placed a Visonic MCT302 next to it, a sensor which most of us will know. The largest component is the Atmel ATmega328 microcontroller. There’s also a USB connector on the board to connect it to your PC, and a power connector. It has 14 digital I/O pins, 6 analog inputs, a reset button and some more components needed to support the microcontroller. Connecting to the PC is as simple as connecting a USB cable to the board and your PC.

The software you need to write code for the Arduino board is open source and can be downloaded and installed in just a matter of minutes. It contains everything to develop, test and upload your code to the Arduino board.
Arduino IDE

Today the Arduino arrived. This is really Plug and Play! After connecting the USB cable and power, starting the Arduino development environment on my PC and opening an example program (called a sketch) and uploading it to the Arduino i was ready to go!
dsc_1650_resize

The Arduino hardware can be extended with additional shields that can be plugged on top of the Arduino board. Shields for XBee, Ethernet, Motor control, DMX, RFID, GPS and much more.

Next step will be building 2 XBee Adapter Kits i bought and try to get a wireless connection up and running between my PC and the Arduino, so that i can communicate with the Arduino while it’s disconnected from my PC.

dsc_1646_resize

Once that is working, i’ll try and make my own home-brew Webcam Pan & Tilt mechanism. If i succeed, i think many more interesting projects will follow 🙂