import machine
from machine import Pin,PWM
from time import sleep_us,sleep
buzzer=Pin(4,Pin.OUT)
buzzer_pwm=PWM(buzzer)
echo=Pin(18,Pin.IN)
trig=Pin(5,Pin.OUT)
while True:
trig.value(0)
sleep_us(2)
trig.value(1)
sleep_us(10)
trig.value(0)
x=machine.time_pulse_us(echo,1)
distance=(0.034*x)/2
print('Distance:', distance, 'cm')
if distance<50:
buzzer_pwm.freq(500)
buzzer_pwm.duty(100)
sleep(0.1)
buzzer_pwm.duty(0)
sleep(0.1)
sleep(1)
#https://randomnerdtutorials.com/micropython-hc-sr04-ultrasonic-esp32-esp8266/