DHT11 Sensor With Raspberry PI and Python

4.7
(19)

Last Updated on 9th June 2024 by peppe8o

In this article, I will show you how to interface a Raspberry PI with DHT11 Sensor using Python.

Getting environmental conditions is the first and simplest task required to create more complex electronic automations. Raspberry PI with DHT11 sensor can get temperature and humidity values with cheap pieces

DHT11 sensor

The DHT11 Sensor, which acronym stands for “Digital Humidity and Temperature”, is a cheap solution to measure indoor Temperature and Humidity (it is not indicated for outdoor uses). It can be used with Arduino (usually in Weather Station projects or similar) or alone, directly connected to a Raspberry PI.

To configure the DHT11 humidity sensor on a Raspberry Pi you need to connect the VCC and GND pins to +5V and GND from Raspberry Pi and the Data Pin of DHT11 to a GPIO PIN of the Raspberry Pi, according to the following wiring diagram.

Another common sensor, with a similar name, is the DHT22. The difference between DHT11 and DHT22 is that the DHT22 is the more expensive version, but allows a better humidity measuring range (from 0 to 100% with 2-5% accuracy), while the DHT11 real humidity range is from 20 to 80% with 5% accuracy.

For this article, I’m going to use a Raspberry PI Zero W with a soldered header. But this article applies also to other Raspberry PI computer boards.

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Raspberry PI Zero WH board
Raspberry PI Zero WH board

You may also find it useful to evaluate a sensor kit which is a cost-saving solution if you want to test many sensors for different purposes.

Step-by-Step Procedure

Wiring Diagram

Please refer to the following picture to connect Raspberry PI with the DHT11 sensor:

RPI DHT11 wiring

Note that the DHT11 signal (Data) pin is connected to RPI pin 11, which is the GPIO 17 (refer to Raspberry PI Zero Pinout for complete schema).

Install OS (Raspberry PI OS Lite)

Start preparing your Operating System. You can install the fast Raspberry PI OS Lite (without a Desktop environment) or opt for Raspberry PI OS Desktop, using its internal terminal.

Make sure your OS is up to date with the following commands:

sudo apt update -y && sudo apt upgrade -y

We also need to get pip3 to install the required libraries:

sudo apt install python3-pip -y

Create virtual environment

In order to install the required Python libraries, we have to enable a virtual environment. I suggest you to read my articles on this topic to get info on how to use virtual environments also with cron tasks:

Beginner’s Guide to Use Python Virtual Environment with Raspberry PI. We can create and activate our virtual environment with the following commands:

python3 -m venv my_project --system-site-packages
source ./my_project/bin/activate

Install CircuitPython-DHT

The following adafruit dht library will be required to enable communication of Raspberry PI with DHT11 sensor:

pip3 install adafruit-circuitpython-dht

Create a new reading script named DHT11Test.py:

nano DHT11Test.py

Insert the following code:

import time
import board
import adafruit_dht
#Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D17)
while True:
    try:
         # Print the values to the serial port
         temperature_c = dhtDevice.temperature
         temperature_f = temperature_c * (9 / 5) + 32
         humidity = dhtDevice.humidity
         print("Temp: {:.1f} F / {:.1f} C    Humidity: {}% "
               .format(temperature_f, temperature_c, humidity))
    except RuntimeError as error:     # Errors happen fairly often, DHT's are hard to read, just keep going
         print(error.args[0])
    time.sleep(2.0)

And finally, execute it by typing:

 python3 DHT11Test.py

This code will show measured data and also make you aware that the DHT11 sensor naturally receives a number of reading errors (as also referenced in adafruit comment inside the script):

pi@raspberrypi:~ $ python3 DHT11Test.py
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Checksum did not validate. Try again.
Temp: 66.2 F / 19.0 C    Humidity: 56%
A full buffer was not returned.  Try again.
Temp: 66.2 F / 19.0 C    Humidity: 56%
Checksum did not validate. Try again.
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%
Temp: 66.2 F / 19.0 C    Humidity: 56%

To have a clean list of correct measurements from Raspberry PI with DHT11, change inside code the line “print(error.args[0])” with “pass”. This avoids printing errors on terminal and outputs readings when it is correctly received:

pi@raspberrypi:~ $ python3 DHT11Test.py
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%
Temp: 66.2 F / 19.0 C    Humidity: 55%

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.7 / 5. Vote count: 19

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?