XBee Basics


What’s cooler than a robot? A robot with a wireless network adapter! The XBEE wireless radio modules from Maxstream are a quick, inexpensive, and easy way to implement a wireless serial interface between two devices. They are practically a drop in replacement for a TTL-level serial cable. They have a low (~ 50mA) current consumption, and long range (rated at 300ft line of sight, but I have at least full speed coverage throughout a two-story house).

An important note: Series 1 XBEE devices cannot talk to Series 2 (or series 2.5 devices)

Wiring Options
Connecting to the XBEE devices is quite easy, however, just remember that they are 3.3V devices. The regular ol’ XBEE will draw about 50mA (the PRO modules may draw upwards of 300mA). We can easily power our regular XBEE from an LP2950 3.3V 100mA regulator.

There are a variety of easy to use modules for setting up your XBEE:

  • An XBEE USB Explorer can be used to connect an XBEE directly to your PC. It is a compact circuit board that already hhas a 3.3V regulator, plus a USB connection to commiunicate with your computer. It draws power directly from the serial port. Simply plug it into a USB port and you’re ready to go. I always keep an Explorer handy to do setup on my XBEE devices. (The XBEE Explorer is the red circuit board shown in the picture below).

 

  • You can also connect an XBEE directly to an FTDI breakout board.XBEEs unfortunately use 2mm headers, rather 0.1″ that we are all so accustomed to, however a breakout board that converts the 2mm layout to a breadboard-accessable 0.1″ one is available for only a few dollars. The breadboard below shows an FTDI breakout board, connected to and powering an XBEE breakout board.
  • An XBEE can be directly coupled to the TX/RX lines of a 3.3V or 5V microcontroller. In either case, it will still require a 3.3V power source. It is recommended that you use several resistors for a voltage divider if using the XBEE with a 5V microcontroller, however, I don’t. The following schematic shows both a circuit with voltage divider resistors and without, regardless, the XBEE always requires a 3.3V power supply:

 

Most users will find they already have the FTDI drivers, but you may find that you need to install drivers for the XBEE Explorer or FTDI breakout. They can be found at http://ftdichip.com/Drivers/VCP.htm

Pairing and Other Parameters
If you want to have your XBEEs communicate at high speed, with the automatic resending of lost packets, you’ll want to pair them. And trust me — you want the automatic resending capability! This is very easy, we just need to set a few registers in the device. XBEE devices have several settings that we will use, and many more that we won’t be using right now. XBEE modules use standard AT commands. This is a protocol left over from the days of serial modems, but it still works for us now. These commands allow you to edit the registers found inside the XBEE. Each register has a name, prefixed by AT, such as ATBD, which is the Baud Rate register.

First off, XBEE radios only operate at a given baud rate, this is the number of bits per second that the XBEE can send. A brand new xbee will default to 9600bps, which is pretty slow. We can change the baud rate, by changing the ATBD register. Both of our XBEEs have to be the same baud rate to talk to one another. The available baud rates (and corresponding ATBD value) are:

  • 1 = 2400bps
  • 2 = 4800bps
  • 3 = 9600bps
  • 4 = 19200bps
  • 5 = 38400bps
  • 6 = 57600 bps
  • 7 = 115200 bps

The next parameter of interest is the Personal Area Network ID. This is a number shared amongst each XBEE in a network. For now, we are only using 2 XBEEs, but we could have many, many more in a single network. XBEEs on different networks do not “see” each other. The default PAN is 3332, so you should avoid that number. The PAN ID is stored in ATID

Once both of our XBEEs are on the same network, we can give each one an address number, denoted by ATMY. We can also set the destination address, which is what address number to talk to, denoted ATDL (for destination low, we really don’t need to use the high bytes if we keep our address numbers < 16 bits in length). A sample setup of two XBEEs that will talk directly to one another, at 38.4kbps:

XBEE1:
ATID = 1111
ATMY = 10
ATDL = 11
ATBD = 5

XBEE2:
ATID = 1111
ATMY = 11
ATDL = 10
ATBD = 5


