Posted on Leave a comment

DHT11 temperature and humidity sensor on Raspberry Pi

In this tutorial we use a DHT11 temperature & humidity sensor. With a Python script running on your Raspberry Pi, we will read the ambient temperature and humidity. When you have completed this tutorial, you will be able to connect the DHT11 sensor to your Pi via the GPIO pins. You’ll also have the basic code to convert the output signal of the sensor to useable information in your Python script. This tutorial can also be used for a DHT22 sensor.

What you’ll need for this tutorial

First, you need to have a Raspberry Pi 3 running on the latest version of Raspbian. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. Your Pi will need to have access on the internet to download a Python library. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “First Python program on the Raspberry Pi” and/or “Raspberry Pi first program using GPIO” to have a quick introduction.

Next you’ll need some extra hardware:

  • a breadboard (we are using a 400 points breadboard)
  • an DHT11 or DHT22 temperature and humidity sensor
  • Dupont jumper wires
  • a T-cobbler (optional)
  • a 40 pin GPIO cable (optional)

Raspberry Pi GPIO discovery kit

If you miss any equipment, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.

 

About the DHT11 temperature and humidity sensor

DHT11 sensor

The DHT11 is a low-cost and popular sensor for measuring temperature and humidity. The device on the module requires 3 connections to the Raspberry Pi : 3,3V ; GND and a GPIO input pin. As the output signal is 3,3V , it can be directly connected to a GPIO input pin of the Raspberry Pi. The temperature range is 0-50°C (+/-2°C) and the humidity range is 20-90% (+/-5%). The sensor is quite slow and has limited accuracy, but it is an ideal device for your experiments.

Setting up the hardware part

Before starting connecting wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable of the board!

DHT11 connected to Raspberry Pi
  • connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
  • plug the cobbler onto the breadboard as shown in the figure above or below
  • plug the other end of the 40 pin cable in the T-cobbler
  • connect the VCC pin of the sensor to the 3,3V pin (red wire)
  • connect the OUTPUT pin of the sensor to pin 23 (yellow wire)
  • connect the GND of the sensor to a GPIO GND pin (black cable)
DHT11 connected to Raspberry Pi

Installing the Python library

The DHT11 sensor communicates with a specific protocol. Fortunately, we don’t have to deal with these details and thanks to Adafruit, we can use a Python library. To download the library make sure your Pi has access to the internet. Open a terminal window and write following commands:

sudo apt update
sudo apt install build-essential python-dev
git clone http://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

Now we can install the library. For this tutorial we only need to install it for Python 3, but as we’re working on it now, it’s easy to install it for both Python versions:

sudo python setup.py install
sudo python3 setup.py install

Writing the code

The aim is to write a very simple Python program that allows us to visualize the measured ambient temperature and humidity. To write the code, we use the Thonny IDE. You can find Thonny under the application menu of your Raspberry Pi.

Write or paste following code in the IDE:

import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
DHT11_pin = 23

humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT11_pin)
if humidity is not None and temperature is not None:
  print('Temperature={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
  print('Failed to get reading from the sensor. Try again!')

Some explanations about the code:

  • import Adafruit_DHT First we import the Adafruit_DHT library
  • sensor = Adafruit_DHT.DHT11 We define the type of sensor. Change to DHT22 if you use this type of DHT-sensor.
  • DHT11_pin = 23: We assign the input-pin to GPIO 23
  • humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT11_pin) Reading the sensor
  • if humidity is not None and temperature is not None: if we get a reading from the sensor
  • Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the code line after the if command.
  • print('Temperature={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)) printing the results on the screen. The results are formatted with 1 decimal.
DHT11 Python script

When you are done with the code, click on the Run button. You will see a message with the current temperature and humidity. Click again on the Run button if you want a new measurement.

Congratulations! With this setup you can read temperature and humidity now. You can further use the script to process the result in an application where you want to monitor the temperature and humidity for example. Have fun with it!

Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

By subscribing you agree with our privacy policy.