If you want to measure the distance between two points accurately with Arduino, you should use the sharp IR sensor, because of its high accuracy and small error rate in the measurement.
In this tutorial, the Arduino will help you get accurate measurement of the distance, so that you can use it in your project.
GP2Y0A21YK0F sharp ir sensor must be connected to the Arduino board as shown below.
Connections from the GP2Y0A21 sharp ir sensor to arduino :
• red wire → Arduino 5V pin
• black wire → Arduino GND pin
• yellow wire → Arduino pin A0
Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.
Place an object at different distances in front of the sensor and you will see that it displays the distance accurately on the serial monitor.
Now, as shown in the image below, the serial monitor displays the distance.