This popular sensor made by Sharp produces an analog output that varies from 2.25V at 4cm to 0.4V at 30cm.
Based on “typical values” from Sharp, the formula to translate sensor outputs into distance (the formula is only valid for a sensor output between 80 to 500) :
distance = (2914 / ( readVal + 5)) – 1;
Connection Diagram:
Arduino Implementation:
//Infrared Proximity Sensor - Sharp GP2D120
//an analog output
int IRpin = 0; //analog pin 0
void setup(){
pinMode(IRpin,INPUT);
Serial.begin(9600);
}
void loop()
{
int val = analogRead(IRpin);
float distance = calculate_distance(val);
Serial.print(distance);
Serial.print("t");
Serial.print("cm");
Serial.println();
//just to slow down the output
delay(1000);
}
float calculate_distance(int readVal)
{
//5V / 1024 = 0.0048828125
//Between 4 and 30cm
float volts = (float)readVal * 0.0048828125;
float distance = (2914 / ( readVal + 5)) - 1;
return distance;
}
Serial Terminal:
References:
http://www.sharpsma.com/webfm_send/1205
http://www.acroname.com/articles/sharp.html
