from machine import Pin, time_pulse_us
import time

# Pins define karein
TRIG = Pin(5, Pin.OUT)    # Trigger pin
ECHO = Pin(18, Pin.IN)    # Echo pin

def get_distance():
    # Trigger signal bhejna (10µs high pulse)
    TRIG.value(0)
    time.sleep_us(5)
    TRIG.value(1)
    time.sleep_us(10)
    TRIG.value(0)

    # Echo ka response lena
    duration = time_pulse_us(ECHO, 1)  # Echo pin high hone ka waqt (µs me)
    
    # Distance calculate karna (Speed of sound = 343 m/s)
    distance = (duration * 0.0343) / 2  # cm me distance
    return distance

# Infinite loop
while True:
    dist = get_distance()
    print("Distance:", dist, "cm")
    time.sleep(1)  # 1 second ka delay
