#Telling Distance using Ultrasonic Sensor
from machine import Pin
import utime
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
def myUltra():
trigger.low() #Turn off pulse
utime.sleep_us(2)
trigger.high() #Turn on pulse
utime.sleep_us(5)
trigger.low()
#a loop for checking if receiver received any signal
while echo.value() == 0:
signaloff = utime.ticks_us() #store the current time stamp
while echo.value() == 1:
signalon = utime.ticks_us() #store the current time stamp
TotalTime = signalon - signaloff
distance = (TotalTime * 0.0343) / 2 #0.0343 is speed of sound in cm/us
print("The distance from object is ",distance,"cm")
while True:
myUltra()
utime.sleep(1)