XBee Shield Hookup Guide

Contributors: Jimb0
Favorited Favorite 0

Example: Communication Test

Lets start simple. In this section we’ll upload a sketch which passes data between your serial monitor and the XBee. This sketch can be use to create a “portal of communication” between your Arduino’s serial monitor, and another XBee (connected to a computer via a USB Explorer).

Double-Check Your XBee Network

Before continuing with this example, you’ll need to make sure your XBee’s are configured correctly – they need to be on the same network and have compatible destination and MY addresses. By default, XBees will all be compatibly configured, but we recommend setting up unique network ID’s and addresses. Check out the Configuring Networks page of our Exploring XBee’s and XCTU tutorial for help with that.

This example assumes you have XCTU installed and two compatibly-configured XBees – one connected to your computer via a USB Explorer (or Dongle, or Serial Explorer) and another plugged into the Shield/Arduino.

alt text

The Arduino Sketch

Here’s the sketch we’ll use. It makes use of the SoftwareSerial library, which is included with all of the recent Arduino releases. Before uploading this sketch make sure the switch on the Shield is in the “DLINE” position!

Note: If you are using an Arduino Leonardo and not a Arduino Uno, change the switch to the “UART” position and use this sketch instead.


Set up a software serial port to pass data between an XBee Shield
and the serial monitor.

Hardware Hookup:
  The XBee Shield makes all of the connections you'll need
  between Arduino and XBee. If you have the shield make
  sure the SWITCH IS IN THE "DLINE" POSITION. That will connect
  the XBee's DOUT and DIN pins to Arduino pins 2 and 3.

// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX

void setup()
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.

void loop()
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor

After you’ve uploaded the code, follow this series of steps to verify that everything is working:

  1. Open the Arduino’s Serial Monitor. Make sure the baud rate is set to 9600.
  2. Switch to XCTU and click over to console mode.
  3. Type something in the console view, it should show up on the Serial Monitor.
  4. Type something into the Serial Monitor (and press “Send”), it should show up in the console view.
  5. Yay!

alt text

You can use this setup to create a chat system. If you have another computer nearby, try to see how far your XBees can be from each other while still reliably communicating.

If your XBee’s aren’t communicating with each other, try getting them closer together (if they were far apart to begin with). Otherwise, check out our troubleshooting section of the Exploring XBee tutorial.