MEASUREMENT OF THE SPEED OF SOUND WITH HY-SRF05

I have used the following code in order to measure the speed of sound, it works!
but I can't understand the logic of it!!! especially this line "pingTime = pulseIn(echoPin, HIGH);"
I would really appreciate that if anyone answers me and tells me the exact reason why this code prints the speed of sound.

what are the rules of
delayMicroseconds(2000);
delayMicroseconds(10);

int trigPin = 13;
int echoPin = 11;
float pingTime;;
float speedOfSound;
float targetDist = 15;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pingTime = pulseIn(echoPin, HIGH);

  speedOfSound = 2*targetDist / pingTime ;

  speedOfSound = speedOfSound * 10000;

  Serial.print("The Speed of Sound is ");
  Serial.print(speedOfSound);
  Serial.println(" m/s");
  delay(1000);
}

Moderator: added code tags

It relies on the fact that the (ultra)sound is travelling a fixed distance. If the distance is known then you can calculate the speed.

It's basically a simple opposite of the normal use of these sensors where you assume the speed and from the time taken you calculate the distance.

Steve

+1 for slipstick

Given that the speed of sound depends on temperature you can even make a (rough) temperature sensor of it.

fixed distance give SOS --> lookup table --> temperature

And you can make things warmer or colder simply by moving the sensor :smiley:

LOL
yes definitely if something heats up it normally expands :wink: