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:

ID: XXXX
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):

ID: XXXX
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.

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

 

Comments

    • 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 ?

    Email:
    paquetalain2gmail.com

    Thank for your help

  • If you wish for to grow your familiarity simply keep visiting this site and be updated with the
    newest information posted here.

Leave a Reply

Your email address will not be published.