Xbee module. with PIC

, Posted by ADMIN at 10:07 AM

Xbee module.

Several months ago I bought a 434Mhz transmiter and a receiver planed to make a wireless project, but I couldn't manage to make them work. Later on, I gave up for those moudels and find other way, then I found Xbee which has several good features including Analog-to-digital conversion, low power consumsion and free testing and configuration software. I purchesed 2 Xbee ZigBee OEM RF modules but not a development kit, becasue I want to develop on my breadbord. but I found that the Xbee pin is not fit into my breadboard!! Now I understand why there are Xbee Breakout board in electronics stores.

After put those 2 Xbee into my own breakout board, I found that a transparent mode is very easy to use because they don't need any configuration. I just direct connect Data OUT pin to PIC's RX pin and Data IN to PIC's TX pin and provide 3.3V supply for Xbee. They are amazing replace USART hard-wire between 2 PICs.

For this example, I made wireless thermometer by taking adventage of Xbee ADC, convert voltage value form LM34 temperature sensor and send back to host to calculate and display temperature.

First of all, I need to config 2 Xbee, one is a sender and the other is a receiver.

ID = 0x3456 'PAN ID
ID = 0x3456 'PAN ID
MY = 1 'my address
MY = 2 'my address
DL = 2 'destination address
DL = 1 'destsination address
D0 = 2 'D0 is analog
IU = 1 ' I/O output enable
IR = 0x3E8 'sample rate every 1 second
WR ' save configuration
IT = 1 'sample before transmit 1 time

WR 'save configuration  


To setup those configuration, for those who have an Xbee's development board, you can use X- CTU program config all those parameters. but I don't have a development board, so I wrote a simple program and let PIC config the Xbee. I use my EASYPIC3 with PIC16F877A on board, connect RS232 port to my PC and then connect PIC's TX pin to Xbee's Data IN pin and PIC's RX pin to Xbee's Dataout pin. Program following below into microcontroller, open USART terminal program under Tools, click Connect and then press Reset button to start program. If all responds in UART terminal window are "OK", the Xbee has been configed. Change setting for receiver and redo again with another Xbee.

char buff = 0;

void interrupt(){
buff = RCREG;
TXREG = buff;

void Print_str(char *txt){
while(*txt) Usart_Write(*(txt++));
void delay100(){

void main(){
PIE1.RCIE = 1;

Usart_init(9600); // initail USART

//Code for sender
Print_str("+++"); //enter AT command mode
Print_str("ATID3456"); //PAN ID
Print_str("ATMY1"); //my address 1
Print_str("ATDL2"); //destination address 2
Print_str("ATD02"); //input 0 in analog mode
Print_str("ATIR3E8"); //sample rate 1 second (hex 3E8)
Print_str("ATIT1"); //samples before transmit 1
Print_str("ATWR"); //write settings to firmware


//Code for receiver

Print_str("ATID3456"); //PAN ID
Print_str("ATMY02"); //my address 2
Print_str("ATDL01"); //destination address 1
Print_str("ATWR"); //write settings to firmware

The schematic.

Onec both Xbees-- the sender and the receiver, have been configed. Connect LM34 to D0 pin and provide 1.28V reference voltage to Vref pin as figure shown below.

For recceiver, I use my EASYPIC3 board with PIC16F877A @ 8MHz clock. Provide 3.3V for Xbee and connection shown below.

Xbee UART Data Out.

Figure show below is received data.

Data come in framed using API structure, the packet can be break down as:

  • 7E Start delimiter.
  • 00 0A Length bytes.
  • 83 API identifier value byte.
  • 00 01 Source address bytes.
  • 20 RSSI value byte.
  • 00 Option byte.
  • 01 Sample quantity byte.
  • 02 00 Channel indicator.
  • 02 A2 Sample data ADC0
  • B4 Check sum.

For this example, the temperature is calculate form byte 12 and 13. And because it's pretty much the same as my previous LM34 project, so I'm not show C code here.

Firmware Upgrade .

I found a great web site to upgrade Xbee Firmware , that use a few component. I did follow step by step from that site and it work well, so I built one for my own to upgrade and config my Xbee.

API frame packet .

The default config of the Xbee is to operate in Transparent mode, the Xbee act as a serial line replacement. By set AP = 1, the Xbee operates in API (Application Programming Interface) mode which is capabillties for networking application.

When operate in API mode, all data entering and leaving the module is contained frame that define operation or event in the module. To perform 16-bit address sending data in API mode, you have to follow frame structure shown below.

The Length is a number of byte of Frame Data not include Checksum byte. Checksum byte can be calculate by 0xFF - sum of all data in Frame Data.

Example code .

 * Project name:
     Xbee's API frame structure example
 * Copyright:
      Nicholas Sirirak
 * Description:

 * Test configuration:
     MCU:             PIC18F2620
     Dev.Board:       -
     Oscillator:      HS, 16.0000 MHz
     Ext. Modules:    -
     SW:              mikroC PRO V1.65

#define _CTS  PORTC.F0
#define _RTS  LATC.F1

#define Delimeter    0x7E    
// start dilimeter
#define DH    00             // MSB destination id
#define DL    01             // LSB destination id
#define TX_request_16   1    //API identifier
#define Frame_id        0    // frame id

// Performs API packet data
void API_frame_send(char *str){
     char checksum = 0;
     char count = 0;
     while(*str != 0){
     checksum += *str;              //sum all text
     checksum += TX_request_16;     //API identifier
     checksum += Frame_id;          //frame id
     checksum += DH;                //MSB destination address
     checksum += DL;                //LSE destination address
     checksum += 1;                 //Option byte
     checksum = 0xFF - checksum;
str -= count;                  //reset pointer
     count += 5;                    //add 5 byte to counter

     UART1_write(Delimeter); // Start delimiter
     UART1_write(0);         // MSB length byte
     UART1_write(count);     // LSB length byte, delimiter and check sum is not include
     UART1_write(TX_request_16);    // API identifier
     UART1_write(Frame_id);    // frame id
     UART1_write(DH);    // MSB destination id
     UART1_write(DL);    // LSB destination id
     UART1_write(1);    // option byte
     while(*str != 0){
// MAIN    MAIN     MAIN
void main() {
     TRISB = 0;
     TRISC = 1;
     ADCON1 = 0x0F;   // all digital I/O
     _RTS = 0;
        API_frame_send("API structure test");

X-CTU Received data .


With Regards,

sms communication,
Tanjore -1.

mobile :9944 186 173        

Currently have 2 comments:

  1. Anonymous says:

    I was extremely pleased to uncover this great site.
    I wanted to thank you for your time for this particularly fantastic read!
    ! I definitely really liked every part of it and I have you
    saved as a favorite to see new information on your web site.

    My web page;

  1. Anonymous says:

    Thanks for a marvelous posting! I definitely enjoyed reading
    it, you're a great author. I will be sure to bookmark your blog and
    will often come back someday. I want to encourage you to definitely continue your great writing, have
    a nice evening!

    Look into my homepage :: ()