How to interface DHT11 DHT22 Temperature sensor with Raspberry Pi

About:
In this tutorial i will explain how to interface and program DHT11/22 temperature sensor with Raspberry Pi. I have used adafruit python library to program these sensors in python shell.




Things you will need:
Raspberry Pi (any model will work)
DHT11 or DHT22
4k7 Resistor




Circuit Diagram:

Circuit diagram

Python Code:

#!/usr/bin/python
# Name: DHT11/DHT22Interfacing Temperature sensor with raspberry Pi
# Author: Pranay Sawarkar
# Website: www.etechpath.com
# Copyright: Creative Common

import Adafruit_DHT

# sensor = Adafruit_DHT.DHT22
sensor = Adafruit_DHT.DHT11


# Sensor connected to Raspberry Pi GPIO4.
pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor, 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 capture reading. Please Try again!')

 




Steps:

  1. Connect your temperature sensor to GPIO4 of your raspberry pi  and don’t forget to connect 4k7 pull up resistor between vcc and data output pin. You can use any 4k7 to 10k range resistor for pull up purpose.
  2. Power on your raspberry pi and open terminal.
  3. First we will download and install adafruit library for DHT sensors,
    sudo apt-get update
    git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  4. Install python essential files. (if you are using it for first time)
    sudo apt-get install build-essential python-dev
    sudo python setup.py install
  5. Now your raspberry pi is ready to run the adafruit dht python program,
  6. Copy main python code at home directory in a empty file and name it as dht.py (or download ready dht.py file from download section and copy it to your raspberry pi home directory).
  7. Now you can use bellow command to check the sensor data by running this python program from your raspbery pi terminal,
    sudo python dht.py

    You will get output like this,



 

Leave a Reply

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