Noobie question about programming for Ultrasonic Range Finder

Hello,

I have just started with programming for the arduino and was playing with my new ultrasound range finder. The code I have is as follows

  digitalWrite(trigger_pin, LOW);
  delay(5);
  digitalWrite(trigger_pin, HIGH);
  delay(10);
  digitalWrite(trigger_pin, LOW);
  duration = pulseIn(echo_pin, HIGH);
  distance_mm = (duration/2)/2.91;

We set trigger pin to HIGH for 10 milliseconds following which we set trigger pin to LOW and call the pulseIn command to determine the time for the sound waves to bounce back.

From the documentation for pulseIn at http://arduino.cc/en/Reference/pulseIn the command pulseIn(echo_pin, HIGH) returns the width of the HIGH pulse in microseconds.

Aren't we trying to determine the length of time for which the echo_pin was set to LOW. ie. the time required for the pulse to return? Why do we measure the width of the HIGH pulse here?

Thanks in advance. Sachin

was playing with my new ultrasound range finder

Which is a . . ?

AWOL:

was playing with my new ultrasound range finder

Which is a . . ?

HC SR04

Your trigger pulse is too long; the spec calls for just 10 microseconds. And yes, you do need to measure the amount of time the echo pin is HIGH.

AWOL: Your trigger pulse is too long; the spec calls for just 10 microseconds. And yes, you do need to measure the amount of time the echo pin is HIGH.

I just went through the spec sheet. (I need to remember to do that first the next time).

Thanks for the correction in the pulse duration. So I had a misconception that the echo pin is directly linked to the pulse received and was therefore confused. ie. I thought that the echo pin goes high when the pulse is received and low when no pulse is bounced back. The sensor simply communicates the time taken for the pulse to bounce back by issuing a HIGH pulse on the echo pin.

Thanks for your help.

Sachin

The echo pulse goes high pretty much as soon as the device is triggered, and goes low as soon as the echo is received.
There may be a deliberate “deaf period” during and just after the outgoing pulse train to prevent direct coupling causing spurious returns, analogous to a pulsed radar’s main bang suppression. (Bats also have main bang suppression by having a mechanism that clamps their eardrum to prevent them deafening themselves!)

AWOL: The echo pulse goes high pretty much as soon as the device is triggered, and goes low as soon as the echo is received. There may be a deliberate "deaf period" during and just after the outgoing pulse train to prevent direct coupling causing spurious returns, analogous to a pulsed radar's main bang suppression. (Bats also have main bang suppression by having a mechanism that clamps their eardrum to prevent them deafening themselves!)

Thanks. That makes sense to me now.

Hi, Lots of Information about Ultrasonic Sensors and working code on the ArduinoInfo.Info WIKI HERE:

And THIS: has test code for an Ultrasonic Sensor plus an example at the end with a Robot that is "Looking Around" with an Ultrasonic Sensor and avoiding collisions.

Terry,

I was able to modify the sources from the page for the robot to cater to my Robot. My Robot has a single DC rear wheel drive and a single servo which contains both steering and the ultrasounic range finder.

It was my first taste of a code using statemachine. The statemachine code simplifies the Robot operations quiet a bit. My next step is to show a further simplified version of this code to my 8 year old and have him modify the calls in each state to change the behaviour of the robot.

Thanks.

Hi, Glad you were able to adapt that code!

I will be rewriting that code to remove the regular "delay()" . After some discussion with the author of the SM State Machine Library, I think I understand how to write a non-blocking state machine. This would allow multiple things to happen "Apparently at the same time" (but not really).. I would like to be better able to "Look Around" while the robot is moving and cause actions to avoid collisions etc. I'll put a note here when I get that working...