Wednesday, October 30, 2013

Proximity Detection with Sonars using Arduino

In one of my projects, the goal is to detect someone standing in front of an object. My sensors are mounted on this object. I went about figuring out the best way to do this. Turns out there can be several ways of detecting proximity, PIR sensor, IR sensor or Sonar sensor. PIR sensor is not recommended as it only works with moving objects. If the object in front of a camera stops, there is no way of detecting if it is still in front or not (Read http://www.robotoid.com/appnotes/sensors-passive-infared.html). The IR Sensor would require a receiver to be placed at the opposite end of the transmitter for a breach to be detected. In our case this solution won't work as our object could be located in the middle of a crowded space. The solution seems to be using the Sonar sensor. A single or a couple could be used with fine tuned settings to detect an object to within a couple of feet of the object.

So my next experiment is to simply connect a sonar proximity sensor to an Arduino and get signal readings. In order to do that I ordered an HC-SR-04 sensor from EVS Electro.  This is a pretty good distributor if you are based in Islamabad. They can deliver it to you for Rs 200 or you can order and pick it up from their office. I got the sensor for Rs 1200 which is a bit expensive if you compare it to its price on Chinese websites, but then it would be almost the same if you add delivery and tax if any.




This sensor works on the principle of Sonar where ultra sonic sound waves are transmitted which bounce back off an object. Since we know the speed of sound we can figure out the distance by measuring the time traveled by the sound wave to the object and back. The formula for speed is 

          speed = distance / time, 

we can find the distance by 

          distance = speed * time ; 

where speed is the speed of sound and the time is time  taken by the sound wave to travel from the object back to the sensor which would be the total distance of traveling and coming back divided by 2.

The sonar sensor used here has 4 pins. Vcc, Trig, Echo and Gnd. Vcc requires 5 volts so it is connected to the 5v source on the Arduino and the ground is connected as well. This module handles the triggering and the echo sensing on its own. In order to make it work the trigger pin is kept high for 5~10 microseconds, time stamp is taken, after which input is sensed on the Echo pin. As soon as the echo pin goes high another time stamp is taken. Difference between the two timestamps is the total time covered by the pulse transmission, bouncing and returning back. The following is the arduino code for the Sonar Sensor.  The original code can be found at (http://www.arduino.cc/en/Tutorial/Ping). It has been modified to be used with our sensor which has two pins rather than one for the sensor.

/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
    
   The circuit:
    * +V connection of the Sonar Sensor attached to +5V
    * GND connection of the Sonar Sensor attached to ground
    * TRIG connection of the Sonar attached to digital pin D10
        * ECHO connection of the Sonar attached to digital pin D11

   http://www.arduino.cc/en/Tutorial/Ping
  
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
  
   Modified Oct 25th 2013
   by Shamyl B. Mansoor
  
 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int trigPin = 10;
const int echoPin = 11;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, distance1,inches, cm;

 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //Echo
  duration = pulseIn(echoPin, HIGH);
  distance1 = (duration/2)/29.1;
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print("duration: ");
  Serial.print(duration);
  Serial.print(": ");
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
   
  delay(500);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Once you run this sketch and test the sensor, it seems pretty accurate!. Here are some pictures of it measuring distance of an object in front of it.







1 comment:

  1. An excellent article, will surely try this after the semester ends IA.
    There's one thing I wanted to ask; what is the maximum distance to which it can sense correctly?

    ReplyDelete