from machine import Pin, I2C
import time
import utime
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
TRIG = Pin(3, Pin.OUT)
ECHO = Pin(2, Pin.IN)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
def distanz_messen():
TRIG.low()
utime.sleep_us(2)
TRIG.high()
utime.sleep_us(10)
TRIG.low()
while ECHO.value() == 0:
start = utime.ticks_us()
while ECHO.value() == 1:
end = utime.ticks_us()
dauer = utime.ticks_diff(end, start)
distanz = (dauer * 0.0343) / 2
return distanz
lcd.clear()
lcd.putstr("Entfernung:")
while True:
abstand = distanz_messen()
lcd.move_to(0, 1)
lcd.putstr("{:.1f} cm ".format(abstand))
time.sleep(0.5)