Raspberry Pi - DHT11

This tutorial instructs you how to use Raspberry Pi to read the temperature and humidity from DHT11 sensor. In detail, we will learn:

We suggest:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Temperature and Humidity Sensor DHT11
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of DHT11 Temperature and Humidity Sensor

The Temperature and Humidity Sensor Pinout

The DHT11 on the market comes with two forms: sensor and module.

The DHT11 sensors in their original form have four pins:

  • GND: This pin must be connected to ground (0V)
  • VCC: This pin must be connected to the voltage supply (5V)
  • DATA: This pin is used for communication between the sensor and Raspberry Pi
  • NC: This pin should not be connected and can be ignored
DHT11 temperature and humidity sensor pinout

The DHT11 module has three pins:

  • GND pin (-): must be connected to GND (0V)
  • VCC pin (+): must be connected to VCC (5V)
  • OUT pin: used for communication between the sensor and Raspberry Pi

※ NOTE THAT:

The arrangement of pins on a module can differ between manufacturers. It is essential to always use the labels printed on the module when working with it. Be sure to take a close look!

Wiring Diagram

Wiring to Raspberry Pi is the same for both sensors. In its original form, a resistor with a value between 5K and 10K Ohms is necessary to keep the data line high, thus enabling communication between the sensor and the Raspberry Pi.

Raspberry Pi - DHT11 Sensor Wiring

The wiring diagram between Raspberry Pi and DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Connect the DHT11 sensor to the Raspberry Pi as follows:

  • DHT11 VCC pin to Raspberry Pi 5V (pin 2 or any 5V GPIO pin)
  • DHT11 GND pin to Raspberry Pi GND (pin 30 or any GND GPIO pin)
  • DHT11 DATA pin to a GPIO pin on the Raspberry Pi (e.g., GPIO 12 (pin 32).

For better stability, you need to add a resistor (a value between 5K and 10K Ohms) between the VCC and DATA pins of the DHT11.

Raspberry Pi - DHT11 Module Wiring

Most DHT11 sensor modules come with an integrated resistor, thus eliminating the need for additional wiring or soldering.

The wiring diagram between Raspberry Pi and DHT11 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

Connect the DHT11 module to the Raspberry Pi as follows:

  • DHT11 VCC pin to Raspberry Pi 5V (pin 2 or any 5V GPIO pin)
  • DHT11 GND pin to Raspberry Pi GND (pin 30 or any GND GPIO pin)
  • DHT11 DATA pin to a GPIO pin on the Raspberry Pi (e.g., GPIO 12 (pin 32).

How To Program for Raspberry Pi to read Temperature and Humidity from DHT11 Sensor

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the library for DHT11 temperature and humidity sensor by running the following command:
sudo pip3 install Adafruit_DHT
  • Create a Python script file DHT11.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-dht11 import Adafruit_DHT import time # Set the sensor type and GPIO pin sensor = Adafruit_DHT.DHT11 pin = 17 # Change this to the GPIO pin you used for DATA try: while True: # Try to read the temperature and humidity from the sensor humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) # If the reading was successful, display the values on the same line if humidity is not None and temperature is not None: print(f"Temperature: {temperature:.1f} °C, Humidity: {humidity:.1f} %") else: print("Failed to retrieve data from the DHT11 sensor.") # Wait some time before taking the next reading time.sleep(2) except KeyboardInterrupt: print("\nExiting...")
  • Save the file and run the Python script by executing the following command in the Terminal:
python3 DHT11.py
  • Check the results on the terminal.
PuTTY - Raspberry Pi
Temperature: 25.4 °C, Humidity: 52.7 % Temperature: 25.7 °C, Humidity: 52.1 % Temperature: 26.1 °C, Humidity: 52.3 % Temperature: 25.9 °C, Humidity: 52.3 %

The script continuously reads the sensor data every 2 seconds (as specified in the time.sleep(2) line), so you will see updated readings like this every 2 seconds until you stop the script by pressing Ctrl + C keys in the terminal.

If, for any reason, the script fails to read data from the sensor, it will display:

PuTTY - Raspberry Pi
Failed to retrieve data from the DHT11 sensor.

Additional Knowledge

Remember that the DHT11 sensor is not the most accurate or reliable sensor available. If you need more precision, you might consider using the DHT22 or other more advanced sensors.

Let's compare DHT11 and DHT22 sensors.

The commons between DHT11 and DHT22

  • Providing the temperature and humidity information.
  • Pinouts remain the same.
  • The wiring to Raspberry Pi is unchanged.

The differences between DHT11 and DHT22

DHT11 DHT22
Price ultra low cost low cost
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ± 2°C ± 0.5°C
Humidity Range 20% to 80% 0% to 100%
Humidity Accuracy 5% ± 2 to 5%
Reading Rate 1Hz (once every second) 0.5Hz (once every 2 seconds)
Body size 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

Evidently, the DHT22 is more precise than the DHT11 and has a broader range, though it is pricier.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!