Configuring and using XBEE wireless modules

Posted: November 7, 2010 in Electronics, Tutorials
Tags: , ,

Xbees are some of the most powerful wireless modules you can find and they’re also very easy to configure and use. The only thing is they cost about Rs.1000 to Rs.2500 depending on the range and other parameters. If you’re like me and only bought the modules without the breakout boards or forgot to buy it, then you have to make the modules breadboard friendly to use it. I’ve made a PCB which converts the 2.0mm spacing of the pins of xbee to the normal DIP spacing of 2.54mm.

You can download the schematic and board files of eagle here. If you don’t have eagle you can download the layout in pdf format here.

You need to make two PCB’s, one for each module. If you don’t know how to make PCB’s check out my tutorial which shows you how to easily make PCB’s at home.

PCB’s after etching and drilling

Now take a berg strip and break it into 4 parts with 10 pins each and push the pins to one side as shown.

To do this just put the strips on the PCB and push them against the ground and they’ll go inside.

Now put the 2.0mm strips and solder everything. Remember that since this is a single sided PCB, you have to solder the berg strip from the bottom. This will seem a little awkward but it saves you the need for a double sided PCB.

So now that we’ve made a breadboard friendly module, it’s time to configure them.  Xbees come configured out of the box if you just want to use it as a UART serial link, they’re configured at 9600bps and their PAN(Personal Area Network) ID is set to 3332. Devices with same PAN ID can “see” each other i.e. they’re in the same network. But xbees can be used for so much more, e.g. you can directly use the built in ADC to sample values and send it directly without any microcontroller. There are also 8 digital I/O pins which you can configure as air wires.

Every other tutorial I’ve seen requires you to buy some sort of a board which will cost you anywhere from Rs1000 to Rs.2000. I’m going to show you how to hook up the xbee to a PC and configure settings like baud rate, PAN ID etc with just a serial port(Or a USB to serial converter) and some easily available components. Once you know how to change the settings you can fully exploit the capabilities of the module.

Before you begin to do connect anything, please note that the VCC on xbee is not 5V tolerant, so if you’ve given 5V to the VCC, you might’ve already fried it. Most users say that the input lines are also not 5V tolerant but it seems to work fine for me. Components required:

  1. Xbee wireless modules
  2. USB to serial converter(if you don’t have a serial port)
  3. IC 7404(Inverter)
  4. 5V regulator (7805)
  5. 3.3V regulator(LD33V or LM3940), alternately you can use a LM317T to get 3.3V which is what I did.

Software required

  1. X-CTU by digi

If you don’t have a serial port, then you’ll have to buy a USB to serial converter. It might say on the box of the converter that it is “fully RS-232 compatible”, but I’ve found that most converters are TTL compatible(RS-232 has signals going up to 25V, whereas TTL is 0 and 5V). This is a good thing, since you don’t have to use a level shifter such as MAX-232. If your converter is not TTL compatible, just substitute the 7404 with the MAX-232 along with the necessary components.

Make the circuit shown in the schematic on a breadboard and if you’re using an LM317, set it to around 3.1V by varying the 10K pot.

After connecting everything, make sure that the VCC on the Xbee pin is not above 3.3V, now place the xbee module in the socket and plug in the USB/Serial cable and open X-CTU.

Without changing any settings, press “Test/Query” and if everything is connected properly it should show the following message.

If it doesn’t check for the following

  1. Make sure there’s connection between all the points(Du-uh)
  2. Make sure you haven’t reversed RX/TX(TX of one should go to RX of other and vice versa) and the direction of the NOT gates.
  3. Make sure you’ve the correct drivers for the USB to serial converter and that you’ve selected the correct COM port.

Assuming you’ve fixed the problems, we’ll now begin “talking” with the xbee. The xbee modules use AT commands to distinguish between data and command signals. When you want to change or read any settings in the xbee, you first have to send “+++”, so that it knows that you want to enter command mode, it only stays in this mode for 10 seconds if you don’t send any command, after which you’ll have to resend “+++”.

Now go to the terminal tab of X-CTU, this provides a direct serial link between the PC and the XBEE.

Type in “+++” without quotes(Do not press enter), it should return “OK”

Now we’ll check the baud rate of the module Type in “ATBD” and press enter, it should return ‘3’ which means 9600bps according to the datasheet.
0 – 7 (standard baud rates)
0 = 1200 bps
1 = 2400
2 = 4800
3 = 9600
4 = 19200
5 = 38400
6 = 57600
7 = 115200
0x80 – 0x3D090
(non-standard baud rates up to
250 Kbps)

