Raspberry Pi Pico - DHT22 - LCD

We will learn how to program a Raspberry Pi Pico to measure temperature and humidity using a DHT22 sensor and display these readings on an I2C LCD screen.

Raspberry Pi Pico DHT22 LCD I2C

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LCD I2C
1×DHT22 Temperature Humidity Sensor Module
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT22 and LCD

Explore tutorials below on the DHT22 sensor and LCD to understand their pin configurations, features, and how to program them.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico DHT22 LCD

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - DHT22 Sensor - LCD I2C

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-dht22-lcd """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C import time import dht # The Raspberry Pi Pico pin (GP15) connected to the DHT22 sensor DHT22_PIN = 15 # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Use the address found using the I2C scanner # Define the number of rows and columns on your LCD LCD_ROWS = 2 LCD_COLS = 16 # Initialize the DHT22 sensor DHT22 = dht.DHT22(machine.Pin(DHT22_PIN)) # Initialize I2C i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Setup function lcd.backlight_on() lcd.clear() # Custom character for the degree symbol degree_char = chr(0xDF) # Main loop: Read data from the DHT22 sensor and display on LCD every 2 seconds while True: try: DHT22.measure() temperature = DHT22.temperature() # Gets the temperature in Celsius humidity = DHT22.humidity() # Gets the relative humidity in % print("Temperature: {:.2f}°C, Humidity: {:.2f}%".format(temperature, humidity)) lcd.clear() lcd.set_cursor(0, 0) # Move to the beginning of the first row lcd.print("Temp: {:.2f}{}C".format(temperature, degree_char)) lcd.set_cursor(0, 1) # Move to the beginning of the second row lcd.print("Humi: {:.2f}%".format(humidity)) except OSError as e: print("Failed to read from DHT22 sensor:", e) time.sleep(2)

※ NOTE THAT:

The LCD I2C address can vary depending on the manufacturer. In our code, we used the address 0x27, which was given to us by the manufacturer, DIYables.

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the DHT22 module and the LCD I2C according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LCD-I2C”, then find the LCD I2C library created by DIYables.
  • Click on DIYables-MicroPython-LCD-I2C, then click Install button to install LCD I2C library.
Raspberry Pi Pico LCD I2C library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the message in the Shell at the bottom of Thonny.
  • Make the area around the sensor warmer or cooler.
  • Watch what happens on the LCD screen.

If the LCD screen displays nothing, check here: Troubleshooting on LCD I2C

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!