Additionally, XBEE devices are 2.4Ghz radios. The 2.4Ghz band is currently used by many, many devices. None of them operate at exactly 2.4Ghz, there are several different channels which are in the 2.4Ghz band. We can change which channel the XBEE devices are on, ATCH, but again, devices on different channels cannot talk to one another.

Using the MaxStream Software

MaxStream distributes a piece of software called X-CTU which allows you to set all of the parameters we describe above, using a Graphical User Interface. Unfortunately, the software is a Windows application, but luckily for those of you using Linux (like myself), X-CTU can be run under Wine, here’s how.

When we start X-CTU, we need to select the baud rate and port that our XBEE is connected to:

It’s a good idea to click Test/Query to check that your baud/port are correct. If so, you’ll see a box that says:

Once we know we have a good connection, we’ll switch over to the Modem Configuration tab, and click Read. The items of interest are obviously the Channel (CH), PAN ID, Destination Low (DL), and Source Address (MY). We have to scroll down a ways to see the Baud Rate (BD) setting:

Once we make any changes, click Write to send them out to the XBEE. The write phase could take a little time.

Using the XBEE-API Terminal to Send AT Commands Directly
The XBEE-API Terminal is a simple to use python program that allows you to interact with an XBEE directly, sending AT commands. Why can’t we just use any old terminal? First, we want to send carriage returns rather than newlines, but also, we don’t want to send a carriage return on some transmissions.

To setup the XBEE-API Terminal, you will need Python 2.5, and the PySerial package installed. You’ll then want to pull down xbee-serial-terminal.py and xbee.py and put them on your path (I’d also recommend renaming xbee-serial-terminal.py to something easier like xbeeterm.py):

This same API can be used to programmatically interact with XBEEs quite easily. Setting this up to run, is just like any other python script.

Before we can send AT commands, we need to get the XBEE into AT command mode, by sending “+++” and nothing else, for at least 3 seconds. We should have a short pause and then see “OK”. The XBEE will automatically exit AT command mode after 10 seconds of inactivity. We can then send data to our XBEE, or read back. With our example XBEE still in the XBEE Explorer, we would see:

Code:
> +++
OK

> ATBD
5
> ATID
1111
> ATDL
11
> ATMY
10
> ATND
????<<ADD DESCRIPTION HERE>>

That last command is quite interesting. ATND tells the XBEE to do a Node Detection, which lets us find what devices are on around us. In this case our Arduino XBEE was on, we can now see what the key registers are set to, and how strong our connection is.

We can also easily set parameters. An important thing to note though, is that changes we make are stored in temporary memory, if we power the XBEE device off, they are lost. We need to send ATWR to write the changes to non-volatile memory. If we decide to change our settings:

Code:
> +++
OK
> ATID2222
OK
> ATMY12
OK
> ATID
2222
> ATWR
OK

Our PAN is now 2222, and our ID is 12 rather than 10. Our XBEEs will no longer talk to each other, or even see one another. You should change it back before continuing!

Testing our setup with an Arduino
To test our setup, we can use an Arduino to loopback our data. In this example, the Arduino will recieve data from the XBEE, and then send it back out, after incrementing by one. Thus if we send “Hello”, we should see “Ifmmp”. Hook up one of our XBEEs as shown above with our Breadboarded Arduino Connection, and plug our other into the XBEE Explorer. NOTE: You will have to remove the XBEE from the breadboard when downloading code onto the Arduino.

Code:
// this is where we will put our data
int myData = 0;

void setup(){
    // Start up our serial port, we configured our XBEE devices for 38400 bps. 
    Serial.begin(38400);
}

void loop(){
    // handle serial data, if any
    if(Serial.available() > 0){
        myData = Serial.read();
        if(myData == '\n')
            Serial.print(myData,BYTE);
        else
            Serial.print(myData + 1, BYTE);
    }
}

Notes About Protocols
Being a wireless network, there will eventually be lost information — even though the XBEE has automatic resend of lost packets. For mission critical stuff, I recommend adding your own data integrity or checksum/resend protocols on top of the hardware resend built into the XBEE.