IAN LANG ELECTRONICS

Using Xbee on your Arduino

Go Back XBee

The XBee Doorbell Project

Previously, to control things using wireless technologies, we have used straightforward RC transmitters and receivers, and most latterly a low cost TX/RX pair made by RF Solutions. Both solutions worked adequately but neither was without its problems. In this chapter we are going to look at a more sophisticated method and use XBees on our Arduino boards. If you did not arrive at this article via the XBee article, you can read it by clicking the link on the left.

 

Here's a short video showing what we are going to do here with a pair of Xbee series 2 modules.

The XBee that had the piece of tape on it in that video was the co-ordinator, the other the end device. As you saw, the co-ordinator had the activate button and the end device worked the piezzo buzzer to produce the melody. If this was a real doorbell, we'd amplify that sound electronically and mechanically, but I stuck with the piezzo because at the volume it is it's annoying enough.

I really do not like the tuneful doorbells. I have no idea why, but they irritate me beyond reason. They are however a very good study start when it comes to wireless control for their very simplicity.

 

So, this is the idea. The co-ordinator acts as a transmitter. The end device acts as a receiver. The two transmit serial data in exactly the same manner as does a wire with the exception that there's no wire, just a well modulated radio wave. It works on 2.4 GHz, exactly the same as the wi-fi we use and which is on 24 hours for internet access. But, because of the PAN ID in the XBees, the wi-i does not interfere, and the Xbees don't interfere with the wi-fi either. Perfect.

 

The problem is that the XBees can't make logic decisions.  They can send and receive data, and in some respects do actions when they do, but what they can't do is parse it and act on a logical argument, and that's why we need the microcontrollers.

So, let's look at the transmitter first.

The XBee is acting as a transmitter only here and so needs only three connections. They are Vcc, which is 3.3 and NOT 5V, it won't work on 5V and if you give them more than 7V they fry. Secondly is ground (GND) and lastly DIN (Data In) which is attached to TX and takes in the serial output from the Arduino board.  The button is set to keep pin 7 reliably low except when pressed. So, the code then:

 

 

byte outgoing;

void setup(){

Serial.begin(9600);

}

void loop(){

if(digitalRead(7)==HIGH){outgoing=49;

Serial.println(outgoing);

delay (3000);

}}

 

That's it. That's all there is. Let's canter through:

 

Firstly we set up a variable to contain the information we put to the transmitter:

 

byte outgoing;

 

All we do in setup is open the serial communication at the same rate the XBees are working at:

void setup(){

Serial.begin(9600);

}

 

The loop is:

 

void loop(){

if(digitalRead(7)==HIGH){outgoing=49;

 

all the code under that conditional can only be executed if 7 is high, and 7 can only be high if the button is pressed. So when the button's pressed:

 

Serial.println(outgoing);

 

49 is the ASCII code for the character "1" and so that's what gets sent through the serial port and hence the transmitter. Now we wait for three seconds to stop the data going through hundreds of times:

 

delay (3000);

}}

 

And that's it. A "1" gets sent every three seconds over the air if you keep your finger on the button that long.

 

The receiver is wired up so:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

byte incomingByte;

void setup(){

 

Serial.begin(9600);

}

void loop(){

if (Serial.available() > 0) {

incomingByte = Serial.read();

//Serial.println(incomingByte,DEC);

if (incomingByte==49){

    int (nl)=19;

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2};

for (int j=0; j<nl;j=j+1){//loop for nl value

tone (8,f[j],150*d[j]); // tone (PIN,FREQUENCY,DURATION)

delay (250*d [j]);} //wait until duration period has passed

 

}}}

 

The receiver is a bit more complex but not much so. Again we set up a variable to read the incoming data from the serial port if there is any, and open for communication.

 

byte incomingByte;

void setup(){

 

Serial.begin(9600);

}

 

In the loop, we look or incoming data, and if there is any we assign it to the variable incomingByte:

 

f (Serial.available() > 0) {

incomingByte = Serial.read();

 

if the incoming data is "1" that's ASCII 49 and so:

 

  int (nl)=19;

 

that line above counts the number of notes in the melody

 

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2}; //sets duration of the note

for (int j=0; j<nl;j=j+1){//loop for nl value

tone (8,f[j],150*d[j]); // tone (PIN,FREQUENCY,DURATION)

delay (250*d [j]);} //wait until duration period has passed

 

If you alter the numeric value 250 in delay (250*d [j]);} you can speed up the melody or slow it down. The smaller the value the faster the melody, and vice-versa.

 

We've used the XBees as a TX/RX pair here but they are capable of more than that. Over the page, let's look at a method of feedback we can use to positively register that the receiver has got the signal and is ringing the bell.

More>