'''
from hcsr04 import HCSR04
from time import sleep
sensor = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)
while True:
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
sleep(1)
'''
#write the script without the library ->interface the ultrasonic sensor with esp32
import machine
from machine import Pin
from time import sleep_us,sleep
echo=Pin(18,Pin.IN)
trig=Pin(5,Pin.OUT)
#x=machine.time_pulse_(echo,1)
#d=(0.0343*x)/2
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 #using 0.034 instead of 0.0343 to get right value
print('Distance:', distance, 'cm')
sleep(1)
#https://randomnerdtutorials.com/micropython-hc-sr04-ultrasonic-esp32-esp8266/