Skip to content

Instantly share code, notes, and snippets.

@Cybermaxke
Created March 13, 2018 13:06
Show Gist options
  • Save Cybermaxke/d523564bd24ed09cd28fc742e81917c8 to your computer and use it in GitHub Desktop.
Save Cybermaxke/d523564bd24ed09cd28fc742e81917c8 to your computer and use it in GitHub Desktop.
import RPi.GPIO as gpio
import time
try:
# Change to board mode
gpio.setmode(gpio.BCM)
# The sensor is attached to pin 18 and 17
triggerPin = 17
echoPin = 18
gpio.setup(triggerPin, gpio.OUT)
gpio.setup(echoPin, gpio.IN)
while True:
gpio.output(triggerPin, gpio.HIGH)
time.sleep(0.00001) # Wait 10 µs
gpio.output(triggerPin, gpio.LOW)
while not gpio.input(echoPin):
continue
startTime = time.time()
while gpio.input(echoPin):
continue
interval = time.time() - startTime
distance = interval * 17000
print('Distance: ' + str(distance))
time.sleep(0.5)
finally:
# Cleanup
gpio.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment