Ultrasonic sensors are devices that use ultrasonic sound waves to detect and measure distances to objects.

Ultrasonic sensors work based on the principle of sending out ultrasonic waves (inaudible to the human ear) and measuring the time it takes for the waves to bounce back after hitting an object. 





HC-SR04:


HC-SR04 sensor emits an ultrasonic sound wave, which is typically at a frequency of 40 kHz (inaudible to the human ear).

In order to generate the ultrasound, we need to set the Trig pin on a High State for 10 µs.

The sensor continuously listens for the reflected wave using its Echo (Echo) pin.

When the reflected wave is detected, the Echo pin goes high (logical 1). The sensor measures the time it takes for the emitted sound wave to travel to the object and back to the sensor. This time is known as the "time of flight."



Distance Calculation:


• Distance = Speed x Time

• Sensor uses the time it measured and the known speed of sound to calculate how far away the object is.

• Distance = (Speed of Sound x Time) / 2

 Time is the time it took for the sound wave to travel to the object and back.

 Speed of Sound is the speed at which sound travels in the air, typically around 343 m/s at room temperature.

 Dividing by 2 is necessary because the sound wave goes to the object and then comes back to the sensor.