"""
Project :Building a Temperature Monitoring System using
Raspberry Pi Pico, DHT22 Sensor,
and I2C LCD Display
The goal of this project is to create a temperature
monitoring system that utilizes a DHT22 sensor to
measure the temperature and a Raspberry Pi Pico
microcontroller to read the data and display it
on an I2C LCD display.
"""
from machine import I2C, Pin
from time import sleep
from dht import DHT22
from pico_i2c_lcd import I2cLcd
def initialize_i2c_lcd(sda_pin, scl_pin, i2c_freq):
"""Initialize the I2C LCD display with the given parameters."""
i2c_bus = I2C(0, sda=Pin(sda_pin), scl=Pin(scl_pin), freq=i2c_freq)
i2c_address = i2c_bus.scan()[0]
return I2cLcd(i2c_bus, i2c_address, 2, 16), i2c_address
def display_temperature(lcd):
"""Read the temperature from DHT22 sensor and display it on the LCD."""
dht_sensor = DHT22(Pin(7))
lcd.clear()
lcd.putstr("Temperature:")
while True:
dht_sensor.measure()
temp = dht_sensor.temperature()
if isinstance(temp, float):
lcd.move_to(0, 1)
lcd.putstr("{:.1f} C".format(temp))
sleep(1)
def main():
"""Main function to run the I2C LCD display example."""
lcd_display, _ = initialize_i2c_lcd(sda_pin=0, scl_pin=1, i2c_freq=400000)
display_temperature(lcd_display)
if __name__ == '__main__':
main()