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
Hopefully, I have published my first post on www.mcu-turkey.com which is one of the leading platforms based on embedded systems in Turkey. I will do my best in the following days so I am planning to publish a number of posts related with stm32 such as driving GLCD, menu design on GLCD, a couple of different robot software, remote controller, Bluetooth module control, basic application development on android and so on.
This is my first post as Turkish : http://www.mcu-turkey.com/stm32-lerde-eeprom-emulasyonu/
