title: Arduino ultrasonic distance sensor
body: <p>I am trying to receive information from my sensor, however my output is just 0 all the time, is there something wrong in my code? Everything hardware related is done well.</p>
  
  <pre><code>loop()
  {
      long duration, inches, cm;
  
      pinMode(pingPin, OUTPUT);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);
  
      pinMode(pingPin, INPUT);
      duration = pulseIn(pingPin, HIGH);
  
      inches = microsecondsToInches(duration);
      cm = microsecondsToCentimeters(duration);
  
      Serial.print(inches);
      Serial.print("in; ");
      Serial.print(cm);
      Serial.print("cm");
      Serial.println();
  }
  
  long microsecondsToInches(long microseconds)
  {
      return microseconds / 74 / 2;
  }
  
  long microsecondsToCentimeters(long microseconds)
  {
      return microseconds / 29 / 2;
  }
  </code></pre>