You can use an ultrasonic to measure the amount of distance. Ultrasonic Ranging Sensor is a non-contact distance measurement module with stable performance and high ranging accuracy, with the inexpensive price. The measurement range can be up to 5M, which would be helpful for your project such as robotic obstacle avoidance and so on.
The counter starts counting at the same time as the ultrasonic wave is being transmitted. The ultrasonic wave propagates in the air. When it hits the obstacle on the way, it immediately reflects back, and the ultrasonic receiver immediately stops timing when it receives the reflected ultrasonic wave. The propagation speed of ultrasonic waves in the air is 340m / s. According to the time t recorded by the timer, the distance s between the launch point and the obstacle surface can be calculated, namely: s = 340t / 2.
It can detect the distance between the ultrasonic wave and the target, and it can cooperate with the car to make the obstacle avoidance car and so on.