How to use xbees with arduino

18,250 Views
How to use xbees with arduino

Want to communicate between two arduinos wirelessly ? Heres a simple tutorial on how to use xbees for communicating between two arduinos.

Components you'll need

 2 X arduino uno

 2 X  xbee series 1

 2 X sparkfun xbee explorer regulated

 1 X sparkfun xbee usb board(optional,you can use arduino to configure xbees instead)

In this method i'll be using arduino to configure xbees.

How ? 
When you remove the microcontroller from your arduino board then it works just as an FTDI board.

So your first step would be to gently remove the microcontroller from one of your arduino boards.

After that  you need to connect only 4 wires to the xbee explorer regulated.Check the image below for connections

 

Black wire is for ground connection

Red wire is for  Vcc(5V).

Yellow wire goes from TX on arduino to DOUT on xbee explorer regulated.

Orange wire goes from RX on arduino to DIN on xbee explorer regulated.

Note : This connection is only for configuring xbees.

Now download XCTU Software.Unfortunately XCTU is only available for Windows and that too 32 -Bit version.

But dont worry if you have a 64-bit windows OS as i have personally tried that software on 64-bit OS and it worked properly.

After installing the software follow these steps

Configuring first xbee

Open the software.You'll get a dialog box like this

As we are using arduino's FTDI chip for configuring xbee module you will see a COM port on the name of arduino uno or whichever arduino you are using.If you are using Xbee explorer USB then you will see something like USB COM port.

When you will use the software for first time then the baud rate will be set to 9600 by default.You can change this as per your needs.

For now set it to 19200.

Now click on Test/Query button and if your xbee is properly connected to arduino and in a working condition then it will return a OK message as in below image

Now click on Modem Configurations tab.You will get this

Click on read button or if you know what model you are using then you can simply select the modem,function set.

But it is recommended to read the modem config(much easier).

After clicking on read button you will get

Then select the latest version from the drop down list.

After that in this part you need to set only 5 values which are marked with a red rectangle.

PAN ID - The PAN ID for both the xbees should be same otherwise your xbees wont communicate with each other.You can set it to your choice(4 digit no.)

For now set it to 3137

DH - Set it to 0

DL - Set it to 10

MY - SEt it to 11

Now scroll down and set the baud rate to 19200.The baud rate in the first step and at this step should be same.

Thats it .Now click on Write button next to the Read button.

You'll get a confirmation message(if everything goes right) at the bottom of the window and you're done !!

Configuring second Xbee

For configuring the second xbee you need to follow the exacts steps as for first xbee except there are few changes as :

PAN ID will be same as for first xbee i.e. 3137

DH  will also be same .i.e. 0

DL - Set it to 11

MY - Set it to 10

Yes the DL and MY values are swaped for second xbee.

Thats it you are done with configuring  your both xbees.

Now put your microntroller back in your arduino.Please dont put it in the wrong way.DOUBLE CHECK before powering.

Connecting the xbees to arduino for communicating.

For this you will need two arduinos.

Connect one xbee to one arduino and the another xbee to another arduino in the same way as shown below.

TX on arduino to DIN on xbee explorer.

RX on arduino to DOUT on xbee explorer.

GND to GND

VCC to VCC(5v)

Note: While configuring we connected TX to DOUT and RX to DIN and for communicating we connected TX to DIN and RX to DOUT.

Now your xbees are ready to use !!

Now let us learn how to actually communicate between two arduinos.

For testing connect a LED to arduino (this will be your reciever) and connect a potentiometer to arduino (this will be your transmitter).

 

Code 

Sender


//Community of Robots
// Sample code for sending potentiometer values

int val;
int new_val;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
val = analogRead(0);

// remap values
new_val = map(val, 0, 1023, 253, 0); 

Serial.print(254, BYTE);     //SYNC char
Serial.print(new_val, BYTE);
delay(150);
}

 


 

Reciever


// RECIEVER
byte incomingByte,pot;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);

pinMode (5, OUTPUT);  // set led pin 5 to output
delay(1000);
}

void loop() {

 if (Serial.available()) {   // is there any information available on serial port ?
incomingByte = Serial.read(); // assign bytes to the variable ‘incomingByte’
Serial.print(int(incomingByte));

if ((int(incomingByte) == 254)) {
pot = Serial.read();
Serial.print("Pot = ");
Serial.print(int(pot)); //print the values of pot on serial monitor
}
}
analogWrite (5, pot); //pwm led according to pot values
}

I'll upload a video showing this soon.

NOTE: While uploading the code(sketch) to your arduino make sure you remove connections on both the Tx and Rx pin otherwise you will get an error.After uploading the code successfully connect the Tx and Rx again.

 Thats it !!

Comments and corrections are always appreciated.

Thanks for reading.

Comments

kawal's picture

Xbee

Hello Simkas,

I am assuming that you have configured your arduino uno on your system and have atleast tested that arduino communicates with your system(pc or laptop) by uploading blink example from arduino IDE.

Now,

What exact error xctu is giving and on doing what? 

 

Add a new comment

Login or register to post comments

New to community of robots ?

Ratings

0

Advertisement