from machine import Pin, I2C
from pico_i2c_lcd import I2cLcd
import dht
import time
# Khởi tạo I2C cho LCD
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Cảm biến DHT22 nối vào GP16
sensor = dht.DHT22(Pin(16))
# Thông báo khởi động
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Khoi dong...")
time.sleep(1)
lcd.clear()
while True:
try:
# Đọc dữ liệu từ cảm biến
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# Xóa LCD trước khi ghi dữ liệu mới
lcd.clear()
# Dòng 1 hiển thị nhiệt độ
lcd.move_to(0, 0)
lcd.putstr("T:{:.1f}C".format(temp))
# Dòng 2 hiển thị độ ẩm
lcd.move_to(0, 1)
lcd.putstr("H:{:.1f}%".format(hum))
except Exception:
# Nếu đọc lỗi thì hiện thông báo
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Doc DHT loi")
# Đúng yêu cầu của đề là cập nhật mỗi phút
time.sleep(60)