How to use xbees with arduino

18,120 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

vjeamrhasty's picture

newbie

hello i'm new to arduino,

i have a problem on making the xbee s1 and arduino uno communicate, i'm using IO expansion shield and xbee shield in connecting the xbees to the arduino...

arduino wont load a sketch if the xbee is connected to it, it will load a sketch if no xbee is attached...

i really need some help... i ony got 20days left to finish my project... please help me..

kawal's picture

Hi, As i mentioned in the

Hi,

As i mentioned in the post you need to remove TX and RX connections while uploading code otherwise the code wont upload.I have never used xbee shield but i guess there is some kind of button or jumper that swtiches mode on xbee shield.

talasyn's picture

Arduino v 1.0.1?

Hi there :-)

I'm newish to arduino and VERY new to Xbee.  I've tried to use your examples to test out my xbee config and am having a little bit a problem.  It seems that the BYTE keyword is no longer supported.  I've tried a number of workarounds but am still having issues.  Would it be possible to post an arduino 1.0.x version of your code?

 

One change that I've tried, but its only reading 1 character at a time is:

SENDER

....

 

// remap values
 
Serial.write("254");     //SYNC char
Serial.write("200"); // This is just to test sending. 
.....
 
And in the RECEIVER I changed the datatype declaraction at the top to:
char incomingByte,pot;
 
If I look at the serial monitor on the receiver, I am getting the data 1 character at a time as in:
2
5
4
2
0
0
 
I'm thinking there has to be a better way than to create an array, etc on the receiving end.  I'm sure there is a better way to process the data as BYTEs again.
 
Any help would be great.
 
Thanks!
Todd
kawal's picture

Welcome Todd, I have not yet

Welcome Todd,

I have not yet used arduino IDE version 1.Give me a day or two to test the code on version 1 and come back with any changes if required and for the data recieiving part,you are recieiving the data correctly(one integer/char at a time).The code is meant to transmit data in that way.

// Kawal

simkas's picture

we have tried all the baud

we have tried all the baud rates and still nothing.. 

simkas's picture

(No subject)

kawal's picture

Setup is fine. Try different

Setup is fine.

Try different baud rates on xctu and see if it helps.

simkas's picture

Hey Kawal. We have gone

Hey Kawal. We have gone through some examples with arduino without any problems at all. It says that we have to press the reset button and hold it for 10 seconds, but afterwards it doesn't do what we tell it to. The main problem is that our arduino and xbee cannot connect, but xctu recognices our arduino but when we tell it to test it with a 19200 baud, it tells us to press the reset button and apply that we are dealing with a communication fail. 

Thanks a lot for your apply - we are greatfull for all the help that we can get as soon as possible. 

kawal's picture

Image

Please provide a picture of the setup you are using for configuring xbee.It's hard to guess what's wrong without a picture.

simkas's picture

Hey there. We are a couple of

Hey there. We are a couple of guys trying to figure out how to make xbee and arduino "talk" by using your instructions but without any luck. It appears that we don't get any inf-files when we downloaded arduino. When using the xctu software, the software responses that it is unable to connect with our arduino uno r3. The xbee that we are using is a model s1. We don't know what is wrong and we are desperate! Your help would mean the world to us!

Add a new comment

Login or register to post comments

New to community of robots ?

Ratings

0

Advertisement