Thursday, November 22, 2012

Distance measurement sensors

There are many Aurdino sensors to measure distance. one common analog sensor is the Long range Infrared sensor GP2Y0A02YK0F, other common digital sensor is Ultrasonic sensor HC-SR04.
The use of each sensor depends on your needs. For example, the IR sensor can measure distances between  15 cm to 150 cm otherwise output distances would be inaccurate, in this case, there are some algorithms used for error detection and one common algorithm is Kalman filter. The Ultrasonic sensor can measure distances from 2 cm to 400 cm. Cost of each sensor should be considered when choosing the sensor.

IR Sensor GP2Y0A02YK0F 

The IR sensor has 3 ports, VCC for the supply supply which is 5V DC, GND for the ground, and V0 for the output voltage represents the distance, according the output voltage we can calculate the distance using some equation. The equation used for calculate distance is 
          inches = 4192.936 * pow( outputVoltage ,-0.935) - 3.937;
          cm = 10650.08 * pow(outputVoltage,-0.935) - 10;

Below is the Outline Connection for the component

source: https://www.sparkfun.com/datasheets/Sensors/Infrared/gp2y0a02yk_e.pdf


To connect the device, connect the VCC port with the 5V supply voltage in the aurdino port, GND to the ground, VO to any of the analog ports in the board.

Below is a working sample shows how to use the sensor.

         #define sensorIR1 1           
         float sensorValue, inches, cm ;   
         void setup() {
           Serial.begin(9600);
         }

         void loop() {
           sensorValue = analogRead(sensorIR);
           inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
           cm = 10650.08 * pow(sensorValue,-0.935) - 10;
           delay(500);
           Serial.println(sensorValue);
           Serial.println("");
         }

Ultrasonic Sensor HC-SR04


The sensor has 4 ports, VCC the supply voltage which is 5V DC, GND Ground, TIG Trigger, and ECHO is the echo port.

Generally,  Ultrasonic sensors generate high frequency sound waves and evaluate the echo which is received back by the sensor. Sensors calculate the time interval between sending the signal and receiving the echo to determine the distance to an object.

To connect the device, connect the VCC to the 5V supply voltage, GND to the ground, TGR and ECHO to two digital ports in the board.


There is one library  to deal with the sensor. the library consists of 2 files ultrasonic.h and ultrasonic.cpp and should exist under Document/Aurdino/Libraries/Ultrasonic folder,  if you don't find them, you can download library Here.

Blow is a working example for ultrasonic sensor, the example  should exist in the example list of Aurdino "File-> SketchBook->Aurdino->UltrasonicDemo"

     #include "Ultrasonic.h"
     #include <LiquidCrystal.h>
     //LiquidCrystal lcd(11, 10, 9, 4, 5, 6, 7);
     Ultrasonic ultrasonic(12,13); //trigger into 12

     void setup() {
     //lcd.begin(16, 2);
     //lcd.print("testing...");
     Serial.begin(9600);  
     Serial.println(" Ultrasonic testing...");
     
     }

     void loop()
     {
      Serial.print(ultrasonic.Ranging(CM));
      Serial.println("cm");
       delay(100);
     }

No comments:

Post a Comment