XBee/XBee Pro Wireless Communicate with Microcontroller

1.0 Introduction

The XBee and XBee Pro radio is made by Digi (formerly Maxstream) which is shipped with firmware implementing the IEEE 802.15.4 protocol. These modules use the IEEE 802.15.4 networking protocol for fast point-to-multipoint or peer-to-peer networking. However, the most different part between XBee and XBee Pro is they have different cover distance range for communicate with own module. XBee can be covers around 30m at indoor and 100m at outdoor. Inversely, XBee Pro can cover higher distance range than XBee which is 100m at indoor and 1500m at outdoor. Both devices that has a UART interface can connect directly from microcontroller to pins of RF Module (XBee/XBee Pro). Using UART interface, we can use this wireless devices to communicate between microcontroller to microcontroller (two PICs) or between PC to microcontroller. Here, we will discuss how to use two XBee/XBee Pros to interface with microcontroller and how to send command for configuration XBee/XBee Pro Module.  

2.0 Xbee/XBeePro hardware interface with microcontroller

Bear in mind, XBee is designed for 3.3V system, those use system will require extra work and component before XBee module could be embedded in the system. So that, we used SKXBEE (XBee Pro Starter Kit) as our example in this article. SKXBee can be used to interface for 5V microcontroller board (SK40C+PIC16F877A). Any microcontroller with UART peripheral can be used to interface with SKXBee. In this case, we have to connected XB_RX pin to PIC Transmitter pin TxD (RC6) and XB_TX pin to PIC Receiver pin RxD (RC7). However, the RESET button of SKXBee is optional case depend of user who would like the microcontroller to reset SKXBee during run time. For more detail, you can refer to SKXBee User Manual on Cytron website page (www.cytron.com.my).


The below figure has been shown as example hardware connection of SKXBee with SK40C+16F877A:



"The connection between PIC and SKXBee"

The configuration of UART is complete, SKXBee is ready for embedded wireless development. Trasmitting and receiving data require software or firmware development on particular microcontroller. Before develop software programming, we should know how to setting XBee Module Source address and Destination address, it is very important for us to transfer data by transparently from one XBee module (Source) to another XBee module (Destination).Two ways to setting ID address which is using X-CTU software or using programming code to send specified command for XBee ID address setting. We will discuss both ways in this article. In fact, user only need select one of both ways to do it.   

3.0 Using X-CTU software to setting Source address or Destination address.

X-CTU which is a computer based software to communicate with Xbee, to change configuration or Transmit/Receive data. Double click the “setup_x-ctu.exe” file to install this software.NOTE: Using X-CTU for address setting, SKXBee is no necessary connect to microcontroller before the setting process is done.

Launch the X-CTU Software and connect the USB cable from SKXBee to computer USB port (without connect to microcontroller). Select “PC Setting” tab–> select serial COM Port –>click “TEST/QUERY:

The box will show as figure below, click “OK” for close this box:

Basically, Xbee/XBee Pro has two command mode options which is AT Command Mode and API Command Mode. Here, we only use AT Command Mode. The figure below AT Command table is provided some commonly used for setting address:

All parameter are in Hexadecimal format. For more detail of Xbee/XBee Pro command, please refer to User Manual of XBee/XBee-Pro OEM RF Module.To send AT command and parameter, use syntax shown below:

NOTE: The failure to enter AT Command Mode is most commonly due to baud rate un-match. Ensure the “Baud” setting on the “PC Settings” tab matches the interface data rate of the Xbee Module. By default, the baud rate should be 9600bps.

The below example shown how to check the previous Destination ID address of Xbee module and how to change a new Destination ID address using “WRITE” command. To store the new value to non-volite (long term) memory, “ATWR or atwr” should be used in AT Command Mode.

For modified parameter values to persist in the module’s registry after a reset, changes must be saved to non-volatile memory using the “WR” (Write) command. Otherwise, parameters will be restored to previously saved values after reset. When a command is send to a module, the module will parse and execute the command.

Upon successful execution of a command, the module return an “OK” message. If execution of a command results in an error, module will return “ERROR” message. For setting two SKXbee Source address and Destination address for communicate each other, the command sending should same (parameter 1111 and 2222 can be change) as below figure:

Now, we can write the coding to send command for setting SKXBee Source address and Destination address. However, we have to refer the AT Command Mode table. The main reason is the command code exactly same with the code we have using X-CTU software for setting the parameter.

4.0 Using programming source code to setting Source address or Destination address.

If we want to use coding to send the command to SKXBee, we have to ensure the hardware connection between SKXBee to microcontroller is complete. It can refer back to the previous sub topic on 2.0 Xbee/XBee Pro hardware interface with microcontroller.

Due to this case, we are using SK40C and 40 pins PIC16877A to interface with SKXBee. UART data flow will be use for them to communicate each other. So that, we will initialize the UART configuration in programming code. This figure below has been shown as an example code:

[code lang=”c” highlight=””]void uart_init(void)
{
SPBRG=129; //set baud rate as 9600 baud
BRGH=1; //baud rate high speed option
TXEN=1; //enable transmission
TX9 =0; //8-bit transmission
RX9 =0; //8-bit reception
CREN=1; //enable reception
SPEN=1; //enable serial port
} [/code]

UART Transmit and Receive coding configuration:

