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 USB Explorer
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.
The Sharp IR distance sensor connected to the UNO to transmit sensor data over XBee:
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.
[…] Arduino: Arduino + Ethernet + DHCP with EtherTen In the last post on Arduino, I briefly wrote about my experiments with setting up a wireless communication between a […]
how did you program the xbee pair?
It’s a pity you don’t have a donate button! I’d definitely
donate to this superb blog! I suppose for now i’ll settle for bookmarking and
adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this
site with my Facebook group. Chat soon!