import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO. BCM)

TRIG=23
ECHO=24
SPEEDOFSOUND=34500 # incm/s

GPIO.setup(TRIG, GPIO. OUT)
GPIO.setup(ECHO, GPIO. IN)

GPIO.output(TRIG, False)
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 * (SPEEDOFSOUND/2)

distance = round(distance, 2)

print distance


GPIO.cleanup()
