# Ultrasonic sensor distance equation and the factor 0.0001657

Hi I am using Ultrasonic waves sensor for detecting distance of object from my device.

As I understand the formula for calculating the distance is :

distance = (speed of sound * reflection time)/2

Now with arduino library's we are using function pulseIn() for getting Pulse Width
(which is happens when pin changes from high to low , waves comes back from reflection )

and then :

distance = Pulse Width * 0.0001657 .

Questions :

1. What is the meaning of pulse Width and how it connects to related to reflection time ?
2. from where this factor comes from 0.0001657 and how it related to speed of sound ?

Thanks for help !

terryking228:

Thanks terryking228,
Unfortunately this article has same knowledge as mine and not helps me to understand this factor ):

The speed of sound is 343 meter/sec at 20 degrees Celsius. That is 34300 centimeters/sec.

That is 0,03434 cm/microsecond or 0,01352 inches/microsecond.

The sensor measures the distance from and to the object, so we have to divide this value by 2.

That gives us 0,01717 cm/microsecond or 0,006760 inches/microsecond.

distance = pulsewidth (µs) * 0,01717 cm/µs.
distance = pulsewidth (µs) * 0,006760 inches/µs.

Instead of dividing by 2 and then multiplying by 0,03434 some code (for centimeters) uses a division by 58,24 which is mathematically the same. This saves one floating point operation and makes the code much harder to understand.

Thus 0,0001657 is a value I cannot understand, just like you.

Where did you find this?

distance = Pulse Width * 0.0001657

If it was somewhere on the internet, you are too trusting. Use the correct number instead.

jremington:
Where did you find this?
If it was somewhere on the internet, you are too trusting. Use the correct number instead.

You can find it in many posts/tutorials...but none of them explain what it is ,
That's why I try'd to ask here .

What do you mean use the correct number please? it is the correct number ,
As writing on all tutorials and its working , distance = pulseIn() * 0.0001657 .

148us/inch, 5.83ms/m are the correct figures for sound in dry air at 20C, by simple calculation

For metres, pulseIn() / 5830.0 [ pulseIn() * 0.0001715 ]
For inches, pulseIn() / 148.1 [ pulseIn() * 0.006752 ]

If you double 0.0001657 you get 0.0003314 so it seems that it is still a speed sound but at 0°C in dry air.

sit is the correct number ,
As writing on all tutorials and its working , distance = pulseIn() * 0.0001657 .

Just because it appears several places on the internet does not mean that it is correct.

jremington:
Just because it appears several places on the internet does not mean that it is correct.

I agree, it's like Celcius instead of the correct Celsius.
Anyway I believe that the difference in the range of 4m is small, about 20cm, compared to the precision and accuracy of this sensor.

I found a datasheet for the US100 ultrasonic sensor that describes the derivation of that factor.

They did indeed use ~331 m/s as the speed of sound, which is correct for 0 degrees C. Not very accurate at room temperature!

Thanks for everyone we are getting close .

So the explanation is ~331 m/s divide by 2 is 165.5 m/s in 0 degrees dry air .

distance = (speed of sound * reflection time)/2
vs
distance = (0.0001657 * pulseIn()) <----- correct working distance in meters

but how 165.5 becomes 0.0001657 ?

----------------------edit------------

165.5 m/s = 0.0001657 m / microseconds

now distance is = 0.0001657 m/µs * pulseIn() µs = m (: Thanks !!!