Working of Ultrasonic sensor-
We have mounted an ultrasonic sensor atop a servo. When the ultrasonic sensor is triggered, it sends out a pulse and simultaneously starts an internal clock. When the pulse hits an obstacle, it gets reflected back to the sensor. The sensor calculates the time difference between the sending and receiving of the pulse. Since the speed of the pulse in air (let it be 'v') and the time gap ('t') is known, the distance to the obstacle can be calculated as-
Time for total trip = t
Time for reaching the obstacle = t/2
=> distance = v*(t/2)
This value has been calculated in our code and can be stored and manipulated accordingly.