[code lang=”c” highlight=””]
unsigned char uart_rec(void) //receive uart value
{
unsigned int rec_data;
while(RCIF==0); //wait for data
rec_data = RCREG;
return rec_data; //return the data received
}
void uart_send(unsigned char data)
{
while(TXIF==0); //only send the new data after
TXREG=data; //the previous data finish sent
}
void uart_str(const char *s)
{
while(*s)uart_send(*s++); // UART sending string
} [/code]

Now, we can write the coding to send command for setting SKXBee Source address and Destination address. However, we have to refer the AT Command Mode table. The main reason is the command code exactly same with the code we have using X-CTU software for setting the parameter.For initialize the Source address and Destination address to SKXBee1 module, we can write the coding

[code lang=”c” highlight=””]
void xbee_init(void)
{
uart_str("+++");//send command to enter XBee Pro command
mode delay_ms(200);// waiting for finish sending and XBee respond
uart_str("atmy1111");//send command to setting Source address
uart_send(0xD);// 0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atwr");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atdl2222");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
} [/code]

For initialize the Source address and Destination address to SKXBee2 module, we can write the coding in another microcontroller PIC;

[code lang=”c” highlight=””]
void xbee_init(void)
{
uart_str("+++");//send command to enter XBee Pro command mode
delay_ms(200);// waiting for finish sending and XBee respond
uart_str("atmy2222");//send command to setting Source address
uart_send(0xD);// 0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atdl1111");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200); uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
} [/code]

Using UIC00B programmer to loading source code into PIC16F877A SK40C:

When we have finished load the coding into PIC, we should press “RESET” button to ensure the address will store in Xbee/XBee Pro module. Otherwise, the Xbee/XBee Pro will store with previously address value.

NOTE: Please remember the Source Address and Destination address only need one time setting process for ID address, the Xbee/XBee Pro module will store the address in module. User no need to setting it every time when using Xbee/XBee module, except that user want to change a new Source address or Destination address to these RF module.

5.0 Conclusion:

XBee/XBee Pro is a user friendly RF module for wireless communication between two PICs or between a PIC and a computer. When we know how to use it with wisely, we can use this wireless device to control a robot or equipment, data collection, security system and etc. By the way, we have to know the command protocol for this RF module, then we can used and handle it easily with microcontroller or computer. Please do post your inquiry in our technical forum as we seldom check the comment section in tutorial site.

Here, we have prepared two project for you to refer (Source Code and Demo Video is included), please click the link below :

1. Joystick control Flexibot by using SKXBee wireless communicate

2. GUI control Flexibot by using SKXBee wireless communicate


Buy

, , , ,

Related Post

How to Make your Line Following Robot Faster?

Simple steps to control Stepper Motor using 2Amp Motor Driver Shield and CIKU

Using MDD10A with CIKU

Using Cytron XBee Shield

21 thoughts on “XBee/XBee Pro Wireless Communicate with Microcontroller

  1. Control of Proyect PR26 with Xbee and should Controlling Robot with Microcontroller and Xbee and send data.

  2. is that a must to connect the Xbee with micro-controller in order to use the xbee? [the zigbee is used in a greenhouse monitoring system]

  3. It depends on your requirement, XBee can be standalone wireless device. One of the example that we show is DIY project PR20, Standalone XBee.

  4. Hi, first of all thank you for making all this information.

    I was wondering how I could do to know (in a mesh network), which is the closest router to my ED. This obviously would ask from the PIC associated with ED. My goal is to transmit this information to C, by way of knowing that my ED area is located.

    thx 🙂

  5. I am trying to communicate between a computer and PIC16F877a. I have used two SKXbee with xbee S1. My issue is that i can receive at computer by my PIC but when i am transmitting from computer to PIC things are not working out for me.

  6. thanks a lot for this but i have a problem when trying it

    the code when i tray it with ” mikroC PRO for PIC” errors found

    like this instruction

    while(RCIF==0) can i use these instead if (uart1_data_ready())
    please can you help me with this
    i need it for my graduation project

  7. we do the project based on robot, so we need wireless communication , my question is “how to configure the smart phone to xbee module”?

  8. thank you for sharing.
    i am using xbee pro series 1 in my project . The task is to let the micro controller communicate with the pc. i simply set the pan id and transmitter and receiver id using x-ctu. now i want the controller to transmit some data to pc . Do i need to set the transmitter and receiver information again. i am confused with the controller part. ATMEGA16 is being used. Can anyone help me with that. I have negligible knowledge of the micro controller.
    I tested pc to pc chat using the xbees . it worked fine

  9. Xbee alone is transceiver, a combination of transmitter and receiver. A Xbee can transmit and receive. No extra configuration is needed since you have create the link using PC. Just use it. Please do continue the discussion in our technical forum.

  10. Hello everyone, I am currently working on my final year project that use Xbee S1. I also used Arduino Uno microcontroller which need to communicate with the PC. Can someone tell me how to get the coding or tutorial so that I can develop my project. Please help me. TQ

  11. helloe everyone, i want to ask, the all source code need to write in one file or each source code on one file

  12. hi.. i dont know how to program the pic 18f4550 to connect wiith xbee S2.. and I dont know how to setting the xbee use xctu .. can anyone help me?

  13. Hi, when I use the code given there, there’s a lot of error such undeclared identifier ‘uart_send’ in expression. what should I do

Leave a Reply

Your email address will not be published. Required fields are marked *