First Contact: XBee Radios

XBee Series 2While this post may technically be off topic – I can’t really claim it’s connected to my Sprite or the KickSat in any direct way – I’ve decided to post it as its own project anyway. The XBee radios run off the Arduino microcontroller platform and interact using radio transmissions, so I thought I’d shoehorn them in after the last post about the Sprites’ wireless communications and following a stint of microcontroller talk.

XBee radios are small electronic devices that use the ZigBee protocol to transmit information wirelessly. Many radio devices use the ZigBee protocol, but XBees are some of the easiest to configure and deploy. They are designed for point-to-point transmissions or for the creation of mesh networks. Users employ XBees to construct robot swarms, remote controlled devices, wireless sensor networks, and anything else they dream up.

In this introductory project, I’ve built a wireless doorbell with two XBee Series 2 radios. Series 2 radios allow mesh networking; Series 1 radios do not. Mesh networking is not necessary for this particular project since there are only two nodes, but having that capability leaves room for more interesting projects in the future. Initial setup of the XBees requires Digi’s X-CTU configuration software, which unfortunately is Windows only. After this initial configuration, most of the radios’ settings can be adjusted with one of many the terminal programs that can be found on any platform (an often-cited choice is CoolTerm).

I’ll avoid describing the configuration process (the first 50 pages of Robert Faludi’s Building Wireless Sensor Networks can help guide you through that process if you’re interested) and skip to the more interesting part: combining Arduinos and XBees to make a wireless “doorbell.” XBee pins are spaced more closely than the holes on a standard breadboard, so I had to solder together an adapter before laying out the circuits. For this project, I built adafruit‘s XBee Adapter Kit:

adafruit XBee adapterXBee Adapter Kit from Adafruit Industries after being soldered

Every XBee network has a single coordinator radio; the rest of the radios act as receiver nodes. The network engineer designates which kind of node each XBee functions as using the configuration software. XBee coordinator boardsAn XBee’s function can be changed at any time (assuming access to a Windows computer with the configuration software), but the designation exists purely in the realm of software – there is no physical indication as to what kind of node any particular XBee has been configured to act as. Thus, labeling the XBees with masking tape to indicate whether they are coordinator or router nodes is a good idea. There are only two radios in this network, so the coordinator node can be placed in either position. For this project, I connected the coordinator node to the button or “doorbell.” In the image to the left, an XBee labeled “C” (coordinator) connects to an Arduino Uno using a white miniature breadboard. The tall black and silver object with the red top located on the breadboard functions as the “doorbell.” When the button is pressed, the corresponding pin on the Arduino will register a high voltage, indicating that it should send a wireless message through the XBee radio.

The exact code running on the coordinator node’s Arduino microcontroller is as follows:

// the Coordinator sketch

/*
 * constants
 */
// the button will be connected at pin 02:
const int BUTTON = 2;
// the led will be connected at pin 13:
const int LED = 13;

/*
 * the setup() function runs once, when the
 * Arduino is powered on
 */
void setup() {
  // set the button pin (pin 02) to an input pin:
  pinMode(BUTTON, INPUT);
  // set the led pin (pin 13) to an output pin:
  pinMode(LED, OUTPUT);
  // initialize the serial communication at 9600 baud:
  Serial.begin(9600);
}

/*
 * the loop() function loops until the
 * Arduino is powered off
 */
void loop() {
  // if the button pin registers as a high voltage:
  if(digitalRead(BUTTON) == HIGH) {
    // send the character 'D' to serial communication:
    Serial.print('D');
    // set the led pin to a high voltage:
    digitalWrite(LED, HIGH);
    // wait 10 milliseconds so the loop doesn't repeat too fast:
    delay(10);
  }
  // otherwise:
  else {
    // turn the led pin off (set its pin to a low voltage):
    digitalWrite(LED, LOW);
  }
}

// end of the Coordinator sketch

XBee receiver boardsThe receiver node makes up the second part of the network. The construction of this receiver portion looks similar to the coordinator portion; it’s composed of an XBee, a miniature breadboard, and an Arduino Uno, along with some wires and other components. The most visible difference is that, instead of a red-tipped button, this breadboard holds a small Piezo speaker. When the receiver node picks up the appropriate signal, the Arduino sets the pin connected to the speaker to a high voltage, causing the speaker to buzz. I have added an LED to both Arduinos so that the transmission and reception is visible in addition to being audible.

The code running on the receiver node’s Arduino:

// the Receiver sketch

/*
 * constants
 */
// the buzzer will be connected at pin 05:
const int BELL = 5;
// the led will be connected at pin 13:
const int LED = 13;

/*
 * the setup() function runs once, when the
 * Arduino is powered on
 */
void setup() {
  // set the buzzer pin (pin 05) to an input pin:
  pinMode(BELL, OUTPUT);
  // set the led pin (pin 13) to an output pin:
  pinMode(LED, OUTPUT);
  // initialize the serial communication at 9600 baud:
  Serial.begin(9600);
}

/*
 * the loop() function loops until the
 * Arduino is powered off
 */
void loop() {
  // if serial communication is available:
  if(Serial.available() > 0) {
    // and if serial communication receives a 'D' character:
    if(Serial.read() == 'D') {
      // set the led pin to a high voltage:
      digitalWrite(LED, HIGH);
      // set the buzzer pin to a high voltage:
      digitalWrite(BELL, HIGH);
      // wait 10 milliseconds so that the output is perceptible:
      delay(10);
      // set the led pin to a low voltage:
      digitalWrite(LED, LOW);
      // set the buzzer pin to a low voltage:
      digitalWrite(BELL, LOW);
    }
  }
}

// end of the Receiver sketch

Notice how much easier it is to parse the code running on the Arduinos compared to code written for other microcontrollers. Explaining the code for the NerdKits temperature sensor project took two posts, and I left parts of it out! The code running on the Arduinos can be sufficiently explained using // comments. Granted, this project is lighter on programming than the other. But enabling this kind of quick prototyping is what Arduino was designed to do. Without the Arduino platform, the focus of this project couldn’t help but skew away from the XBees and more towards setting up logic using microcontrollers to govern their communication.

Now that the infrastructure of the network has been completed, it can be put to use (you may want to turn up the volume; YouTube practically muted the buzzer noise after the upload):

In this demonstration, the XBees are hardly more than 10 or 20 cm apart. This is only because I needed to power both of them and keep them close enough to show both in one shot. The Series 2 XBees have an indoor range of up to 40 meters, with a line-of-sight range of three times that: 120 meters. Next time I have access to a football stadium I’ll try and put that range to the test.

Leave a Reply