Neeraj
Neeraj

Reputation: 146

Measuring Distance using ultrasound sensors in raspberry pi

I am trying to use an ultrasound sensor to measure the distance(D) from the sensor(S) to an obstacle(X) . The basic principle is that I am going to send a pulse of sound and receive it back , and using the time it takes to travel from S to X and back (say,T) , I am going to calculate the distance using the following formula : D = ( V * T ) / 2 . (V is the speed of Sound in air). The following is some python code to achieve the same:

#Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO    = 24
print "Ultrasonic Measurement"

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()

while GPIO.input(GPIO_ECHO)==0:
    start = time.time()

while GPIO.input(GPIO_ECHO)==1:
     stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300

# That was the distance there and back so halve the value
distance = distance / 2

I am having difficulty in understanding why the start and stop time are calculated like this. To me , it seems like the start time is the time "when we first get a high signal" and the stop time is the time "when we last get the high signal" , so that their difference will come out to be the time " for which the pulse was high" , which I think will be independent of the distance as the high would have been sent for the same duration each time. I have tried to look at other sources and they all seem to consider this time only , that is , the time for which the input was high for the ECHO sensor. I however , disagree.

It think that code should be like this :

# start time is time when we start sending the signal
start = time.time()
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

while GPIO.input(GPIO_ECHO)==0:
    pass
# stop time is the time when we first get a high on the output.
stop = time.time()

while GPIO.input(GPIO_ECHO)==1:
    pass

# Calculate pulse length
elapsed = stop-start

It seems to me that I am missing something obvious. I will appreciate it if someone can point me to what it is.

Upvotes: 0

Views: 834

Answers (1)

G. Sliepen
G. Sliepen

Reputation: 7973

This is because that is how the ultrasound echo sensor works. You send it a pulse to the GPIO_TRIGGER line. This causes the sensor to start sending a brief pulse of sound. However, that in itself takes some time. Then it has to wait until it has received an echo of that pulse. The output of the sensor goes high when it has finished sending the sound pulse, and it goes low again when it has finished receiving the sound pulse. The time inbetween is the time it takes for the sound pulse to reach some object and get reflected back.

Upvotes: 1

Related Questions