from machine import Pin, I2C
import time
import dht
import i2c_lcd # Assuming you have i2c_lcd.py and lcd_api.py libraries
# Initialize I2C on GP0 and GP1 (SDA, SCL)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000) # Adjust I2C pins and frequency as needed
# Initialize the LCD screen
lcd = i2c_lcd.I2cLcd(i2c, 0x27, 2, 16) # (I2C, address, rows, columns)
# Initialize the DHT sensor on GPIO pin 15
sensor = dht.DHT22(Pin(15)) # Change to DHT11 if using that sensor
# Function to read temperature and humidity and display it on LCD
def display_temp_humidity():
try:
# Read sensor data
sensor.measure() # Trigger the sensor to measure temperature and humidity
temp = sensor.temperature() # Temperature in Celsius
humidity = sensor.humidity() # Humidity in percentage
# Clear the LCD
lcd.clear()
# Display temperature and humidity on the LCD
lcd.putstr("Temp: {}C".format(temp))
lcd.move_to(0, 1) # Move to the second line
lcd.putstr("Humidity: {}%".format(humidity))
except Exception as e:
lcd.clear()
lcd.putstr("Sensor Error!")
# Main loop to keep updating the display
while True:
display_temp_humidity()
time.sleep(2) # Update every 2 seconds