There are several kinds of sonar systems. Here I am going to mention a simple and popular one, just for you to understand how in general it works.

The way a sonar works is by sending a pulse of sound, often called a ping, and then listening for reflections of the pulse, also called echo. From the reflected pulse and the time that it took to reach the sonar receptor, we can measure the distant of the object. The sender sends a pulse of sound every time (something that we call frequency). This pulse hits the object and reflects to the receiver, which detects this echo. The important parameters in the sonar system are:

1. The time interval of the pulses (frequency)2. The time it takes for the pulse to reach the object and to reflect to the receiver.3. The speed of sound in the air, water of any mean of transmission.

If you know these factors, you can calculate the distance between the sonar and the object.

Unfortunately the signal from the sonar sender looses energy on its trip, and the echoes return to the receiver with less energy, but still enough to give information about the existence of an object and the distance to its location.