from machine import Pin, I2C, time_pulse_us
import time
from pico_i2c_lcd import I2cLcd
trig = Pin(15, Pin.OUT)
ech = Pin(14, Pin.IN)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
i2c.scan()
LCD_I2C_ADDRESS = 0x27
LCD_ROWS = 2
LCD_COLS = 16
lcd = I2cLcd(i2c, LCD_I2C_ADDRESS, LCD_ROWS, LCD_COLS)
while True:
trig.value(1)
time.sleep_us(10)
trig.value(0)
duration = time_pulse_us(ech, 1)
dist = (duration * 0.0343)/2
lcd.clear()
lcd.putstr("Dist: {:.1f} cm".format(dist))
time.sleep(1)