Skip to content

Instantly share code, notes, and snippets.

@pzula
Created March 12, 2016 21:04
Show Gist options
  • Save pzula/a5a16e624a5abb47ead3 to your computer and use it in GitHub Desktop.
Save pzula/a5a16e624a5abb47ead3 to your computer and use it in GitHub Desktop.
Ultrasonic sensor
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
timeout = 0.020
while 1:
GPIO.setup(11, GPIO.OUT)
#cleanup output
GPIO.output(11, 0)
time.sleep(0.000002)
#send signal
GPIO.output(11, 1)
time.sleep(0.000005)
GPIO.output(11, 0)
GPIO.setup(11, GPIO.IN)
goodread=True
watchtime=time.time()
while GPIO.input(11)==0 and goodread:
starttime=time.time()
if (starttime-watchtime > timeout):
goodread=False
if goodread:
watchtime=time.time()
while GPIO.input(11)==1 and goodread:
endtime=time.time()
if (endtime-watchtime > timeout):
goodread=False
if goodread:
duration=endtime-starttime
distance=duration*34000/2
print distance
Posts: 1
Joined: Thu Mar 05, 2015 5:07 am
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment