Getting started with the Arduino and XBee

Getting started with the Arduino and XBee

Today, we’ll be talking about how to set up something called the XBee with the Arduino.  The XBee is a wireless device  made by Digi International.  The weakest XBee made is capable of communicating at a distance of up to 400 feet outdoors (100 feet indoors) in a high interference environment (Robogames, for example) with almost no data loss, etc.  In addition, you can run thousands of units on one network simultaneously.

Now, before we begin, I realize there are dozens of articles similar to this one scattered around the web.  However, when I first started using the XBee, I was unable to find an article that had all the information I needed in one place.  Therefore, I thought I’d make my own!

This article assumes you’re using a Windows machine, but I may update later on with instructions on how to do this under Linux.

Here’s what you’ll need:

  • One Arduino Uno or Arduino Mega.
  • One Arduino Wireless SD Shield.
  • Two XBee wireless modules.
  • One USB XBee interface.
  • One 6V 2Amp power supply.
  • One USB A to B cable.
  • One USB A to Mini-B cable.

You can get all of these items in a kit from Trossen Robotics.

Step 1: Configuring the XBees

The first thing you’ll need to do is download and install the X-CTU software from Digi International, which is under the “Diagnostics, Utilities and MIBs” drop-down menu.  After that is installed, mount one of the XBees on the USB to XBee interface as shown in the picture below.

The XBee mounted on the USB interface which is plugged into the computer.

Plug the interface into your computer using the USB A to Mini-B cable and then launch the X-CTU application.  Change the value under “Baud” to 9600.

You should now see something similar to “USB Serial Port (COM4)” in the “Select Com Port” box.; select it, and then click on the “Modem Configuration” tab in the window.  Now you’ll see a window similar to the one below.

The Modem Configuration tab in the X-CTU application.

Click the “Read” button and then wait a few seconds.  The space that was blank should now have a bunch of different information in it.  The items we’ll be modifying are ID – PAN ID, DL – Destination Address Low, MY – 16-bit Source Address and BD – Interface Data Rate.

The PAN-ID (or ID) is the Personal Area Network Identifier.  For XBees to “see” and communicate with each other, they must have identical PAN-IDs.  It can be any number between 0001 and 9999; just don’t use 3332 because that’s the default!  Also, be sure to remember this number because you’ll need it for the second XBee too!

The Interface Data Rate determines how fast your XBees can communicate with each other.   We’ll be using a rate of 38400 Bits Per Second (BPS), which corresponds to “5” in the X-CTU utility.

For the rest of the values, it should look like the following:

DL: 11
MY: 10
BD: 5

Once you have entered the values, click “Write” and wait for it to finish.  After it has finished, repeat this process for the second XBee using the following values (ID and BD stay the same):

DL: 10
MY: 11
BD: 5

Step 2: Setting up the Arduino

Next up, you need to get your Arduino ready to use!  First, download and extract the Arduino Software and then plug the Arduino into your computer via the USB A to B cable.  You don’t need to worry about the power supply yet; the USB cable supplies power to the Arduino automatically.

Windows will now try to install the driver software for the Arduino.  Once it invariably fails, you will need to tell the Windows Add Hardware wizard to search the FTDI USB Drivers folder which should be in the Arduino folder you extracted, under Drivers (arduino-x.x.x >> drivers >> FTDI USB Drivers).

Once Windows finishes installing the drivers, launch the Arduino software.   Then go to Tools >> Serial Port and select the COM port your Arduino is connected to.

Now you need a program for the Arduino!  Download and extract this file and then open it inside the Arduino editor (I’ve commented almost every line to explain what everything does).  You should now see a screen similar to the one below.

The Arduino software with the example program loaded.

Click the green arrow pointing to the right (the second icon on the top left of the screen).  This will “upload” the program to the Arduino board.  Once it has finished uploading, close the editor and unplug everything from the computer.

Step 3: Putting it all together

You’re almost done!  We now need to plug one of the XBees into the Arduino.  To do this, we must first install the Wireless SD shield on it, and then the XBee will go on top of that.

You can see the finished result in the image below.  I’m using an Arduino Mega, but the Wireless SD Shield mounts in the exact same way on an Arduino Uno.  Also, take careful note of the direction that the XBee and Wireless SD Shield are mounted in.

The Arduino with the XBee and Wireless SD Shield installed.

Be sure to verify that the tiny switch on the top of the Wireless SD Shield is set to “Micro”.

Next, plug the other XBee into the computer via the USB interface.  Then, plug the 6V power supply into an outlet but don’t plug it into the Arduino yet.

Your setup  should now look something like the picture below (excluding the Arduino being plugged in).

The Arduino and XBees connected and powered on.

Open the Arduino software on your computer and go to Tools >> Serial Port to select the COM port your XBee USB interface is connected to.

Go to Tools >> Serial Monitor; you should see a blank screen.  Set the baud value at the bottom right of the serial monitor window to “38400”.

Now it’s time to plug the Arduino in! A few seconds after plugging it in, the serial monitor should be outputting some stuff similar to the image below.

The serial monitor showing output from the Arduino.

Congratulations, now you’re thinking with portals XBees!  From here, you can do all sorts of things like wireless data collection, autonomous robots of mass destruction, etc.

Since this is my first real tutorial, please leave your comments and tell me if anything should be improved or changed.

XBee Basics (For description of PAN-ID and BD Interface Data Rate)



    • Muhd Azri
    • October 31, 2013 at 9:26 pm

    Hello, thank you so much for sharing this Xbee/Arduino program.I played around with the Arduino sketch that you upploaded and i tried to change the baud rate from 38400 to 115200. when i plugged in the Xbee, the serial monitor printed out all kinds of gibberish. Do you mind explaining why this is as such? thanks so much!

      • Brandon
      • October 31, 2013 at 10:47 pm

      Thanks so much for your comment; I’m glad my examples were able to help you! :D

      For now, I’ll assume you changed the baud rate inside the sketch itself but left the baud rate in the serial monitor the same. The reason you got all sorts of gibberish is because on one end of your serial connection (the XBee, Arduino and the sketch), you’re trying to read and write 115,200 bits per second, whereas on the other end of your connection (the computer), you’re trying to read and write 38,400 bits per second. What this basically means is that one end of the connection expects to send and receive a lot less data per second than the other end, causing things to get squished together and jumbled. My information might be a little off (I’ve never gotten too into the technical details of RS232 serial protocols), but I hope this helps clear up things a bit!

      To change my example to use a 115200 baud rate, you’ll need to do these two things:

      1: Change line 52 in my sketch from “Serial.begin(38400)” to “Serial.begin(115200)”.
      2: Change the baud rate in the bottom right of the serial monitor to 115200.

      If everything goes well, you should be receiving legible information instead of gibberish!

    • Alain
    • January 27, 2014 at 8:01 pm

    I get two Xbee 900 pro RPSMA conected on Arduino Uno and Arduino Due.

    All communication at 9600 baud (Xbee module, Arduino serial.begin()programmation, Arduino PC Monitoring window, PC usb com)

    I use Arduino Xbee shield on both processor (Uno and Due). I set jumper on Arduino Xbee shield to Xbee (connecting Xbee to serial arduino line) and use “Serial.print()” Arduino command at Tx Xbee module to “talk” with the Rx Xbee module.

    For now i get a one way communication but I will need full duplex.

    Get data loss between Tx and Rx ???

    I try différent baud rate. 9600 work better. I work with “Quad-band Wired Cellular Antenna SMA” from Sparkfun. A dipole antenna operating at 900 MHz.

    The data is well printed on the Tx monitoring Windows but in the Rx unit, stream of data miss (in Rx monitoring windows and in Rx Arduino buffered string).

    Get a few garbage caracter and loss data at receiver.

    Any sugestion ?

    To close antenna (Rx & Tx) can be a problem ?


    Thank for your help

    • Frank
    • October 14, 2014 at 2:11 am

    thanks for your tutorial.
    I have got some problems while I was trying it out.
    I am using xbee s2 module.
    In the CoolTerm I typed ATMY10, and it showed “error” instead of “OK”.
    Then I opened X-CTU, the value of MY was not changeable.
    what mode should I use for these two XBees?
    could you please help me out?

      • Brandon
      • December 12, 2014 at 8:57 am

      Hmmm, that’s definitely a strange issue. o-O; Generally, if the MY value is greyed out, the other ones should also be greyed out; the MY values is critical when configuring the XBees. What method are you using to connect the XBee to your computer running XCTU? If you’re not already, I would suggest using one of the XBee to USB adapters like the one in my post.

      Once you’re able to estabilish a solid connection to your PC, you’ll want to open up the X-CTU utility and make sure your XBees are configured something like this (assuming a 38,400 baud rate):

      XBee 1:

      ID: XXXX
      DL: 11
      MY: 10
      BD: 5

      XBee 2:

      ID: XXXX
      DL: 10
      MY: 11
      BD: 5

      Where ID: XXXX is any number that suits your fancy, like ID: 1234 or ID: 4211 – just make sure it’s the same for both XBees.

      Once you finish configuring them, connect the XBees to their corresponding devices and go into your terminal again to see if you’re able to communicate Make absolutely sure that on both ends, your devices are treating the XBees as a serial port running at 38,400 baud, or it won’t work (many terminals have a baud rate setting in them; make sure yours is set to 38,400 or you’ll just receive garbled and/or useless data).

      Hope this helps a little bit! ;D


    • kevenlaker
    • December 9, 2014 at 1:30 am

    You may need $11.9 Arduino Can-Bus Shield here:

    • Patrick Smit
    • October 19, 2015 at 11:48 pm

    Thank you for sharing this information.
    At the end you say: From here, you can do all sorts of things like wireless data collection, autonomous robots of mass destruction, etc.

    I am interested in the data collection part. Unfortunately I don’t know which programs I need to collect the data. I like to collect the data on my pc. All I can find is information using the XCTU terminal. But I think the XCTU terminal is just for testing puposses. I like to collect the data in Excel for example or in a database.

    Can you tell me how to do this?

  • For example, if there is a tall building or
    structure in Angry Birds, you should be inclined to attack the point where
    the weight rest’s on, or the weak point of the structure.
    Backpacks and over-the-shoulder personalized tote bags are another popular item on the back to school
    shopping list. In case you know somebody that is a fan of it who would be
    having their birthday bash in the near future, it’s only right that you simply
    organize for them an Angry Birds themed birthday event.

  • Fantastic web site. A lot of helpful info here. I’m
    sending it to a few friends ans additionally sharing in delicious.

    And of course, thank you on your effort!

Leave a Reply

Your email address will not be published.