 # Finding distance using Ultrasonic Distance Sensor

Hi everyone! I am currently doing my final year project. I am a little confused on the part that I found online by finding the distance between an ultrasonic distance sensor(PING))) Sensor) and an object. I do not really understand how to the microsecondsToCentimeters method works. Because by finding the distance of sound, I can use the formula, distance = speed * time / 2. But microsecondsToCentimeters seems to use another way to get the distance. Can someone explain the concept of the method to me? *Just a little confused here... Thanks! The codes are as follows:

``````int sensor = 27;

void setup() {
Serial.begin(9600);
}

void loop() {
long duration, distance;

pinMode(sensor, OUTPUT);
digitalWrite(sensor, LOW);
delayMicroseconds(2);
digitalWrite(sensor, HIGH);
delayMicroseconds(5);
digitalWrite(sensor, LOW);

pinMode(sensor, INPUT);
duration = pulseIn(sensor, HIGH);

distance = microsecondsToCentimeters(duration);

Serial.print(distance);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
``````

Distance = time / speed / 2.

wvmarle:
Distance = time / speed / 2.

Thanks for the reply. But why do I use this calculation instead of using distance = speed * time / 2? What's the difference between them?

One is correct, yours not... multiplication vs division.