I took a piece of code from the internet in order to use my ultrasonic sensor. But I don't fully understand it. I understand that the lines:
GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False
keeps the bip for 0.00001 seconds, then the pulse_start it the timestamp when the sensor starts receiving the echo and pulse_end is the timestamp when the sensor finishes receiving the echo.
Shoudn't the pulse_start be the timestamp when the beep ended, and pulse_end be the timestamp when echo receiving ended?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) TRIG = 23 ECHO = 24 print "Distance Measurement In Progress" GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.output(TRIG, False) print "Waiting For Sensor To Settle" time.sleep(2) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) while GPIO.input(ECHO)==0: pulse_start = time.time() while GPIO.input(ECHO)==1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) print "Distance:",distance,"cm" GPIO.cleanup()