All the commands can be found in the datasheet but remember that commands should be prefixed with AT. E.g. to check the PAN ID, send “ATID”.

Writing a value – We’ll now change the PAN ID of the module. To do this, type the following
+++OK
atid //press enter
3332
atid2222 //press enter
OK
Atid //press enter
2222
atwr
OK

If you don’t type “atwr”, the settings will not be saved to non-volatile memory and they’ll be lost if you power off the device.

You can also change these settings by going to the modem configuration tab and writing the values directly in the text boxes. Before doing this you should press “read”, upon which it’ll ask you that it has to download the latest firmware, click ok and be really patient, cuz it’ll take a really long time(mine took around 15mins)

After this you can change any setting and press write and they’ll be saved. You can even upgrade your xbee’s firmware to the newest one available. Just select the last number from the drop down list of “version” and tick “Always update firmware” and press write. Untick it after you’re done.

Now that you know how to configure the module, I’ll show you how to interface a PC and an AVR microcontroller to control an LED using serial communication.

Components for the AVR side:
1.ATMEGA8(anything with a USART will do)
2.Xbee module
3.8MHz crystal.
4.5V and 3.3V voltage regulators
5.LED

Keep the circuit on the PC side as it is. This will be the transmitter side. Make the receiver circuit as shown on another breadboard.  

Code for AVR:
#define F_CPU 8000000UL
#include<util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUDRATE 9600
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)

void USART_Init(void)
{
//Set baud rate
UBRRL=(uint8_t)UBRRVAL; //low byte
UBRRH=(UBRRVAL>>8); //high byte
//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|
(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
//Enable Transmitter and Receiver and Interrupt on receive complete
UCSRB=(1<<RXEN)|(1<<RXCIE);
//enable global interrupts
}

uint8_t USART_vReceiveByte(void)
{
// Wait until a byte has been received
while((UCSRA&(1<<RXC)) == 0);
// Return received data
return UDR;
}

ISR(USART_RXC_vect)
{
UDR=0;//clear UART buffer
char data;
data=USART_vReceiveByte();//receive data
if(data=='a')
PORTC=0x01;//turn on LED if recieved character is 'a'
else PORTC=0x00;}

int main(void)
{

DDRC=0xFF;//make PORTC output
PORTC=0x00;//set all outputs to 0
sei();//enable global interrupts
USART_Init();
while(1)
{

}



I think the code is self-explanatory, I’ve just set up the AVR to toggle the LED on and off depending on the ascii character received. The fuse settings to configure the ATMEGA8 for the external crystal are

hFuse: C9

lFuse: FF

It might be different if you’re using other microcontrollers. Compile the code in your favorite compiler and upload it to the microcontroller.

After setting up everything, power  both sides on and go to the terminal in X-CTU and type ‘a’ and the LED should glow and if you press any other key, it should turn off. You can do this in hyper terminal too if you have XP.  Make sure both modules are set at the same BAUD rate and PAN ID.

This was just a simple example to show you how to get started with the xbee modules. There are other wireless modules available like the 434Mhz  FSK modules(Rs 300 per pair) but I’ve found they’re too slow for my applications, that’s why I switched over to Xbee.

Hope you found the tutorial helpful. Please leave any comments/Questions below.

Comments
  1. suraj says:

    awesome dude 🙂

  2. newbie says:

    what fullname for IC 7404 and the voltage used for the IC?

    thanks you!!

  3. newbie says:

    thanks you bro
    one more question , hehe

    how about the baudrate xbee
    let say, my uC set baudrate 2400
    can the xbee set baudrate 2400 too?

    thanks

    • amithmv says:

      Yup… You can set it to any of the standard values, you can do it through either AT-commands or using x-ctu gui…

      • Werner Smidt says:

        Hi Amithmv
        Your post is sone days old but but ma be you can help me. I want to change the XBee baud rate from default 9600 to 2400. You say and the Parallax manual “getting started with XBee” say the same. I want to change the baud rate via the micro controller by using the AT commands. My question what is the first baud rate I have to select? The default (9600)? Or the new one (2400)? Or a mix between old and new one?
        The sequence is
        +++ ‘enter into command mode
        ATRE cr ‘reset to factory default
        ATGT 3 cr ‘Set low Guard time
        ATBD 1 cr ‘ Change Baud rate to 2400
        ATCN cr ‘Exit command mode

        I have tried to send the AT commands with 9600 Bauds and 2400 from the micro controller without results. After disconnect from power XBee go back to default mode and I can communicate with 9600 (monitoring with a scope).
        Do you have any idea? I google since two weeks an I found nothing helpful.
        Best regards
        Werner

  4. newbie says:

    hello, i already build a circuit xbee interfacing pc by using 7404
    but the result :
    unable communication the modem.
    i also buy a new usb serial converted

    http://www.lelong.com.my/vztec-usb-rs232-serial-9pin-converter-I898518-2007-01-Sale-I.htm

    i think maybe the promble because the usb converted cable

    i hope u can help me

    thanks

    • suraj says:

      its costly… but may work for my application for the train.. wat do you think.. let me know if know a cheaper one

  5. vk_cnc says:

    great
    n now how to communicating xbee to avr?

  6. Norsang Lama says:

    the schematic and board file is missing here…can you again upload it here…

    regards

  7. Hassan says:

    Hi. I want to use it for Smart energy metering. Is this model suitable?
    And my major query is that is dev kit required or they way you did is fine. IS software free?
    I will use 8052 or pic maybe. Suggestions please and suggest zigbee model too.

    • amithmv says:

      Well if you want point to point communication then you should go with series 1 xbee, and if you want mesh network then go for series 2. The development kit not required if you are sure of what you’re doing cuz you can easily destroy them if you give the wrong voltage. And yes the software is free. If you want long range communication then you’ll have to go for xbee pro which is more expensive.

  8. Hassan says:

    HI, i want to you use this module in smart metering. Suggestions and is dev kit required or the way you did is fine.

  9. Joe says:

    Hi,
    I already build this circuit, with differences, that I use 3,3V logic for AVR to, but it isnt working. Are you any idea what is wrong? I use the same C-code, and use Atmega8L chip. I going to change it for another atmega8. Last question: have you write another xbee projects?

  10. Thank you very much for helping me set up my xbee.
    One thing, variables should not be initialised inside ISR and the variable used inside ISR should be volatile.

  11. Kapil Sharma says:

    Hi Amith, thanks for this informative tutorial.

    Well, I am facing a problem with my Xbees. I have configured my 2 XBee module for point to point communication (same PANID and Baud Rate). However, I am not been able to exchange data between the two. I am simply testing the devices by sending data through hyperterminal.

    One quite peculiar problem with one of my Xbee is that it is not responding to AT commands. Infact, it doesn’t switch to command mode when I fire “+++”(“OK” is not returned). But, for the fact, I am being able to Test/Query the Xbee using XCTU. The seller from which I purchased my Xbees says that he had sent me the devices after configuring for point to point.

    I will highly appreciate your help in this regard.

    Thanks & Regards,
    Kapil Sharma

  12. silky says:

    haii…
    may i ask u a question?
    I want to use it for street lighting using powerline communication. Is this model suitable for outdoor project?

  13. Dawayne says:

    I have the atmega1284p could you help with the translation of proper registers to use as this one has two USART ports

  14. hanumesh13 says:

    hi,
    Want to know whether xbee series 2 has an inbuilt micro-controller and ADC. I have setup a co-ordinator and a end device using x-ctu. I want end device to be connected to some sensors( temp, smoke,etc) . The question is how to connect this end device to sensors .

    If i have to use a micro-controller then , how can i interface xbee ( end device ) and a micro-controller.

  15. Thien Phan says:

    That helps a lot for my project. Awesome !!

  16. maheen says:

    sir, i wanna use xbee in my project and wants microcontroller to transmit signals to pc.. can u help me with that

  17. Rajuyareddy says:

    Anyone knows how to interface xbee with dsp TMS320f2812. If yes ,can you give me the code.

  18. Bhuvan says:

    Hi Amith
    Have you tried making 4channel controller(Controlling RC car and airplane) with XBEE, If so please send me the tutorial link. Or is there any alternative for that.

    Thanks
    Bhuvan

  19. izzatulain says:

    hi, i wanna to ask u about xbee. i’m using pic16F877A. so, how can i connct it with xbee? i had connect the pic with xbee, but, the xctu software doesen’t detect the xbee..

Leave a reply to suraj Cancel reply