from machine import I2C, Pin
from utime import sleep
from pico_i2c_lcd import I2cLcd
from dht import DHT22
# Configuração do barramento I2C nos pinos GP8 (SDA) e GP9 (SCL)
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
sensor = DHT22(Pin(16))
button = Pin(18, Pin.IN)
# Descobre o endereço I2C automaticamente (geralmente é 0x27 ou 0x3F)
I2C_ADDR = i2c.scan()[0]
# Cria o objeto do display LCD com 2 linhas e 16 colunas
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# Mensagem inicial
lcd.move_to(0,0)
lcd.putstr("LCD")
lcd.move_to(0,1)
lcd.putstr("com DHT22")
sleep(2)
lcd.clear()
while True:
sensor.measure()
temperatura = sensor.temperature()
umidade = sensor.humidity()
if button.value() == 1:
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Temperatura:")
lcd.move_to(0, 1)
lcd.putstr(f"{temperatura}C")
sleep(2)
else:
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Umidade:")
lcd.move_to(0, 1)
lcd.putstr(f"{umidade}%")
sleep(2)