Created
March 21, 2019 11:32
-
-
Save mhashim6/24238eae452527cffdde385a4923bc6c to your computer and use it in GitHub Desktop.
RaspberyPi-ultrasonic
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 | |
TRIGGER = 18 | |
ECHO = 24 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(TRIGGER, GPIO.OUT) | |
GPIO.setup(ECHO, GPIO.IN) | |
try: | |
while True: | |
GPIO.output(TRIGGER, 1) | |
time.sleep(0.0001) | |
GPIO.output(TRIGGER,0) | |
GPIO.wait_for_edge(ECHO, GPIO.RISING) | |
startTime = time.time() | |
GPIO.wait_for_edge(ECHO, GPIO.FALLING) | |
stopTime = time.time() | |
elapsed = stopTime - startTime | |
print("distance: ", (elapsed * 34300)/2, ".") | |
time.sleep(1) | |
except KeyboardInterrupt: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment