DHT11 Sensor With Raspberry PI and Python

4.6
(15)

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

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

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 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.

Follow peppe8o posts on -> Twitter

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 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 sensors kit (like Elegoo Starter Kit) which is a cost-saving solution if you want to test many sensors for different purposes.

Check hardware prices with the following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon DHT11 umidity temperature box

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 corresponds to GPIO 17 (refer to Raspberry PI GPIO schemas for complete schema).

Install OS (Raspberry PI OS Lite)

Start preparing your Operating System. You can install the fast Raspberry PI OS Lite (without Desktop environment) or you can also 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
sudo apt upgrade

Istall CircuitPython Libraries

CircuitPython libraries are a set of Python libraries from Adafruit which simplify external sensors management. These libraries require Python3. Check if it is installed with the command:

python3 --version

Otherwise, install python3:

sudo apt install python3

Install pip for python3:

sudo apt install python3-pip

Enable I2C and SPI

Enable and configure I2C (standard designed to allow one chip to talk to another)

sudo apt install -y python-smbus
sudo apt install -y i2c-tools

And enable Raspberry PI I2C kernel support with:

sudo raspi-config

In the following config screen, go to Interfacing Options > I2C. Enable it by pressing yes on the next screen and following confirmation. Once enabled, you should be forwarded again to the raspi-config home page.

To enable SPI, go again to Interfacing Options >SPI. Enable it by pressing yes on the next screen and following confirmation.

Click Finish on raspi-config home page to go back ssh terminal.

Install Python Libraries

RPI.GPIO should be already installed (otherwise, you can get it installed with the command “sudo apt install python3-rpi.gpio”).

Finally, install adafruit-blinka library. This includes also a few different libraries such as adafruit-pureio (our ioctl-only i2c library), spidev (for SPI interfacing), Adafruit-GPIO (for detecting your board), and of course adafruit-blinka:

pip3 install adafruit-blinka

Setup CircuitPython-DHT

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

pip3 install adafruit-circuitpython-dht
sudo apt install libgpiod2

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 by typing:

 python3 DHT11Test.py

This code will show measured data and also makes 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.6 / 5. Vote count: 15

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?