XBee communication

USB ⇔ Arduino ⇔ XBee

XBee shields and breakouts

USB ⇔ Arduino

Adding in an XBee

Here’s a picture that might help out a bit when dealing with the Arduino XBee shields.
Connecting XBee to the Arduino
Look at the picture until you believe the following two statements.

In the typical USB, Arduino and XBee setup, two of the three “talk” while the third can silently listen. This sometimes means that the USB is unplugged from the computer or the microprocessor is removed from the Arduino board. Because the Arduino Mega has four serial ports, it is possible to for the microcontroller to speak with both its USB partner and an XBee. However, this cannot be done using an XBee shield.

Here’s a table showing how to set up an XBee to talk to either the Arduino or the computer on various shields.

deprecated shield XBee ⇔ Arduino Both jumpers in XBEE position.
XBee ⇔ USB Both jumpers in USB position.
Wireless SD shield XBee ⇔ Arduino Switch in MICRO position.
XBee ⇔ USB Switch in USB position.
Sparkfun XBee shield XBee ⇔ Arduino Switch in UART position.
XBee ⇔ USB Switch in DLINE position sends XBee communication to Arduino digital pins 2 and 3. You’ll need to write a program using SoftwareSerial.
Breakout boards XBee ⇔ Arduino Wire XBee DOUT (pin 2) to Arduino P0 and XBee DIN (pin 3) to Arduino P1.
XBee ⇔ USB Wire XBee DOUT (pin 2) to Arduino P1 and XBee DIN (pin 3) to Arduino PO.


Many sources recommend removing the microcontroller from the Arduino when you want the XBee to connect to the outside world using USB. However, others say that it is sufficient to install the following empty sketch on the Arduino.

void setup() { }
void loop() { }

Load the empty sketch onto your Arduino. Don’t expect anything exciting to happen.

Although the folks at Digi clearly state that “ the inputs to the XBee module are NOT 5V tolerant and cannot accept a voltage level higher than 3.3v on any pin,” you will find many examples of XBee-Arduino interconnection using breakout boards on the web where the 5V outputs of the Arduino are directly routed to inputs of an XBee. At the least, you might consider using a voltage divider to bring the XBee input below 5V. The XBee output should be OK since digital inputs over 3 volts are read as HIGH. Because the Computer Science department has many Sparkfun logic level converters, I just put one of those between my XBee and Arduino. However, I only use the bi-directional TX connectors of the converters. The RX connectors are nothing more than badly designed voltage dividers.

Go ahead and attach your Arduino to an XBee. For now set up your system so that the XBee communicates through the USB connection. It might be best to restrict ourselves to the Arduino shields and XBee breakout boards for this part of the exercise. You can use the Sparkfun shield when the XBee and Arduino start talking.

If you are using a breakout board, be sure to use the Arduino’ 3.3V as the power supply to the XBee You can also attach LED’s to pin 6 (RX signal strength indicator), pin 13 (on), and pin 15 (associate) to see what your XBee is up to. No resistor is needed.

USB ⇔ XBee

If you want to attach an XBee to a personal computer, you should consider using a USB XBee Adapter. These adapters are very useful in those situations where you are debugging XBee applications or when you want to capture XBee output for a web application.

The language of choice for controlling an XBee connected to a personal computer is Python. This should work well on the three major platforms: Linux, Mac OS, and Windows. There are some nice Python libraries, such as python-xbee, that make it easy to get started.

If you don’t have a XBee attached to your Arduino, ask for an XBee USB adapter. You might need an FTDI driver if you are using a Windows system.

The language of the XBee

When talking to the computer, the XBee supports 5-wire RS-232. The five wires are ground, transmit data, receive data, request to send (RTS) and clear to send (CTS). The last two can be used for flow control. Because the Arduino shields don’t support flow control, we are going to be using 3-wire RS-232.

To control the XBee, a variation of the 22-year old Hayes AT modem commands are used. A complete description of the XBee AT command sets can be found in the 70 pages documenting the XBee® 802.15.4 (series 1) modules or the 157 pages documenting the XBee® ZB (series 2) modules.

