title: Getting Distance in Inches and Cm from Ultrasonic Sensor in Arduino
body: <p>I am tinkering with a simple Arduino sketch that detects distances using an ultrasonic sensor. From what I've understood, the trigger sends a ping. The echo listens for an echo that comes back to calculate the distance. This is measured in microseconds. </p>
  
  <p>If that is correct, the question is, how can one determine inches and cm from that ping (that is given in microseconds)? Is this just basic math/physics, and is there just a basic formula for this?</p>
  
  <pre><code>void loop() {
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2); 
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10); 
   digitalWrite(trigPin, LOW);
  
   duration = pulseIn(echoPin, HIGH);
  </code></pre>
  
  <p>This is the part that I'm having issues with. I want to divide ping time from microseconds to inches and then cm:</p>
  
  <pre><code> distance_in = duration ????;
   distance_cm = duration ????;
  
   Serial.print(distance_in);
   Serial.print(" in, ");
   Serial.print(distance_cm);
   Serial.print(" cm");
   Serial.println();
   delay(1500);
  }
  </code></pre>
  
  <p>Any help, with as much explanation as possible would be appreciated. I'm a newbie to Arduino (and not a heavy-lifter in math)</p>