Created
July 16, 2019 07:25
-
-
Save winhtut/43f7cdc1014e5833dd369766ad397948 to your computer and use it in GitHub Desktop.
rpi simple ultrasonic
This file contains hidden or 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 | |
GPIO.setwarnings(False) | |
GPIO.cleanup() | |
GPIO.setmode(GPIO.BCM) | |
TRIG = 4 | |
ECHO = 18 | |
GPIO.setup(TRIG,GPIO.OUT) | |
GPIO.setup(ECHO,GPIO.IN) | |
def get_distance(): | |
GPIO.output(TRIG, True) | |
time.sleep(0.00001) | |
GPIO.output(TRIG, False) | |
while GPIO.input(ECHO) == False: | |
start = time.time() | |
while GPIO.input(ECHO) == True: | |
end = time.time() | |
signal_time = end-start | |
distance = signal_time / 0.000058 | |
return distance | |
while True: | |
distance = get_distance() | |
time.sleep(1) | |
print(distance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment