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 OptionsConnecting 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 = 5XBEE2: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 SoftwareMaxStream 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 DirectlyThe 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 xbeeterm.py and xbee.py and put them on your path:

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:

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.

Notes About ProtocolsBeing 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.

Re: XBEE Basics

I wanted to take a second to thank you for this tutorial. I was really worried for some reason when I orderd my xbees...Everyone said...its easy...you just plug them in and set them to the same channel...this tutorial really makes it clear.

Re: XBEE Basics

Q1: Can you explain if in the pairing process I take each xBee module successively and connect it to my computer with the Explorer board in order to program it?

Q2: Can the XBees, once paired, replace the use of the in circuit programming feature, so that the Arduino can receive my program updates wirelessly from my computer?

Q3: Can you post a sketch example (Arduino) of how one might send a command wirelessly from the computer to the micro to turn on a LED?

Most gratefully, -Migs

"The medicine for my suffering I had within me from the very beginning" Bruce Lee - Jeet Kune Do
"One single grateful thought raised to heaven is the most perfect prayer. " G. E. Lessing
"Time has a wonderful way of showing us what really matters." - Margaret Peters
"We all have the extraordinary coded within us, waiting to be released."-Jean Houston

Re: XBEE Basics

Well, I got it all working. My question is this: If the BAUD is 38400 what difference does the speed make? Isn't it like the wires in a USB cable once the connection is set up?

"The medicine for my suffering I had within me from the very beginning" Bruce Lee - Jeet Kune Do
"One single grateful thought raised to heaven is the most perfect prayer. " G. E. Lessing
"Time has a wonderful way of showing us what really matters." - Margaret Peters
"We all have the extraordinary coded within us, waiting to be released."-Jean Houston

Re: XBEE Basics

Thanks for this great tutorial!

Quick question,
In here you say that both XBEES have to have the same baud rate. Have you tried to have two xbees talk at different baud rates? I did not see this in the documentation. I can easily believe that you are correct, but I could also believe that would not be necessary as the ATBD command sets the baud rate between the xbee and the host and does not effect the RF data rate.

Maybe I will have to try it out and see what happens?

EDIT: Ok, I tried it out. I changed my VB app that emulates my DIY remote control with XBEE to use a baud rate of 38400 (atbd 5) and had it try to connect to my CHR-3 hex running Xans 2.0 code with my XBEE stuff at 57600 (atbd 6) and it worked!

Thanks
Kurt

12-21-2009

cowboynidaho
Guest

Re: XBEE Basics

ok ive been trying and trying to get everything on my xbee equiped arbotix controller to control my nitro car threw my computer using a computer stearing wheel to control the stearing the brake the gas and if i can ever get any of that to work ill want to add other things like an fpv camera and more but i must be a complete moran because im not understanding any of it and no matter how much i look on the net no one seems to of done this and if they have they dont tell about it or how to set it up. i dont mean to be a bother but i need help please if there is anyone out there how knows what im talking about and knows how to do it could you please give me directions and yes spell it out like you are talking to someone who has never used a computer because programming is greek to me!!! Once i get it going i promise ill post alot of pics of the completed project and thank everyone who made it possible.

Re: XBEE Basics

Migs:
The baud is the bit speed at which the PC/Arduino will talk to the XBEE. Any asynchronous serial connection has to define it's baud rate, since the sender and receiver do not share a common clock.

Kurt:
Interesting, although I'm guessing it only works for bursts, if the faster connection is nearly at 100% of the carrier capacity, it can't possibly get through.

Cowboy:
As I said in response to your blog, please post a forum topic, there's numerous reasons why it's advantageous.

To All:
I would suggest posting questions to the main forum, not in response to this tutorial. Authors do not get any notification that people have posted in response to a tutorial, we do usually hang out in the main forum and will see your post there. You'll get a much better response (and a faster one from me), if you post to the main forum.