Raspberry PI DHT22 humidity sensor with a LCD 16×2 display – Weather station

For a workshop I’m going to give about Raspberry PI and how to interact with the outside world, I thought for a start, why not something simple like some kind of a Weather Station.


1x Raspberry PI (I’m using b+)

1x LCD 16×2

1x DHT22 (or DHT11 or am2303) sensor

1x 10K potentiometer

1x 10K resistor

1x breadboard

Here’s the schematic in Fritzing




Sensor Arduino PINs
LCD display Board BCM
2 5v
3 Potentiometer
4 7 GPIO27
6 9 GPIO22
11 22 GPIO25
12 18 GPIO24
13 16 GPIO23
14 12 GPIO18
15 5v
16 7 GPIO4
8  GPIO14

After the wiring, time to connect the PI

Note: Using this wiring and the Adafruit’s LCD libraries, the LCD display will not light up when connecting the PI. After testing the example and running the python script, the LCD will light up and start displaying information




For the sensor to work, we need the DHT libraries developed by Adafruit and setup the environment.

NOTE: As stated by Adafruit, this driver is work in progress

First, set up your development environment

Now, clone the Adafruit libraries

After cloned, enter the directory

Install the libraries

Everything now should be installed. Let’s try the sensor

Now, execute the test file without arguments to see the options available

We’re using DHT22 and connected to the GPIO PIN 4, so, our command line will be:

And the result:

LCD 16×2


With this method, we’re not going to use i2c or UART, but Adafruit’s libraries for LCD. They are easy to install and easy to work with.

Note: If you just want to connect the LCD, refere to this post.

Install the necessary packages

Upgrade distribute

Use PIP to install the GPIO packages

Install the necessary software modules

Try with an example

Now, you can see information on the LCD display

If you want to check the API of the LCD Adafruit’s Python Library:


The Code

The final product





5 Replies to “Raspberry PI DHT22 humidity sensor with a LCD 16×2 display – Weather station

  1. is there a way to include the bmp180 pressure sensor in this setup to display on the lcd. I already have the bmp180 sensor working but i want to include it in the above code to also display.

    1. Hi Kelvin!

      Yes, you only have to change the sensor and replace the output from the DHT to the BMP180

      IE: instead of :
      # Get values
      humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
      replace with the functions from the BMP180 to get the values… and if you change the variables (humidity and temperature) just replace the same in:
      temp = “{:0.1f}*C”.format(temperature)
      hum = “{:0.1f}%”.format(humidity)

      atention to the formaters

      Best regards

  2. hello there, why is there an error on line35 valueError: Unknown format code ‘f’ for object of type ‘str’

  3. Getting the same error as kelvin using code copied from above on a fresh installation of raspbian stretch lite. Followed all the setup instructions and Adafruit example code works fine!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.