Home‎ > ‎Home Alarm System project‎ > ‎

Temperature and Humidity using the DHT22 sensor

This tutorial you will build one or more digital temperature gauges linked to your  Raspberry Pi that can be viewed on the WWW or your smart phone. 

At the end of the project you will have a WWW dashboard that looks similar to this:

 Multiple Sensors

 Display a temperature history graph per sensor

What you need:
  • Any Raspberry Pi
  • the following parts are available through the PrivateEyePi Store
    • DHT22 sensor(s) (TS02) , one per temperature reading you want on the dashboard
    • 10k resistor (one per sensor) 
    • Jumper wire cables and breadboard or wire, soldering iron and solder (JW01, BB01)
For support, queries or suggestions please visit our support forum.
Step 1: Build and test the Electronics:

Parts can be purchased from our store: http://www.privateeyepi.com/store
  • Purchase a DHT22 digital thermometer and humidity sensor
  • Purchase a 4.7k (ohm) resistor
  • As per Figure 1 put the resistor between pins 1 and 2 as in the diagram
  • Connect Pin 1 of the sensor to 5V GPIO (Pin 2)
  • Connect Pin 2 of the sensor to and free GPIO pin e.g. GPIO4 (Pin 7)
  • Ignore pin 3
  • Connect Pin 4 of the temperature gauge to ground (Pin 6)
Figure 1. DHT22 temperature and humidity digital sensor circuit diagram

Figure 2. Picture of circuit from Figure 1 on a bread board

Multiple sensors
If you want to add multiple sensors unfortunately you cannot daisy chain them to the same GPIO (as you can with the DS18B20). You are restricted by the amount of free GPIO pins available on the Raspberry Pi. However you can connect multiple sensors each with their own GPIO pin connection to the Raspberry Pi. Another option for unlimited multiple temperature sensors is to go wireless.

Configure the dashboard 

Step 1: Register an account with PrivateEyePi
If you have not yet registered with PrivateEyePi follow the following steps, otherwise proceed to Step 3. 
  • Go to www.privateeyepi.com
  • Click on "New User" option top right of the screen.
  • Enter your the details on the screen and click update.
Step 2: Configure GPIO
This configuration tells the system that you have connected a temperature sensor to a GPIO pin and to display it on your dashboard.
  • As per Figure 3 click on the GPIO menu option 
  • Click "Add"
  • Type 7 in the "number" field (or any other GPIO pin -  use pin number not GPIO number)
  • Give it a Description of "GPIO 4", or anything else you like

Figure 3. PrivateEyePi setting for a new sensor


  • Click "Update"
  • As per Figure 4 click the "Location" menu item 
  • Enter a Description, e.g. "Temperature Gauge"
  • Select GPIO Port 4
  • Select Type "Temperature"
  • Click "Update"

Figure 4. PrivateEyePi location settings

If you have multiple sensors then repeat this step and add other GPIO and Locations settings for each sensor.

Step 3: Install PrivateEyePi software on your Raspberry Pi

Follow the following link to install the PrivateEyePi software:

At the command prompt: pi@raspberrypi / $


  cd /home/pi/pep
  sudo apt-get update
  sudo apt-get install git
  sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git 
  cd Adafruit_Python_DHT
  sudo apt-get install build-essential python-dev  
  sudo python setup.py install 

Step 4: Register token
  • Follow the following link to enter your token in the globals.py file
  • We have assumed you installed the PrivateEyePi software in the /home/pi/pep directory otherwise you should replace /home/pi/pep in the following steps with your directory.
  • Edit the globals.py file again:
  cd /home
  sudo nano globals.py

Set the PrintToScreen setting to "True". This will print all output to the screen while you are testing your setup. You can switch this off later once everything is working.

Set the Farenheit variable to True if you want your temperature display in Fahrenheit on your dashboard. As per bottom of Figure 5 update the dht22_gpio and dht22_pin_no settings with the GPIO number and it's associated Raspberry Pi pin number (they are not the same!) to which your sensor is connected. In this tutorial it is GPIO 4 (or pin 7 on the Raspberry Pi).

Figure 5 - Set Fahrenheit option and DHT22 GPIO & Pin Number

Press CTRL-X
Then press Y to save the file
Step 4: Run dht22.py program
At the command prompt type:

  cd /home/pi/pep
  sudo python dht22.py
Step 5: View your dashboard
  • Go to www.privateeyepi.com
  • Log in, or click on the "Dashboard" link on top left
  • You should now see the temperature and humidity (red circle in Figure 10) displayed

Figure 10 - Temperature and humidity displayed on the PrivateEyePi dashboard