XBee Example
XBee Example
Communicate with
Microcontroller
Published by: pheysia on July 30, 2011.
26
Share
Tweet
2
Share
Share
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:
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.
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:
NOTE: The
failure to enter AT Command Mode is most commonly due to baud rate unmatch. 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 modules 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:
void uart_init(void)
rec_data = RCREG;
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 in void main() code as:
void xbee_init(void)
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);
For initialize the Source address and Destination address to SKXBee2 module, we can
write the coding in another microcontroller PIC source code void main() as:
void xbee_init(void)
delay_ms(200);
delay_ms(200);
delay_ms(200);
delay_ms(200);</span>
delay_ms(200);</span>
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.
7.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.