Skip to content

Instantly share code, notes, and snippets.

@winhtut
Created July 16, 2019 07:25
Show Gist options
  • Save winhtut/43f7cdc1014e5833dd369766ad397948 to your computer and use it in GitHub Desktop.
Save winhtut/43f7cdc1014e5833dd369766ad397948 to your computer and use it in GitHub Desktop.
rpi simple ultrasonic
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