import RPi.GPIO as GPIO     # Importing RPi library to use the GPIO pins
import time         # Importing time library to provide the delays in program
GPIO.setmode(GPIO.BCM)      # Programming the GPIO by Broadcom SOC channel (BCM) pin numbers. (like PIN29 as'GPIO5')

#GPIO.setmode(GPIO.BCM) -> activates board pin numbering

TRIG = 11
ECHO = 12

GPIO.setup(TRIG,GPIO.OUT)      # Declaring TRIG as output pin
GPIO.setup(ECHO,GPIO.IN)       # Declaring ECHO as input pin
GPIO.output(TRIG, False)       # Set the TRIG as LOW

time.sleep(2)                 # Delay of 2 seconds

GPIO.output(TRIG, True)        # Set the TRIG as HIGH

time.sleep(0.00001)            # Delay of 0.00001 seconds
#used to trigger the sensor(ultra sonic sensor module), which will then send out an 8 cycle ultrasonic burst at 40KHz

GPIO.output(TRIG, False)       # Set the TRIG as LOW

while GPIO.input(ECHO) == 0:     # Check whether the ECHO is LOW
    pulse_start = time.time()       # Saves the last known time of LOW pulse

while GPIO.input(ECHO) == 1:     # Check whether the ECHO is HIGH
    pulse_end = time.time()         # Saves the last known time of HIGH pulse

pulse_duration = pulse_end - pulse_start # Get pulse duration to a variable

pulse_duration = round(pulse_duration, 2) # Round to two decimal points

distance = pulse_duration * 17150        # Multiply pulse duration by 17150 to get distance

print ("Distance:",distance,"cm from the sensor")  # Display distance

GPIO.cleanup()              # Clean/reset the GPIO pins/ports used in program
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT