Tag Archives: zigbee

XBee 002: radio-chat between PC and Arduino

The starting point of this tutorial is that I add some breakaway headers to the XBee’s USB breadboard adapter.

IMAG4302

This is the “B” module from my previous tutorial… and we will start asumming that the XBee has been set up using that tutorial.

XBee 001 Basic example: radio-chat between 2 PC

So, what are we going to do?

We will have an Arduino board connected by USB to one computer and by radio (using a pair of XBee) to another computer. Using one computer or the other we are going to switch LED on and off.

Step 1. Make the electrical connections. We connect 3 LED to pins 9,9,10 on the Arduino. Remember to use an appropriate resistor to limit the amount of current. Use the ground on the Arduino to close the circuit.

arduino-xbee

To be able to communicate with the XBee you need to power it (+5V to pin VDD and ground to VSS pin), the Arduino can provide enough power, so use it. Connect pin 7 to DIN and pin 6 to DOUT. These two pins are going to be used for serial communication between the Arduino and the XBee.

For other alternatives to communicate between a microcontroler and the XBee, the parallax webpage gives a good support here.

Step 2. The code. As you can see, the code comments explain what it does. Simply reads from Serial (USB) or from the new serial defined in ports 6 and 7 to comunicate with the XBee. Depending on what we read from the Serial, we turn on one LED or other.

//Arduino serial comunication happens through the USB or on the pins TX and RX
//including this file allows us to create another Serial port using pins 6 and 7
#include <SoftwareSerial.h> 
#define Rx    6                // DOUT to pin 6
#define Tx    7                // DIN to pin 7
SoftwareSerial Xbee (Rx, Tx);

//Some variables for the LEDs and reading the Serial
int blue_led = 8;
int green_led = 9;
int red_led = 10;
char incoming ='empty';

void setup(){
  Serial.begin(9600);  // start serial port at 9600 bps:
  Xbee.begin(9600);
  pinMode(red_led, OUTPUT); 
  pinMode(blue_led, OUTPUT); 
  pinMode(green_led, OUTPUT); 
}
  
void loop(){
  if(Serial.available()){
    //If there is data on the Serial port, read it and ligth the corresponding lights
    incoming = Serial.read();
    lights (incoming);
  }
  if(Xbee.available()){
    //If there is data on the XBeeSerial port, read it and ligth the corresponding lights
    incoming = Xbee.read();
    lights (incoming);
  }    
}
  
int lights(char incoming){
  switch (incoming){
    case 'red':
    //Turn on the red LED
    digitalWrite(red_led, HIGH);
    digitalWrite(blue_led, LOW);
    digitalWrite(green_led, LOW);
    break;
    case 'blue':
    //Turn on the blue LED    
    digitalWrite(red_led,LOW );
    digitalWrite(blue_led, HIGH);
    digitalWrite(green_led, LOW);
    break;
    case 'green':
     //Turn on the green LED
     digitalWrite(red_led, LOW);
     digitalWrite(blue_led, LOW);
     digitalWrite(green_led, HIGH);
     break;
     default :    
     // default is optional
     break;
   
  }
}
 

Upload the code to your Arduino.

Setp 3. Communicate There is 2 ways of communication. First, using the same computer that is already connected by USB with the Arduino, open the Serial Monitor in the Arduino UI and write the colour of the LED you want to be on.

xbee

And the LED turns on.

IMAG4303

As you can see my electrical set-up is not as tidy as the schematic… but it works.

The second option is using the other XBee (from our previous tutorial) and the program CoolTerm to send data through the XBee. So, disconnect the USB from the Arduino and power it with batteries (or use one computer to power and another to communicate). Connect the “A” XBee with its USB adaptor to the computer and open CoolTerm program. The only change you need to do respect to the previous tutorial is enabling Line mode under Options>Terminal. This will enable sending one string at a time instead of individual characters.

coolterm

Once this is done… it is ready for communication. Simply write the name of the colour you want.

IMAG4312

Hope you find it useful… please, practise it if you can, because soon we are going to use this hack with something more funnier…

