Skip to content

Instantly share code, notes, and snippets.

@mhashim6
Created March 21, 2019 11:32
Show Gist options
  • Save mhashim6/24238eae452527cffdde385a4923bc6c to your computer and use it in GitHub Desktop.
Save mhashim6/24238eae452527cffdde385a4923bc6c to your computer and use it in GitHub Desktop.
RaspberyPi-ultrasonic
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