PulseIn() and Ultrasound sensor question

I have mine set up like the tutorials and it works fine but I don't understand why it does.

This is my understanding of it:
-I send a pulse of 10 microseconds
-pulseIn waits to detect the HIGH from the echo coming back

  • measure how long the echo of my impulse stays high

Shouldn't pulseIn always return 10 micro seconds (the amount of time I left the pulse high on the transmit) however far the pulse traveled?

I am thinking that I don't understand pulseIn correctly but all the stuff I read says it waits to detect a HIGH value and measures how long it takes for it to come back to LOW.

pulseIn page: http://www.arduino.cc/en/Reference/PulseIn

This is my understanding of it:
-I send a pulse of 10 microseconds
-pulseIn waits to detect the HIGH from the echo coming back

  • measure how long the echo of my impulse stays high

Not quite.

When you send a pulse the output of the transmitter is also received by the receiver directly, no echo. The after a small delay the receiver stops being deafened by its own transmitter and starts to listen for the echo. The point when the receiver stops being deafened by its own transmitter is when it starts the timing. Because you start pulseIn() while the receiver is being deafened by its own transmitter. That is the state it waits to be changed from before starting timing.

1 Like

That explains the time detection but I haven't seen it explained anywhere this way, even the ardiono.cc definition doesn't match this.

I think a lot of people just paste the 4-5 lines of code and are happy it works :slight_smile:

Thanks

Hi,
I understood that the output was set LOW at the same time of transmission and then set HIGH on receiving the reflection of the U/S burst. The maths show how the time taken to receive the reflection can be converted to distance.

Here's the code I use:

long scanner(long cm)
{
const int pingPin=2, EchoPin=3;
long duration;

// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT);  

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);  // Only 2uS NOT 10!
digitalWrite(pingPin, LOW); 
duration = pulseIn(EchoPin, HIGH);
delay(100);

// convert the time into a distance
// inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return (cm);
//------------------------------------------------------------------
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimetre.
// 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;
}
//---/code]

Hope it helps, regards

Mel.

1 Like