Created
March 13, 2018 13:06
-
-
Save Cybermaxke/d523564bd24ed09cd28fc742e81917c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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