Exploring Arduino: XBee and Sharp IR Distance Sensor

by Amit


I have been exploring the Arduino since my last post. I haven’t gone around doing something very useful yet. However, I have been able to pick up the bits & pieces and more excitingly getting a hang of the engineering aspects of things. In a small mini-project, I need to have a wireless communication setup. I decided to use XBee for this and this tutorial worked great for me.

XBee Shield

XBee Shield with XBee Series 1 module + Arduino UNO

XBee USB Explorer

XBee USB Explorer + XBee S1 Module

XBee shield stacked on the top of the Arduino UNO. I had to use stacking headers to get a lift, since the XBee shield was protruding onto the USB connector of the UNO.

XBee Shield on the Arduino UNO (using stackable headers to create some vertical room)

The Sharp IR distance sensor connected to the UNO to transmit sensor data over XBee:

XBee "powered" Arduino UNO with the Sharp IR Distance Sensor attached

I hooked up the Sharp IR distance sensor to transmit the distance over XBee to my explorer module. This post helped me get very accurate readings from the distance sensor.

Arduino sketch

Here is the Arduino sketch:

/*
XBee Controller
xbee_controller.pde
*/

/* Sensors*/
int IRpin=1; /* IR Distance Sensor */

/*Actuators*/
int ledPin=13;

/* Read the incoming command*/
int command;

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

void loop()
{

   while (Serial.available()>0)
    {

      command = Serial.read();

      if (command==49) //1
        { /* following code is taken from the blog post linked above*/
          /* Read the IR sensor data and send */
          float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
          float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
          Serial.println(distance);                       // print the distance
         }

      if (command == 50) //2
         {
           /*Let there be light!*/
           digitalWrite(ledPin,HIGH);

         }
    }

}

Communicating with the “remote” XBee

On your host with the XBee explorere connected, open a serial terminal to /dev/ttyUSB0 either using screen (screen /dev/ttyUSB 9600) or using ‘minicom’.
If you send a ‘1’, you should get the IR sensor data and if you send a ‘2’, the LED will light up.

As you can see from above sketch, I intend to extend this sketch to respond to more commands and hence activate different actuator or send any other sensor data.

Advertisement