1

I am following this worksheet and I am struggling to understand the following piece of code:

# Stop when the Echo pin is no longer high - the end time
        while GPIO.input(pinEcho) == 1:
            StopTime = time.time()
            # If the sensor is too close to an object, the Pi cannot
            # see the echo quickly enough, so we have to detect that
            # problem and say what has happened.
            if StopTime - StartTime >= 0.04:
                print("Hold on there!  You're too close for me to see.")
                StopTime = StartTime
                break

I have implemented the worksheet's instructions and the code and when the sensor is very close to the object, I see the ...You're too close... message on the console. However, the condition StopTime - StartTime >= 0.04 and the associated statement ...You're too close.... make no sense to me.

If StopTime - StartTime is greater than a specific value, does it not mean it is taking longer than we want for the pulse to reach the Echo pin? Does that not in turn mean that the object is too far (rather than too close) for the sensor to detect it?

As far as I know, the echo pin is initially zero. When it gets the return pulse, the echo pin changes from 0 to 1. We record this instant as the start time. We then wait for the echo pin to change back to 0 from 1. This instant is recorded as the end time. This StopTime - StartTime check is happening while the echo pin is 1 and we are waiting for this pin to become 0. So if the echo pin is 1, the echo pin has already sensed the return pulse, hasn't it?

a_sid
  • 141
  • 8
  • Oh does this condition mean that if the sensor is too close to the object, the Echo pin is not getting the chance to go from 1 to 0 AND while it stays at 1, 0.04 seconds or more have passed? – a_sid May 09 '21 at 22:12

1 Answers1

1

If the time is longer than 0.04 then it means that the Pi "missed" the return because it wasn't fast enough to see the rapid return. It's basically a sanity check because after 0.04s it would mean the return pulse was missed or the object is physically out of range of the sensor.

tavis
  • 275
  • 2
  • 7
  • Thank you for the answer. _...it would mean the return pulse was missed or the object is physically out of range of the sensor._ I understand the _physically out of range_ part. – a_sid May 10 '21 at 00:10
  • As far as I know, the echo pin initially zero. When it gets the return pulse, the echo pin changes from 0 to 1. We record this instant as the start time. We then wait for the echo pin to change back to 0 from 1. This instant is recorded as the end time. – a_sid May 10 '21 at 00:13
  • This `StopTime - StartTime` check is happening while the echo pin is 1 and we are waiting for this pin to become 0. – a_sid May 10 '21 at 00:15
  • So if the echo pin is 1, the echo pin has already sensed the return pulse, hasn't it? – a_sid May 10 '21 at 00:16
  • it might be the sensor that has missed the return pulse, because the object was too close/far away. In that case, the output would remain high for a while until the sensor decided that it had missed the pulse. Its not clear from the (very crap) datasheet exactly how the logic works - does the pin only go high when the pulse has been detected? – BeB00 May 10 '21 at 00:40
  • 1
    https://lastminuteengineers.com/arduino-sr04-ultrasonic-sensor-tutorial/ this suggests that the signal does go high and stay high until the return pulse gets detected. Its still not clear exactly when the sensor will cut itself off if no pulse is detected, but presumably its >0.04ms (its got to be at least 25ms for 400cm) – BeB00 May 10 '21 at 00:44
  • @BeB00 Thank you for the response. The link in your last comment was helpful. – a_sid May 10 '21 at 01:45
  • @BeB00 The following is stated in your link: `In case, If those pulses are not reflected back then the Echo signal will timeout after 38 mS (38 milliseconds) and return low.` – a_sid May 10 '21 at 01:48
  • @BeB00 This may answer your question in your last comment: _Its still not clear exactly when the sensor will cut itself off if no pulse is detected, but presumably its >0.04ms (its got to be at least 25ms for 400cm)_ It is 0.04 seconds and NOT 0.04 milliseconds. – a_sid May 10 '21 at 01:49
  • @BeB00 In your link it says: `The eight ultrasonic pulses travel through the air away from the transmitter. Meanwhile the Echo pin goes HIGH to start forming the beginning of the echo-back signal.` So does this mean the Echo pin becomes high by itself? Does it not initially need any trigger to go from 0 to 1? – a_sid May 10 '21 at 01:55
  • @a_sid that was a typo, i meant 0.04s. Yes, the echo pin becomes high by itself, i.e. the pin goes high *before* the return pulse is sensed. – BeB00 May 10 '21 at 18:44
  • @BeB00 Thank you for helping me understand this mechanism. I think it will be more helpful for all of us if you compile your comments and post them as an answer. – a_sid Jun 12 '21 at 22:28