Tuesday, October 18, 2011

Serial Trigger using Arduino Duemilanove and Xbee

This blog details the first steps in creating an a proximity alarm/trigger using Xbee and a typical Arduino Duemilanove.
This blog is intended to help beginner/intermediate Smart Systems enthusiasts, like myself, to understand how to create a 'soft tether' system, which they can use as a tether, trigger or alarm in their own projects.
I chose to pursue Xbee radio communication as it is a widely available product with useful potential. There is a fair bit of info online about Xbee but a lot is very rooted in jargon which I found quite inaccessible for a noob such as myself.

For this example we will be programming one unit as a sender and one as a receiver. We are sending serial data via Xbee. The receiver unit will be constantly looking for that serial data and will switch between HIGH and LOW depending on whether any serial data is available.

Things you will need;
  • x2 Arduino Duemilanove (http://www.sparkfun.com/products/10175)
  • x2 Xbee shields (you can also use a breadboard to create your own breakout if you'd prefer)
  • x2 Xbee Series 1 (http://www.sparkfun.com/products/8664)
  • x2 USB to Arduino programming cable (http://www.seeedstudio.com/depot)
  • x1 LED to stick in pin 13 to show HIGH or LOW status of the system.
  • A portable power source for at least one of the Arduino's so that you can test the range of your system. A LiPo is probably the best bet at this stage but should really matter as long as the power supply is of suitable Voltage, 3-5V.

Each of the two units will look approximately like the images below when assembled;
For this very first trial I would attach each of the Arduino's to a computer to program the sketch onto the micro controller and to see that everything is running smoothly in the serial monitor and on the boards themselves.


Serial Sender Code;

void setup(){
Serial.begin(9600);
}


void loop(){
Serial.write('d')
delay(100);
}




    The data we are sending is a 'd' but this is completely inconsequential as the Receiver unit will be looking to see purely if there is any serial data available.
    To add complexity to your system there is the opportunity to add sensors and variables but it is a good idea to get this part of the system running smooth first, then go from there.






    Serial Receiver Code;

    void setup(){
    Serial.begin(9600);
    pinMode(13,OUTPUT);
    }

    void loop(){
    if(Serial.available() > 0){
    digitalWrite(13,HIGH);
    delay(100);
    Serial.flush();
    }
    else {
    digitalWrite(13,LOW);
    }
    }




    The statement in the void loop is saying ' if there is any serial data available then turn on pin 13 or else turn pin 13 off'. Serial.flush is included to refresh the system every loop that serial is available so that the availability of serial data is tested freshly each loop.

    This system will turn the LED in pin 13 on or off depending on whether the two units are in range of one another or if the sender has been switched off.

    At this stage the system is rudimentary RF communication but can be used as a building block for further projects. From here we are able to start adding layers of complexity to the sketch, hardware to the system or new functionalities.

    In the next post I will attach an external power source to my Arduino and attempt to measure the accuracy of the range of the Xbee in various conditions.

    No comments:

    Post a Comment