How to make a thermometer…

… 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:

VCC (+)5V0

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:


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

Open the raspberry/thermometer/ folder. You will see two files:

Open the 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

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:
            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:
            raise SystemExit
        except RuntimeError as error:

if __name__ == '__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:


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…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s