from machine import Pin, time_pulse_us
from gpio_lcd import GpioLcd
from time import sleep, sleep_us, sleep_ms
SOUND_SPEED=340 # sound speed in the air
TRIG_PULSE_DURATION_US=10
trig_pin = Pin(15, Pin.OUT) # Pico GP15 pin
echo_pin = Pin(14, Pin.IN) # Pico GP14 pin
lcd = GpioLcd(rs_pin = Pin(0),
enable_pin = Pin(1),
d4_pin = Pin(2),
d5_pin = Pin(3),
d6_pin = Pin(4),
d7_pin = Pin(5),
num_lines = 2, num_columns = 16)
def us_sensor():
trig_pin.value(0)
sleep_us(5)
# 10 µs impulse
trig_pin.value(1)
sleep_us(TRIG_PULSE_DURATION_US)
trig_pin.value(0)
ultrason_duration = time_pulse_us(echo_pin, 1, 30000) # Returns the propagation time of the wave. (µs)
distance_cm = SOUND_SPEED * ultrason_duration / 20000
return distance_cm
while True:
dist_cm = us_sensor()
lcd.move_to(0, 0)
lcd.putstr("UltSnd Distance")
lcd.move_to(0, 1)
dist_fmt = "{0:.3f} cm".format(dist_cm)
lcd.putstr(dist_fmt)
print(f"Distance : {dist_cm} cm")
sleep_ms(500)
lcd.move_to(len(dist_fmt)-2,1)
lcd.putstr(" ")