Use this proximity sensor to measure distance - it works by sending out an ultrasonic signal and listening for a response!
This is a 5v sensor, so will need  a suitable external power supply if using this with a micro:bit (there's an example in MakeCode here).
 The datasheet is available here.