Skip to content

Instantly share code, notes, and snippets.

@bcopy
Created October 5, 2016 16:37
Show Gist options
  • Save bcopy/84bb9cfaa87e968997086ad614ddb9e6 to your computer and use it in GitHub Desktop.
Save bcopy/84bb9cfaa87e968997086ad614ddb9e6 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 17
ECHO = 18
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment