Learn the science and math behind an ultrasonic sensor.

An ultrasonic sensor can be used to calculate how far away an object is. These sensors can be used to determine things like how far your car is from a wall, or whether or not your water bottle is in the correct place for a water fountain to fill it.

Let’s take a look at how they work.

The sensors are called “ultrasonic” because they emit a rapid series of clicks that are too high for humans to hear.

The noise bounces off an object in front of the sensor and echos back to the sensor.  This is very similar to how a bat navigates as it flies!

The  sensor can detect how quickly the sound returns to it, and that time can be used to calculate the distance. Let’s take a  look at the math.

Sound travels at about 344 meters per second.  

There are 100 centimeters in a meter.  

There are 1 million microseconds in a second.  

So 344 m/s *1s/1,000,000 ms *100cm/1m = 34400/1,000,000 cm/ms =344/10,000 cm/ms

Looking at a specific example, if the sound takes 2000 mcs to return (that’s 2/1000 of a second), then we know it took 1000 mcs to travel out and the same to travel back.  So 2000 mcs x ½ for the distance out but not back x 344 over 10,000 cm/ms equals 34.4 cm away.

More generally speaking, we get the formal time x ½ x .0344 = distance, where time is measured in microseconds and distance is measured in centimeters.

That is how you can use an ultrasonic sensor to calculate distance.