Ultrasonic sensor showing half the distance

I am using HC-SR04 & A6 GSM module with Arduino Pro mini(5v). The distance from sensor is always the nearly half of the actual distance.

Say the actual distance is 80cm’s, the result is showing 40cm’s. I have checked the DC supply it is 5v.

Here is the code.

char phone_no[]="9686555969";
#define echoPin 7
#define trigPin 8
char buf[50];
int ATimer;
void setup()
{
Serial.begin(115200);  
delay(200);
}

void loop()
{
  // send a pulse on the trigger pin to initiate measurement
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  long duration, inches, cm;
  // the length of the pulse on the echo pin is proportional to the distance
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  Serial.println(duration);
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.println("AT+CMGF=1");    
  delay(2000);
  Serial.print("AT+CMGS=\"");
  Serial.print(phone_no); 
  Serial.write(0x22);
  Serial.write(0x0D);  // hex equivalent of Carraige return    
  Serial.write(0x0A);  // hex equivalent of newline
  delay(2000);
  sprintf(buf,"The distance is %lu inches",inches);
  delay(500);
  Serial.println (char(26));//the ASCII code of the ctrl+z is 26
  delay(100);

}


long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Print the duration you are getting to see if that is erroneous - perhaps you have a reflection from
a nearer object?

Tried to print that to, the duration itself is showing 2180, while i placed the sensor downfacing to a floor having no objects in between. Actual distance is 80cm’s it shows 39cm’s.

That is correct, a duration of 2.18ms is 74cm of travel or 37cm each way.

You're not using an 8MHz board flashed with a 16MHz runtime are you?

When i use RAW to check the regulator. The board is regulated to 3.3v, then is guess it is 8Mhz board I am using 5v, since the sensor & GSM module needs 5v supply.