2

Here is a code snipet for ultrasonic sensor

digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);

duration=pulseIn(echopin,HIGH);

Assume trigpin and echopin pins are already defined.

Now according to Arduino reference guide pulseIn funcion:

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the time out.

So shouldn't I be getting 10 microseconds as my answer, since since initial state of echo pin is low, now when the reflected sound hits receiver, echo pin goes high, the timer gets started and when and when the pulse ends the echo pin goes low again and since pulse sent was of 10 microseconds of length, the ans should be 10, but any ultrasonic program I have seen uses this code to find the duration the ultrasonic wave takes since it gets emitted to the time it gets into the receiver.

2

What happens is:

  • You send a pulse of 10 ms with TTL signal. This is the first line in the picture below.
  • Than you call pulseIn. This waits for the echo pulse.
  • The echo pulse will become high when reflected. This happens internally in the module. This is the second line.
  • The echo pulse will stay high depending on the time it took to be echoed back and makes a pin high (echo pin). This is the third line.
  • Than your pulseIn command returns with the time (of the echo pin length).

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.