Call me blind but there just isn’t an easy to find tutorial to understand how to use XBee wireless modules with your Arduino micro controllers. I imagine it is because once you realize how simple it is, you wouldn’t think there would need to be a tutorial. Well I have had enough people ask me about this, so I figured I would post a demo. Over the couple years I have been using Arduino’s, I have embedded these wireless modules to many of my projects and found out many interesting quirks, mostly from trial and error. In this post I will be showing how to wirelessly control your Arduino via two XBee modules.
Overview
The goal of this post is to setup point to point wireless communication between your Arduino and your computer. The Arduino will be remotely controlled via Serial commands sent via the XBee modules.
Hardware Used
In this demo I am using the following setup,
- Arduino compatible micro controller – http://www.arduino.cc/
- XBee Explorer Regulated – http://www.sparkfun.com/products/9132
- XBee Explorer USB – http://www.sparkfun.com/products/8687
- Small servo – http://www.sparkfun.com/products/9065
- 2 x XBee 1mW Chip Antenna (Any XBee, these are just the cheap ones) – http://www.sparkfun.com/products/8664
Part 1 – Putting it all Together
As with most Arduino projects, the first thing we need to do is wire up our project. Thankfully, because we are using SparkFun’s XBee Explorer boards, this is relatively painless.
So with that point in mind, let start with the easiest step. To allow your computer to talk to the remote Arduino, you will need a XBee connected to your computer. As mentioned, the SparkFun XBee Explorer USB board makes this super simple. Take one of your XBee modules and connect it to the Explorer USB board. With a USB cable at hand connect it to the board and just put this setup aside for now. We will be using it once we have loaded code to the Arduino and are ready to test. As a side note – since I use this Explorer USB all the time, I put a piece of Velcro on the back of it as well as another piece of Velcro on the back of my laptop. This is just to make my setup less cluttered with cables everywhere.
The next step in the setup process is getting our Arduino setup with our other XBee module.
Part 2 – Writing the Code
Here is the Arduino code I used for this demo. This demo is very simple, as it just prints the numbers 0 to 100 every second. As you can see, it is as simple as sending the data via the Serial.print command.
void setup() { Serial.begin(9600); } void loop() { delay(1000); for(int a = 0; a < 100; a++) { Serial.print(a); Serial.print(" "); } Serial.println(); }
One thing to note about loading code to the Arduino while using XBee’s. Since the XBee uses the RX and TX of the Arduino, you must disconnect / turn off the connection to the XBee while loading code to the board. In my case, since I am use the SparkFun XBee Shield, I can simple move the small switch on the shield from “DLINE” to “UART”.
Part 3 – Testing the Project
Once this sample code snippet it added to the Arduino, disconnect the Arduino and reconnect it to a external power source (not on the computer you want to wirelessly connect to). Grab the XBee Explorer we set aside earlier and plug it into your computer.
Make sure the correct COM port is selected in the Arduino IDE and then fire open the Serial Monitor. If you haven’t already – make sure that Arduino is externally powered and if everything is working properly, you should see the numbers (0 – 100) appearing on a new line every one second. Congrats, you got your computer listening to your Arduino, wirelessly!
Part 4 – Something More Practical
Here is a code snippet from when I was testing code for my ground antenna for my FI-AUAV. The commands are for pan and tilt control on the antenna mount allowing me to track the position of the UAV in real-time. The commands are sent to the Arduino on the antenna, parsed and then the servo rotation of the antenna is updated.
The commands are PP000# and TT000#. Where PP is pan, 000 is the rotation amount for that servo to rotate to and # is the end line identifier. TT is for tilt control.
#include Servo.h #define BufferLength 16 #define LineEnd '#' Servo pan; Servo tilt; char inputBuffer[BufferLength]; void setup() { Serial.begin(9600); pan.attach(9); tilt.attach(10); Serial.println("Ready for input!"); } void loop() { int inputLength = 0; do { while (!Serial.available()); inputBuffer[inputLength] = Serial.read(); } while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength); inputBuffer[inputLength] = 0; HandleCommand(inputBuffer, inputLength); } void HandleCommand(char* input, int length) { Serial.println(input); if (length < 2) { return; } int value = 0; if (length > 2) { value = atoi(&input[2]); } int* command = (int*)input; switch(*command) { case 'PP': pan.write(value); break; case 'TT': tilt.write(value); break; default: break; } }
The is a simple interpretation of the current code I am using, but this version of the code can easily be implemented in many other robotic platforms.
Final note: To power the XBee and two servos you will need to power the Arduino with something like a 9-12VDC power supply as most traditional 5V supplies won’t have the power to run everything.
Feel free to leave a comment if you have a question. Good luck with your wireless projects!
3 Comments
Trackbacks/Pingbacks
- Simple XBee Communication with Arduino « Ideas for Teaching Computer Technology to Kids - [...] http://fisherinnovation.com/i/2011/06/12/simple-xbee-communication-with-arduino/ [...]


Hello sir,
First of all, thank you for this tutorial. I am using xbee for one of my projects and it’s really hard to find a good tutorial on google.
Second, there are still some parts I do not understand. In your step 2, you loaded the code in the arduino. In step 3, you said you have to plug xbee to the computer. My question is, how does the arduino know where to “serial write” to? Do you not have to configure arduino and xbee so that they are talking to each other?
Same question as Edward Han. Is the XBee using a “First-heard First-read” behavior? First xbee transmission an xbee receiver hears is the winner of the link? Did you match the XBee modules in a previous step you forgot to mention?
Greetings.
You are correct, the step not mentioned in this post is the Xbee configuration completed within their Windows based software. Adafruit has a good post on this topic – http://www.ladyada.net/make/xbee/configure.html check it out and let me know if you are still having issues.
Cheers.