The XBee module uses Hayes’ patented Improved Escape Sequence With Guard Time Mechanism (#4,549,302 which expired in 2003). To enter command mode, the following (see page 27 of the XBee® ZB documentation) must happen before the XBee will acknowledge entry into command mode by printing OK\r.

Getting your computer to talk through the USB

At this point we need to figure out a way of making a connection to the XBee through your computer’s USB. A great source of information about this task, and any other XBee related task, can be found in Robert Faludi’s Building Wireless Sensor Networks, available on-line through the UNC Asheville library.

The first challenge is to determine the name of the USB port. If you just downloaded a program to your Arduino, the port name is the Serial Port your just used. In Windows the name will be similar to COMNN; in Linux, similar to /dev/ttyACMN or maybe /dev/ttyUSBN, and in Mac OS X, similar to /dev/tty.usbserial-XXX.

The second challenge is finding a good serial terminal program that works well with the XBee. You will need one that enables local echo; however, The bigger problem is translating the XBee's newline character into whatever your system prefers after your operating system has done its work. (Review Wikipedia’s article about newline to see just has much trouble this can be.)

Robert Faludi lists several possibilities in his book. He likes CoolTerm which was written by Roger Meier and available through Meier’s freeware web site for Mac OS X and Windows. The instructors have used PuTTY on Windows for connecting to networked computers, via ssh, and serial ports for years but are having trouble getting it to handle linefeeds correctly on Windows.

If you are a long-time Linux user, you might just want to stay with screen but you’ll need to figure out how to turn on the local echo. We’ve had good luck with minicom and putty on Linux. (Oddly enough, we can’t get putty to do linefeeds correctly on Windows.)

In any case, here’s a short table of what seems to be working. If you have any more suggestions, tell us.

minicom on Linux Run as minicom -b 9600 -D /dev/ttyXXX and use the following commands: C-A A to add linefeeds, C-A E to turn on echo, and C-A Q to exit.
putty on Linux PuTTY terminal emulation settings  Putty font settings  Putty serial line settings 
coolterm on Mac OS X CoolTerm serial line settings CoolTerm terminal emulation seetings
coolterm on Windows CoolTerm serial line settings CoolTerm terminal emulation seetings

In any case, you will need to set your chosen program’s configuration for the default RS-232 settings for the XBee.

Whatever program you use, be sure you know how to exit it before you use it.

See if you can type +++, with no characters before and after, to your XBee and be rewarded with an OK. If this works, you might try typing the commands ATSH and ATSL, followed by Enter, to see the serial number of your XBee.

A bit about the XBee architecture

A bit about 802.15.4

A bit about ZigBee

Some XBee AT commands to check things out

Let’s try out some more AT commands to check out your XBee. Be sure to write down the serial number of your XBee this time. Remember, you must enter command mode first. Command mode will expire after a few seconds, so you may need to +++ more than once. Also, it is possible that some of these commands will not word on Series 1 modules. See pages 131-132 for more information

Here are a few commands to try out. Write down the displayed information. The XBee only speaks in hex.

commanddisplayed value
ATVRFirmware version
ATSHSerial number — high 32 bits
ATSLSerial number — low 32 bits
ATMY16-bit network address for this node
ATMP16-bit network address for my parent
ATOI16-bit operating PAN ID
ATCHOperating channel
ATDHDestination — high 32 bits
ATDLDestination — low 32 bits

Transparent mode

Those last two commands may have you wondering about the destination.

Ask someone in the class for there serial address, both high and low bits. The high bits will probably be 13A200, a number assigned to Digi. The low bits should be unique through out the class. If you set your destination address to that of another XBee, all the non-AT characters you type will be forwarded to the other XBee.

You can set the high bits of the destination with the following AT command:
ATDH 13A200

After setting the high bits, set the low bits. Now use ATDH and ATDL to make sure you really set the destination. If you don’t want to wait 100 seconds to drop back into transparent mode, use the AT command ATCN to drop out of command mode. Now type a nice message to other XBee. If that doesn’t work, check that the destination is set correctly.

Finally type the ATWR command to the XBee to tell it to remember this destination when it is power off.

But couldn’t my Arduino do that?

Yes! Rather than you typing sup once every ten seconds, you could connect your Arduino to the XBee and have it send out sup once every ten seconds,

At this point, your XBee has been paired up with another XBee. Only one of XBee of the pair can perform the next step, so decide which it will be. We’re going to can that XBee the “first-step” Xbee.

Try very hard to avoiding powering down either of the XBees during this next step. If you do they will loose their destination address.

Disconnect the first step XBee from your system and set up your Arduino to so that you can run the Arduino IDE.

Now write a little Arduino program that prints sup XYZ N every ten seconds where XYZ is a three-letter word that identifies you and N is incremented for every message. If you want to do a little extra, you can make N depend on some external output, such as a voltage reading read on one of the Arduino’s analog pins.

Do not proceed until you have tested your Arduino program by looking at its serial output. Be sure that your call to Serial.begin sets the baud rate at 9600.

When you are convinced your program is working reconfigure your system so that the output stream of the Arduino is directed to the XBee. If you do this correctly, your sup messages will be printed at the destination system.

Finishing up: two projects

Time may be running short; but, if any is left, choose one of the following two projects.

Reading data from the XBee

Reconfigure and reprogram the second stage XBee so that it parses the communication stream from the first stage XBee and does something interesting such as changing the intensify of an LED.

Using AT commands to choose your destination

Presently the fist stage XBee has a hard-coded destination. Modify your Arduino program so that it uses AT command to initialize its XBee to a new destination. You can find a program to do something like this on the Sending a Message to a Particular XBee tutorial.