The Ultimate Guide to Interfacing a DHT11 Temperature and Humidity Sensor with Raspberry Pi

This guide provides step-by-step instructions on how to connect a DHT11 sensor to your Raspberry Pi, read temperature and humidity data, and display it using Python. Follow along for an easy setup and …


Updated August 11, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This guide provides step-by-step instructions on how to connect a DHT11 sensor to your Raspberry Pi, read temperature and humidity data, and display it using Python. Follow along for an easy setup and smooth sailing with your IoT project!

What is a DHT11 Sensor?

DHT11 is a low-cost digital temperature and humidity sensor that uses a single data pin to communicate with the Raspberry Pi. It measures relative humidity (%RH) in the range of 20-95% and temperature in the range of 0-50°C. The sensor outputs are digital signals, so it doesn’t require any complex analog-to-digital converters.

Materials Required:

  1. Raspberry Pi (any model)
  2. DHT11 Temperature and Humidity Sensor
  3. Breadboard
  4. Jumper wires (male to male, female to female, and male to female/female)
  5. Power supply (5V)

Setting Up the Hardware:

  1. Connect VCC (red wire) of DHT11 sensor to the 5V pin on Raspberry Pi.
  2. Connect GND (black wire) of DHT11 sensor to any ground pin on Raspberry Pi.
  3. Connect the data pin (white wire) of DHT11 sensor to GPIO4 on Raspberry Pi using a jumper wire.
  4. Insert the SD card into your Raspberry Pi and connect it to a monitor, keyboard, mouse, and power supply.
  5. Boot up your Raspberry Pi and open the terminal.

Installing Required Packages:

Before we can start programming the sensor, we need to install some packages. Run the following commands in the terminal to install the required libraries:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git python3-dev python3-pip
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python3 setup.py install

Writing Python Code:

Create a new file called dht11.py in your home directory using the nano text editor:

nano ~/dht11.py

Add the following code to the file and save it (Ctrl+X, Y, Enter):

import time
import Adafruit_DHT

# Set sensor type (DHT11) and data pin
sensor = Adafruit_DHT.DHT11
pin = 4

while True:
    # Try to get a reading from the sensor
    humidity, temperature = Adafruit_DHT.read(sensor, pin)
    
    if humidity is not None and temperature is not None:
        print("Temperature: {:.1f}°C Humidity: {:.1f}%".format(temperature, humidity))
    else:
        print("Failed to get reading from sensor.")
    
    # Wait for 2 seconds before taking another reading
    time.sleep(2)

Running the Code:

To run the code and display temperature and humidity readings, use the following command in the terminal:

python3 ~/dht11.py

You should see output similar to this:

Temperature: 25.0°C Humidity: 40.0%
Temperature: 25.0°C Humidity: 40.0%
Temperature: 25.0°C Humidity: 40.0%

Conclusion:

Now you know how to connect a DHT11 sensor to your Raspberry Pi and read temperature and humidity data using Python! You can use this data in your IoT projects or for any other applications that require accurate environmental measurements.