Check out these instructions to build your own ultrasonic rangefinder. No, I’m not talking about some kind of weird DIY Leica M8. It’s a little device that simply finds the distance between it and another object.
The basic concept is like radar or sonar. You just need to know the speed of sound and measure the amount time it takes for sound waves to leave the device and bounce back. Then multiplying the time elapsed by speed and dividing by two you get the total distance to the object.
The device can be built with easy to find parts and a simple micro-controller. The instructions include source code for the micro-controller and schematic.