import network
import time
from machine import Pin, SoftI2C
import dht
import ujson
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from hcsr4 import HCSR04
# Create an HCSR04 sensor object
sensor = HCSR04(trigger_pin=13, echo_pin=12, echo_timeout_us=10000)
# Set the I2C address, total rows, and total columns for an LCD display
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
# Create an I2C interface
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
# Initialize an LCD display
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
# Display a "Hello" message on the LCD
lcd.putstr("Lab 5 Bai 2")
time.sleep(1)
lcd.clear()
# Start an infinite loop
while True:
# Measure the distance using the HCSR04 sensor
distance = sensor.distance_cm()
# Check if the distance is less than 50 cm and update the LCD display
if distance < 50:
lcd.clear()
lcd.putstr("The distance: \n")
lcd.putstr(str(distance)+" cm")
# Wait for 1 second
time.sleep(1)
# Clear the LCD display
lcd.clear()