Connect ultrasonic sensor with micro:bit.
Program ultrasonic sensor to detect obstacle.
Use ultrasonic sensor to measure the distance between robot and obstacle.
Set which pin you are using for Trigger port (transmitter) and Echo port (receiver) for ultrasonic sensor.
Check the distance between the ultrasonic sensor with the obstacle, returns the value in the unit of cm.
Show Ultrasonic Distance (cm)
Ultrasonic sensors is a devices that used to measures the distance of a target object. Normally, ultrasonic sensor have transmitters and receivers. Transmitter converts electrical signals into ultrasound and blast it out, while receiver converts ultrasound received into electrical signals.
In a similar way to radar and sonar, ultrasonic sensor evaluates target's distance by interpreting the reflected signals. The transmitter will emit ultrasound waves, when the ultrasound wave hit an object, it will be reflected back and detected by the receiver.
By measuring the time taken for ultrasound wave to travel after emitted by transmitter and reflected echo to be received by receiver, then using a simple calculation involving speed of sound wave, the distance of an object can be calculated.
Ultrasonic may not work as expected in the following scenarios:
i. Object is too far away
ii. Object is too small.
iii. Object is too soft.
iv. Object bound the sound wave away from the receiver (as a result, echo can't be detected by receiver).