XBee modems are one of the easiest ways to create a wireless point-to- point or mesh network. They have error correction, are configured with AT commands, come in multiple flavors and can create a wireless serial link out of the box. XBee modems can be connected to a computer, and to an Arduino -- there are lots of ways to connect an XBee. And XBee modems can also be attached to a LilyPad. (The LilyPad is an Arduino designed for wearables.)

Specifically for the Arduino, there is the Arduino XBee Shield. This photo shows an Arduino with a connected shield:

After plugging in an XBee radio and stacking the shield on an Arduino, the Arduino can communicate wirelessly with the outside world. If you want to stick an XBee onto a breadboard, this is probably the best method.

In order to configure the XBee modem, you have to hook it up to your host computer. This can be done either with something like the XBee Explorer USB, or using an Arduino proper, which is how I did it. In order for the host computer to "speak" to the XBee which is attached to the Arduino, I had to:

  • Unplug the Arduino.
  • Remove the shield.
  • Change the jumpers on the XBee shield to the "USB" position.
  • Connect the RESET and GROUND pins on the Arduino (see photo below).
  • Replace the XBee shield.
  • Plug in the Arduino (to USB).

The serial connection from the host computer then goes directly "through" the Arduino to the XBee shield, and I can configure the XBee proper. (Remember: during this time, the host computer cannot "see" the Arduino, i.e. you can't upload your Sketch to it.)

When you've finished configuring the XBee, undo the changes:

  • Unplug the Arduino.
  • Remove the shield.
  • Change the jumpers on the XBee shield back to the "XBee" position.
  • Remove the bridge between RESET and GROUND pins on the Arduino.
  • Replace the XBee shield.
  • Plug the Arduino back in.

Here the host computer talks to the Arduino again over the serial USB connection (i.e. you now can upload Sketches to the Arduino).

It should be quite easy to set up two Arduinos with XBee radios, and the configuration of the radios should also be easy. Unfortunately that seems to be the case for XBee Series 1 radios only: an attempt to set the "MY" address with an ATMYnnn failed with an ERROR in the AT command on my Series 2 modems.

After downloading and installing the X-CTU software (onto a Windows system, as the software is available for that platform only), I used this excellent guide (PDF) as a starting point, and within minutes, my two XBee radios were happilly chatting to eachother.

Even though the following looks like a simple Blink, it is in fact a remote blink. :-) I have two Arduinos talking to eachother:

  1. The first alternates in sending '1' and '0' with a 500ms delay in between.
// Alternate sending '1'/'0' over XBee
    
    void setup()
    {
      Serial.begin(9600);       // XBee
    }
    
    void loop()
    {
      Serial.print('1', BYTE); 
      delay(500);
    
      Serial.print('0', BYTE);
      delay(500);
    }
  1. The second toggles the red LED depending on the value it receives over the wire.
// Toggle LED on LEDPIN depending on '1'/'0' received
    // serially over XBee
    
    #define LEDPIN  13
    byte val;
    
    void setup()
    {
      pinMode (LEDPIN, OUTPUT);
      Serial.begin(9600);       // Start talking to XBee
    }
    
    void loop()
    {
      if (Serial.available()) {
        val = Serial.read();
        if (val == '1') {
          digitalWrite(LEDPIN, HIGH);
        } else if (val == '0') {
          digitalWrite(LEDPIN, LOW);
        }
      }
    }

When I power up both Arduinos, it takes but a few seconds for the XBee radios to synchronize, and the LED on the receiver starts blinking.

(It might be obvious, but it is important to note that the Arduino uses its digital pin 0 and pin 1 to communicate in serial with the XBee shield. This means, that when you are testing your XBee sketch, you can't Serial.print() something to the serial monitor: whatever you send out on the serial interface, goes to the XBee! This of course makes debugging of your sketch a pain.)

I thought I'd take the receiver for a little walk, so I attached a 9V battery and walked around the house. Even without an antenna on my XBee radios, the Arduino kept the LED blinking until I got into the cellar, where it stopped. The distance without an antenna corresponds to the specifications: approximately 25m, which isn't bad.

Flattr this
Hardware and Arduino :: 07 Jun 2009 :: e-mail

Comments

blog comments powered by Disqus