Facebook Twitter Instagram Pinterest YouTube
    Trending
    • Elecrow Meteor IPS Touchscreen with RGB LEDs
    • Pi Pico Pinout Display on the Command Line
    • How to Add a Raspberry Pi Pico Reset Button
    • Pi Pico Onboard LED
    • Pi Pico W Pinout and Power Pins
    • CrowPi L Raspberry Pi Laptop and Learning Platform
    • Pi Pico W Launched
    • Add Kodi to RetroPie Menu
    Facebook Twitter Instagram Pinterest YouTube RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        December 23, 2022

        How to Add a Raspberry Pi Pico Reset Button

        November 20, 2022

        Pi Pico Onboard LED

        May 6, 2022

        Add Kodi to RetroPie Menu

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Hardware»Sensors»DHT11 Temperature and Humidity Sensor and the Raspberry Pi
      DHT11 Temperature & Humidity Sensor

      DHT11 Temperature and Humidity Sensor and the Raspberry Pi

      8
      By Matt on September 21, 2017 Sensors, Tutorials & Help

      The DHT11 is a low-cost temperature and humidity sensor. It isn’t the fastest sensor around but its cheap price makes it useful for experimenting or projects where you don’t require new readings multiple times a second. The device only requires three connections to the Pi. +3.3v, ground and one GPIO pin.

      DHT11 Specifications

      The device itself has four pins but one of these is not used. You can buy the 4-pin device on its own or as part of a 3-pin module.

      DHT11 Sensor Dimensions

      The modules have three pins and are easy to connect directly to the Pi’s GPIO header.

      • Humidity : 20-80% (5% accuracy)
      • Temperature : 0-50°C (±2°C accuracy)

      The manufacturers do not recommended that you read data from this device more than once per 2 seconds. If you do you may get incorrect readings.

      Hardware Setup

      DHT11 Module

      The 4-pin device will require a resistor (4.7K-10K) to be placed between Pin 1 (3.3V) and Pin 2 (Data).

      The 3-pin modules will usually have this resistor included which makes the wiring a bit easier. For this reason I got hold of the module which I could then attach to the Pi with a piece of 3-way Dupont cable.

      Different suppliers may wire the module pins differently so check the PCB markings to identify Vcc (+), data and Ground (-).

      The 3 pins should be connected to the Pi as shown in the table below :

      DHT PinSignalPi Pin
      13.3V1
      2Data/Out11 (GPIO17)
      3not used–
      4Ground6 or 9

      Your data pin can be attached to any GPIO pin you prefer. In my example I am using physical pin 11 which is GPIO 17. Here is a 4-pin sensor connected to the Pi’s GPIO header. It has a 10K resistor between pin 1 (3.3V) and 2 (Data/Out).

      DHT11 connected to Raspberry Pi

      Python Library

      The DHT11 requires a specific protocol to be applied to the data pin. In order to save time trying to implement this yourself it’s far easier to use the Adafruit DHT library.

      The library deals with the data that needs to be exchanged with the sensor but it is sensitive to timing issues. The Pi’s operating system may get in the way while performing other tasks so to compensate for this the library requests a number of readings from the device until it gets one that is valid.

      Software Setup

      To start with update your package lists and install a few Python libraries :

      sudo apt-get update
      sudo apt-get install build-essential python-dev
      

      Then clone the Adafruit library from their repository :

      git clone https://github.com/adafruit/Adafruit_Python_DHT.git
      cd Adafruit_Python_DHT

      Then install the library for Python 2 and Python 3 :

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

      Hopefully at this point the library is installed and ready to be used within a Python script.

      Adafruit Example Python Script

      Adafruit provide an example script that you can use to check your sensor is operating correctly.

      cd ~
      cd Adafruit_Python_DHT
      cd examples

      Then :

      python AdafruitDHT.py 11 17

      The example script takes two parameters. The first is the sensor type so is set to “11” to represent the DHT11. The second is the GPIO number so for my example I am using “17” for GPIO17. You can change this if you are using a different GPIO pin for your data/out wire.

      You should see an output similar to this :

      Temp=22.0* Humidity=68.0%

      Using the Library In Other Python Scripts

      Once the Adafruit library is installed and you’ve got the example script to work you can use the sensor in your own scripts. Simply import the module, setup a few variables and call the “read_retry” function :

      import Adafruit_DHT
      
      # Set sensor type : Options are DHT11,DHT22 or AM2302
      sensor=Adafruit_DHT.DHT11
      
      # Set GPIO sensor is connected to
      gpio=17
      
      # Use read_retry method. This will retry up to 15 times to
      # get a sensor reading (waiting 2 seconds between each retry).
      humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
      
      # Reading the DHT11 is very sensitive to timings and occasionally
      # the Pi might fail to get a valid reading. So check if readings are valid.
      if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
      else:
        print('Failed to get reading. Try again!')
      

      This script can be downloaded directly to your Pi using :

      wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/dht11.py

      and run using :

      python dht11.py

      Buying a DHT11

      The DHT11 is available from many electrical component sellers. Here are a selection :

      • Adafruit
      • The Pi Hut
      • eBay
      • Amazon
      • CPC

      Some may only sell the device itself so you may need to shop around if you would prefer the 3-pin module.

      Final Thoughts

      The DHT11 is probably best suited for projects where rapid data readings are not required and the environment is not expected to see sudden changes in temperature or humidity. A weather station would be an idea project but a central heating controller may require something different.

      For projects that need a faster temperature reading rate (<2 sec) I would probably use a DS18B20 as they respond much faster to changes in temperature. But with both these sensors costing <$5 you can probably afford to buy both and experiment.

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleRetroPie IKEA Arcade Table Based On a Raspberry Pi
      Next Article Playing ScummVM Games on the Raspberry Pi

      Related Posts

      How to Add a Raspberry Pi Pico Reset Button

      Pi Pico Onboard LED

      Add Kodi to RetroPie Menu

      8 Comments

      1. John Jacobson on February 12, 2018 8:10 am

        Thanks worked perfectly as a Python3 script on Raspberry Pi Zero W running Jessie version 8. Thanks very much. I had used the PigPio library before, but prefer sticking to ordinary GPIO on the CPU starved RPi Zero W.

        Reply
      2. ZeXus on March 6, 2018 6:47 pm

        Nice!!

        Reply
      3. Kevin Ackland on September 18, 2018 6:55 pm

        Some great tutorials here. I got the DHT11 to work OK. Earlier I got an OLED 1306 screen to work (they cost less than £2) I’d love to output the temperature and humidity data to the OLED 1306. What about a tutorial on that – or point me in the right direction. Thanks

        Reply
      4. mohamed tamer on October 23, 2020 11:46 am

        nice

        Reply
      5. Michael Behringer on November 14, 2020 2:27 pm

        A while ago it worked fine. Now I get humidity between 142% and 175%… sigh… Probably means sensor is defect – or am I missing something?

        BTW, for folks wondering whether to get the DHT11 or DHT22: The DHT11 is really imprecise (+-3 degree!!!), 22 is a bit better, but not much. For temperature, I did a lot of testing with 5 sensors DS18B20 in parallel, in the same breadboard, under different settings (room temp, fridge, freezer) and they are max 0,5 degree apart – much better accuracy!!

        Reply
      6. Michael Behringer on November 14, 2020 3:15 pm

        OK, I found the problem: I used a 7m cable to connect to the sensor, and tried again with short cables, and now it works! So the problem was the cable, not the sensor!

        Reply
      7. Josef Lebondwaski on January 7, 2021 3:00 pm

        Is it important to use a resistor?
        I have seen examples like this where one is included, but I have also seen examples where there is no resistor between the sensor and the board.

        Reply
        • Matt on January 22, 2021 10:18 am

          The pullup resistor may not matter if the sensor is connected to an input that already has some sort of internal pullup. I think this is why you’ll see some diagrams using the resistor and some not. With the Pi you could enable a pullup on the GPIO and the DHT11 would probably work without the external resistor.

          Reply

      Leave A Reply Cancel Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Recent Posts
      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      December 23, 2022

      How to Add a Raspberry Pi Pico Reset Button

      November 20, 2022

      Pi Pico Onboard LED

      November 14, 2022

      Pi Pico W Pinout and Power Pins

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter Kodi LCD LED Linux media Minecraft Model A motionEyeOS PCB photography photos Pi-Lite Pi Pico power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI SSH temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives
      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Recent Posts
      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      December 23, 2022

      How to Add a Raspberry Pi Pico Reset Button

      Facebook Twitter Instagram Pinterest YouTube RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2022 - All Rights Reserved - Matt Hawkins

      mastodon.social@RPiSpy

      Type above and press Enter to search. Press Esc to cancel.