mBed + xBee

XBee Communication

By: Vlad Cazan

Two XBees and One USB Explorer

I had two XBee modules that I really wanted to use with the mbed. These are the standard OEM RF Modules one with an antenna and one with a ANT chip. For one of the chips I will be using the Xbee Explorer USB from Sparkfun. http://www.sparkfun.com/commerce/product_info.php?products_id=8687

Free Image Hosting at www.ImageShack.us Free Image Hosting at www.ImageShack.us

According to the datasheet (and Simon) of the Xbee modules, the following should be used as the pin layout. Main thing we are looking for first is power, ground, serial rx + tx, and a digital out for the reset.

1 VCC -------------- 3.3v
2 DOUT ------------- mbed serial rx (e.g. p10)
3 DIN -------------- mbed serial tx (e.g. p9)
4 not connected
5 RESET ------------ mbed digital out (e.g p11)
6-9 not connected
10 GND ------------- 0v/GND
11-20 not connected  
It is important to use the 3.3v pin (VOUT) and not the 5.0V (VU) or you might damage your Xbee module. Another important consideration is that the Xbee module has 2mm pin headers which will not fix on a standard breadboard. For this experiment I will be using male to female wiring. Sparkfun does have a breakout board which would work much better for a more permanent solution. http://www.sparkfun.com/commerce/product_info.php?products_id=8276 http://www.sparkfun.com/commerce/images/products/XBeeBreakout-03-L_i_ma.jpg

I have wired the pins accordingly using pin 9, 10 and 11 as the digital in, out and reset for the xbee. The second XBee is connected to a USB breakout board from sparkfun.


Using Simon's example from the forum I edited the code so that one xBee would "talk" and the other one would "listen". Since I still need power for the xbee unit the USB cable is still plugged into the computer but the communication is happening through the air. Using any terminal application if you type something from one mbed, the other will display. Nothing fancy, just a proof of concept. Will try creating something more interesting soon.

 * XBee Example Test
 * A test application that demonstrates the ability
 * of transmitting serial data via an XBee module with
 * an mbed microprocesor.
 * By: Vlad Cazan
 * Date: Tuesday, September 29th 2009

#include "mbed.h"

Serial xbee1(p9, p10); //Creates a variable for serial comunication through pin 9 and 10

DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset

DigitalOut myled(LED3);//Create variable for Led 3 on the mbed
DigitalOut myled2(LED4);//Create variable for Led 4 on the mbed

Serial pc(USBTX, USBRX);//Opens up serial communication through the USB port via the computer

int main() {
    rst1 = 0; //Set reset pin to 0
    myled = 0;//Set LED3 to 0
    myled2= 0;//Set LED4 to 0
    wait_ms(1);//Wait at least one millisecond
    rst1 = 1;//Set reset pin to 1
    wait_ms(1);//Wait another millisecond

    while (1) {//Neverending Loop
        if (pc.readable()) {//Checking for serial comminication
            myled = 0; //Turn Led 3 Off
            xbee1.putc(pc.getc()); //XBee write whatever the PC is sending
            myled = 1; //Turn Led 3 on for succcessfull communication



Two XBees no USB Explorer

For the previous example I used the USB Explorer to mount the second XBee. For this example I have connected both XBee's to the mbed. Again the wiring is very easy, you only need 3 pins per each module and power and ground.

This code example takes a character from the terminal and adds 1 to its ascii value. Again not very useful, but proof it works. Imagin having two mbed's, they can now talk to each other wirelessly for up to 100 feet!


// basic xbee example
// - take chars from the terminal, push them out xbee1
// - listen on xbee2, and print value + 1 to terminal

#include "mbed.h"

Serial xbee1(p9, p10);
DigitalOut rst1(p11);

Serial xbee2(p13, p14);
DigitalOut rst2(p15);

Serial pc(USBTX, USBRX);

int main() {

    // reset the xbees (at least 200ns)
    rst1 = 0;
    rst2 = 0;
    rst1 = 1;
    rst2 = 1;
    while(1) {
        if(pc.readable()) {
        if(xbee2.readable()) {
            pc.putc(xbee2.getc() + 1);



18 Jan 2010

Do you have any thoughts on the best xbee modules to buy for development purposes?  I am confused by the wide range of available options but my initial thinking was to buy two medium / high power with rpsma connections as this seemed to offer the best flexibility / price trade off ie: could be used to develop both short range and longer range projects. eg: http://www.sparkfun.com/commerce/product_info.php?products_id=8768. 

Is there any reason to prefer Series 1 over series 2.5?

Thanks for the write-up.



21 Jan 2010

It all depends on your project. One difference is between a wired antena and a flat antena, if your project is small then you might need to go for the flat antenna which is more expensive, but the wired antenna ones work just as well. Next you have to worry about the distance. If you are just looking to play around and maybe drive a few motors then you should use this model: http://www.sparkfun.com/commerce/product_info.php?products_id=8664

It is very easy to use and works great, and it is the same one I have used in my example.

16 Mar 2010

More Details that Might Help...

Some details might help people with the mbed-to-Xbee example:

In the first sample program above, the characters you type in go from the mbed module to an XBee module, through the air to another XBee module attached to a USB board. I'll assume you have an mbed module connected as noted above, to an XBee module.

I used a Win XP PC (SP2).

1. Parallax (www.parallax.com) also had a USB-XBee adapter, part number: 32400. $US 24.99. I received one within three days.

2. After you have the USB-XBee adapter set up, download the X-CTU software from Digi International at http://www.digi.com/support/productdetl.jsp?pid=3352&osvid=0&s=316&tp=1. Install it. Ignore any Windows warning displays.

3. Also download the document, XCTU Configuration & Test Utility Software User Guide at: http://ftp1.digi.com/support/documentation/90001003_A.pdf.

You can use the X-CTU software to configure XBee modules, check communications between a Windows PC and the XBee module, and also use the X-CTU Terminal window to send information to transmit and operational commands to the USB-XBee module and the Terminal window will also display command responses and information from the XBee module. If necessary, use Windows Settings-->Control Panel-->Hardware-->Device Manager to determine the COM port number assigned to the USB-XBee module. Look under "Ports (COM & LPT)," not under "Universal Serial Bus controllers."

You'll use this COM-port number in the X-CTU "PC Settings" window to select the proper COM port for communications with the XBee module. The default rate for an XBee module is 9600 bits/sec, 8 data bits, no parity, one stop bit. Choose None for Flow Control.

Click on the Test/Query button on the "PC Settings" to ensure your XBee module on the small USB card can communicate with your PC. You should see a message that indicates you have communications, the XBee model, and the firmware version.

4. Unfortunately, the online mbed compiler does not have a window that displays debug information sent from an mbed module to a PC with a printf() or putc() command. The compiler sees the mbed module only as a simple USB memory stick and nothing more. You must establish SERIAL communications with an mbed module for the software shown above to work.

Go to the mbed online mbed Handbook and locate the section "USB Serial" on the opening page. Click on "Windows Driver" to go to the page that explains how to download and install the mbed-USB driver that lets a PC terminal program such as HyperTerminal communicate serially with an mbed module through a USB connection. Connect your mbed board to your PC. Find HyperTerminal at: Programs-->Accessories-->Communications--HyperTerminal. (I never found a version of TeraTerm.)

5. After you install the Windows serial driver, you should find a new serial port in the Device Manager. Now, when you use HyperTerminal, set it to use this port number, COM[number] as the serial port. Again, set the bit rate to 9600 bits/sec, 8 data bits, no parity, one stop bit.

6. Compile the first test program listed above and download it to the mbed module. Switch to the Terminal view in the X-CTU window. If HyperTerminal is not running, start it and ensure you still have the proper COM port and communication settings.

With the code running in the mbed module, when you type a character in the HyperTerminal window (it will not display in this window), you should see it print in RED in the X-CTU Terminal view.

When you first start (or reset) the mbed code, LED3 will be off. As soon as the mbed module receives its first serial character from HyperTerminal, LED3 turns on and remains on. The main LED near the USB connector will flash for each character received. On the Parallax XBee-USB board, a blue LED turns on when its XBee receiver is active and off during "quite" periods.

If you run into problems, create a small program that will run in the mbed module and transmit ASCII characters to the serial port that connects to the XBee module. The X-CTU Terminal window should display them in red. If YOU type information when the Terminal view is active, the characters go TO the XBee module on the USB adapter and appear in BLUE. The mbed code in the first example program above does not use this information.

This ASCII test lets you determine whether or not the USB-XBee module can receive the ASCII characters sent by the mbed module. The X-CTU Terminal view should display the characters sent by the mbed module. Printing ASCII characters range between 33 and 126 decimal. I ran this type of test before I tried to use HyperTerminal to communicate with the mbed board and its attached XBee board. --Jon Titus, Herriman, UT 3-16-2010

01 Dec 2010

I have trouble with a Xbee Pro 900 DigiMesh. I followed the instructions as per above the only difference is that I used the serial on p13, p14 and reset on p15. The XBee module stops transmitting after the first 20 -30 characters sent.

The only way to make it work afterwards is to unplug the module for several hours before attempting again. I tested 3 XBee modules with the same result.

Did anyone encounter similar issue?

01 Sep 2011 . Edited: 07 Sep 2011

It does not work for me, is there anything that needs to be done b4 the transittion? Such as includes XBee library? :(



I have ordered an XBee USB Explorer with believe that it will help.

It did help and I managed to run both codes. The XB24-ZB only work if one XBee is Coordinator AT and the other End Device AT. This site reccommends API but it does not work for me. Perhaps I don't have the right settings.



06 Oct 2011

would you take a photo show how to connect 2 XBEE and mbed ?


13 Dec 2011

i'm trying to utilize the D+ and D- pins to communicate with a local Xbee unit's usb cord and port to read the id number on a remote Xbee.  Can someone please help me out? thanks

26 Aug 2013

I have an Xbee connected to one ARM7 controller and have a seperate xbee connected to a second ARM7 controller. The idea is to communicate between one system, that is controller 1 sends in data to xbee 1 which in turn sends data wirelessley to xbee 2 which sends data to controller 2. Can someone help with psudo code for both Arm processors please. Thank you kindly

18 Oct 2015 . Edited: 18 Oct 2015
I am attempting a simple two-xbee, two-mbed system where I send a byte from one mbed via a connected xbee to a second xbee connected to a second mbed. The xbees are the S1 type. I modified Vlad Cazan's code slightly as shown below and placed this code on both mbed's. The xbees were placed on a Sparfun USB explorer board and connected as described in the example. I first tested the code by connecting physical wires between the two mbed's and all worked as expected. The "success" is simply that the two LEDs flash as expected on both mbeds upon sending and receiving a byte. I cant seem to get this to work with the xbees in the loop -- I see no evidence that the bytes are being transmitted. I have set up the S1 xbee devices using Digi's XCTU utility: ID=2332, MY=1/2, DL=2/1 and confirmed the settings by communicating between the xbees using the XCTU terminal utility. Any ideas would be appreciated. <> include "mbed.h" Serial xbee(p9, p10); //Creates a variable for serial comunication through pin 9 and 10 DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset DigitalOut myled1(LED3);//Create variable for Led 3 on the mbed DigitalOut myled2(LED4);//Create variable for Led 4 on the mbed void ReceiveXbee() { unsigned char c = xbee.getc(); myled1 = !myled1; } int main() { xbee.attach(&ReceiveXbee); xbee.baud(9600); rst1 = 0; //Set reset pin to 0 myled1 = 0;//Set LED3 to 0 myled2= 0;//Set LED4 to 0 wait_ms(1);//Wait at least one millisecond rst1 = 1;//Set reset pin to 1 wait_ms(1);//Wait another millisecond while (1) { wait(0.50); xbee.putc(0x55); wait(0.50); xbee.putc(0xAA); myled2 = !myled2; } } <>

You need to log in to post a comment