Arduino+ultrasonic sensor

Please guide me regarding use of Arduino+ultrasonic sensor.. i am unable to use ultrasonic sensor properly....

Why not? Describe your problem.

i am working with ultrasonic sensor and arduino uno but i didn't get any result in it.... on serial monitor
please solve this

khanfarooq112:
i am working with ultrasonic sensor and arduino uno but i didn't get any result in it.... on serial monitor
please solve this

If you want help, start by posting your code. Between code tags of course. Like this:-

[code]Paste your code here[/code]
It will appear in a block like this

Post your code and a circuit diagram.... do you really think anyone can help if we don't know what you've done?

int trigPin=13;
int echoPin=11;
float pingTime;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
  long duration, inches, cm;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  pingTime = pulseIn(echoPin, HIGH);
  
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2 ;
}

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

It would help if you can confirm the ultrasonic sensor that you have. I think everyone assumes that it is an HCSR-04 with 4 pins. Is that correct?

You save the result in pingTime and you used the duration for the end result, how to get it work?

pingTime = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

then what would i do with pingTime??

khanfarooq112:
then what would i do with pingTime??

This:-

inches = microsecondsToInches(pingTime);
cm = microsecondsToCentimeters(pingTime);

You need to use either pingTime or duration for everything, but not both.
There's no point in getting the initial reading in pingTime, then passing the uninitialised 'duration' to the above functions. (As Bill pointed out.)
You can completely remove 'duration' - it does nothing.

Edit: Also, this 2000us delay is overkill. You only need to make this pin low for about 2 microseconds before beginning the trigger pulse:-
(Take a look at the IDE's "Ping" example.)

digitalWrite(trigPin, LOW);
delayMicroseconds(2000);