from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from i2c_lcd import I2cLcd
from time import sleep
from hcsr04 import HCSR04
# ESP32 Pin assignment
I2C_ADDRL = 0x27
totalRows = 4
totalColumns = 20
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
I2C_ADDRO = 0x3c
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, i2c, I2C_ADDRO)
lcd = I2cLcd(i2c, I2C_ADDRL, totalRows, totalColumns)
lcd.putstr("Embedded ESP32 with micropython")
oled.invert(False)
oled.text('ECE663', 0, 0 , 1)
oled.show()
sensor = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=100000)
for i in range(50):
oled.pixel(24+i, 50, 1)
oled.show()
while True:
try:
distance = sensor.distance_cm()
lcd.clear()
lcd.putstr("Distance: {} cm".format(distance))
print('Distance:', distance, 'cm')
except OSError:
print("Retrying...")
sleep(1)