from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from hcsr04 import HCSR04
from time import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# ESP32 Pin assignment
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
sensor = HCSR04(trigger_pin = 23, echo_pin = 19, echo_timeout_us = 100000)
#timeout is the maximum travel time of the sound wave when the sensor is probable out of range
I2C_ADDR = 0x3c
oled_width = 128
oled_height = 64
I2C_ADDR2 = 0x27
totalRows = 2
totalColumns = 16
oled = SSD1306_I2C(oled_width, oled_height, i2c, I2C_ADDR)
lcd = I2cLcd(i2c, I2C_ADDR2, totalRows, totalColumns)
while True:
distance = sensor.distance_cm()
print('Distance: ', distance)
oled.text('Distance: ', 0,0,1)
oled.text(str(distance), 40,20,1)
lcd.putstr(distance)
oled.show()
sleep(1)