from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04
from time import sleep
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #initializing the I2C method for ESP8266
sensor = HCSR04(trigger_pin=33, echo_pin=32)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
while True:
distance = sensor.distance_cm()
lcd.move_to(0,0)
lcd.putstr('{:^16}'.format('Distance'))
lcd.move_to(3,1)
lcd.putstr('{:<06.2f} cm'.format(distance))
sleep(1)
lcd.clear()