from machine import Pin, I2C
import time
from hcsr04 import HCSR04
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# I2C Pin setup for ESP32
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Ultrasonic Sensor setup
sensor = HCSR04(trigger_pin=25, echo_pin=26)
def measure_distance():
distance = sensor.distance_cm()
return distance
def display_distance(distance):
lcd.clear()
lcd.putstr("Distance:\n{} cm".format(distance))
print('Distance:', distance, 'cm')
def main():
while True:
distance = measure_distance()
display_distance(distance)
time.sleep(5)
if __name__ == "__main__":
main()