XBee 001 Basic example: radio-chat between 2 PC

A year ago I received a big order from  Adafruit industries.

adafruit

Among the things I ordered, was the camera for the Raspberry Pi, an Arduino UNO board, some accessories for the Arduino… and a pair of XBee with micro USB adapters!

IMAG4282

(Link to buy the XBee and the USB adapters).

Ok… so I know the basics about XBee… it can be used to communicate using radio signals… but I don’t know more than that! It is now time to try a few things and learn.

Fortunately, the company that fabricates the XBee has examples on its webpage, and we can follow it to learn (you and me).

Basic XBee ZB ZigBee (Series 2) Chat

The above example explains how to use 2 XBee to communicate between 2 PC. I’m going to follow it trying to simplify/update the example and adding tips when needed.

Step 1. In order to use the USB adapters, we need to install drivers for them. They are here. Look for your OS and download and install the drivers in both PC. In my case, one computer is running Windows 7 x64 and the other one is running the no longer supported Windows XP x32. The driver worked fine on both.

Step 2. Now we need a software to communicate with the XBee through the USB port. That is called CoolTerm.

coolterm

Download the one needed for your OS unzip and it is ready.

By the way… once you are in Roger Meier’s webpage… if you scroll down, you migth find a R2D2 translator which is quite funny to try.

Step 3. You need to write down the numbers on the back of the XBee (top one is the High, and bottom one is the Low), name them A-Coordinator and B-Router.

Series2Bottom-300x300

Step 4. To tell the XBee who is the Coordinator and who is the router, you need to download X-CTU form here.

Once X-CTU is installedm connect the A-Coordinator and open X-CTU. Select the COM port and click “Test / Query” to see if X-CTU can communicate with the radio. The result should look like this (If not, try to diconnect/reconnect the USB and closing/opening the program).

coolterm

Now click the “Modem Configuration” tab on the top and then drop down the “Modem:” list. Select XB24-ZB . Then choose ZIGBEE COORDINATOR AT from the “Function Set” list. Finally, click “Write” to update the firmware.

Repeat the process with the B-Router, selecting ZIGBEE ROUTER AT from the “Function set list”.

Step 5. Now we need to setup our XBee using the CoolTerm program.

A-Coordinator. Connect the XBee named A-Coordinator using the USB and open the program. In the program, click options and Re-Scan Serial Ports. You will know which port the XBee is connected by repeating this step with and without the XBee USB adapter connected.

coolterm

If it doesn’t show, most probably you need to restart the system or the drivers are not properly installed.

The parameters for the coordinator are:

  • Baudrate is set to 9600.
  • Be sure Data Bits is set to 8.
  • Be sure Parity is set to “none.”
  • Be sure Stop Bits is set to 1.
  • In the list on the left side of the options window, click “Terminal.” Be sure “Local Echo” is activated.

Now hit ok and back in the main window, press Connect. You will see a “connect” message on the bottom left corner.

coolterm

Now to set the firmware you need to write this commands very fast. The first one, +++ opens the command mode (it will close by itself in 10 s). The rest just set up the connection. ATDH is the same for all, ATDL has to be the Low number on your B-Router XBee. (If you write the commands without arguments, the device returns actual values). The ATWR command saves the changes. Remember to use it.

+++
OK
ATID 2001
OK
ATDH 0013A300
OK
ATDL 40B18506
OK
ATWR
OK

Click disconnect and remove the device.

B-Router. Now connect the XBee named B-Rooter. You need to go back to options again and look for the correct serial port (it might be different). Hit connect and repeat the commands. Now you need to put the Coordinator’s Low in the ATDL command.

coolterm

Step 6. Try it! Connect the a-Coordinator into one PC and the B-Router into the other. Open CoolTerm and choose the right COM port… (If it doesn’t work, check setup parameters with the commands ATID etc or review the steps to make sure everything works fine). In my case when it works, whatever you write in one PC or the other, appears in both! So you have your chat enabled!

IMAG4283