… using a Raspberry Pi 4, a DHT22 temperature and humidity sensor, and a LCD display.
I am using a SunFounder IIC I2C TWI 1602 Serial LCD Display with integrated I2C module to display measurements.
Let’s wire up all parts together. Connect the DHT22 sensor to GPIO pins of the Raspberry Pi, using the following wiring:
DHT22 SENSOR PINS | GPIO PINS |
---|---|
VCC (+) | 5V0 |
GND (-) | GND |
DATA | GP04 |
Connect the SDA pin of the LCD display to the SDA1 pin of the Raspberry Pi, and the SCL pin to the SCL1 pin on the Pi:

LCD PINS | GPIO PINS |
---|---|
SDA | SDA1 |
SCL | SCL1 |
VCC | 5V0 |
GND | GND |
Installing libraries
Now we’ll need to install some libraries and tools. Turn on your Raspberry Pi 4, and run the commands:
pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
Check that your LCD display is connected, reboot your Raspberry Pi, and type the following command to see all the devices connected to the I2C bus:
i2cdetect -y 1
You will see a table of addresses for each I2C device connected to your Raspberry Pi:
The I2C address of my LCD is 27. You can see another address. Write it down – we’ll need it later.
The code
Clone the azure-iot-blog GitHub repository into a folder in your Raspberry Pi:
git clone https://github.com/jevgenij-p/azure-iot-blog.git
Open the raspberry/thermometer/
folder. You will see two files:
Open the I2C_LCD_driver.py
file in your favorite editor and find the Line 22:
# i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
I2CBUS = 1
# LCD Address
ADDRESS = 0x27
import smbus
from time import sleep
As I mentioned before, my I2C address is 27. If your address is different – put the I2C address of your LCD in line 22, and save the code.
Our program, measuring temperature and humidity, is measure_temperature.py
:
import time
import board
import adafruit_dht
import I2C_LCD_driver
DELAY = 2.0
def main():
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
lcd = I2C_LCD_driver.lcd()
print("Measurement started. Press Ctrl-C to exit")
while True:
try:
temperature = dhtDevice.temperature
humidity = dhtDevice.humidity
print(f"Temp: {temperature:.1f} C Humidity: {humidity}% ")
lcd.lcd_display_string("Temp: {:.1f}{} C".format(temperature, chr(223)), 1)
lcd.lcd_display_string("Humidity: {:.1f}%".format(humidity), 2)
except KeyboardInterrupt:
dhtDevice.exit()
raise SystemExit
except RuntimeError as error:
print(error.args[0])
time.sleep(DELAY)
continue
time.sleep(DELAY)
if __name__ == '__main__':
main()
Line 10 is initializing a DHT22 sensor, using Adafruit Blinka (CircuitPython) library. The sensor is connected to GP04 pin.
Line 11 is initializing the LCD. Lines 20 and 21 print text to the screen. Last parameter (1 or 2) of the lcd.lcd_display_string()
method is a row number.
Now you can run the example:
python3 measure_temperature.py
If everything is connected and configured correctly, you will see temperature and humidity on your LCD display:
Useful links
Installing CircuitPython Libraries on Raspberry Pi or BeagleBone Black
How to setup an I2C LCD on the Raspberry Pi
One thought on “How to make a thermometer…”