import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO_TRIG = 11 GPIO_ECHO = 18 GPIO.setup(GPIO_TRIG, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) GPIO.output(GPIO_TRIG, GPIO.LOW) time.sleep(2) GPIO.output(GPIO_TRIG, GPIO.HIGH) time.sleep(0.00001) GPIO.output(GPIO_TRIG, GPIO.LOW) while GPIO.input(GPIO_ECHO) == 0: start_time = time.time() while GPIO.input(GPIO_ECHO) == 1: bounce_back_time = time.time() pulse_duration = bounce_back_time - start_time distance = round(pulse_duration * 17150, 2) print(f"Distance: {distance}cm") GPIO.cleanup()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter