Raspberry Pi - Humidity Sensor - LCD
This tutorial instructs you how to use Raspberry Pi to read temperature and humidity DHT22 from sensor and display them on LCD I2C.
|Raspberry Pi 4 Model B
|DHT11 Temperature and Humidity Sensor
|DHT22 Temperature and Humidity Sensor
|(Optional) Screw Terminal Adapter for Raspberry Pi
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
Overview of DHT22 and LCD
If you are unfamiliar with the DHT22 temperature humidity sensor and LCD (including pinout, how it works, and how to program), the following tutorials can help you learn:
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code - DHT22 Sensor - LCD I2C
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Prior to utilizing the LCD I2C with a Raspberry Pi, we need to enable I2C interface on Raspberry Pi. See How to enable I2C interface on Raspberry Pi
- Install the LCD I2C library by running the following command:
- Install the library for DHT11 temperature and humidity sensor by running the following command:
- Create a Python script file DHT22_LCD.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
- Change the temperature of the environment around the sensor.
- Check out the result on the LCD.
※ NOTE THAT:
The address of the LCD may differ depending on the manufacturer. We used 0x27 in our code, which is specified by DIYables as the address.