Skip to content

Reading Temperature and Humidity with DHT

Brendan Smith edited this page Sep 9, 2016 · 10 revisions

We have used the pins for the Pi! But, thus far we can only read simple inputs from buttons.

##Overview Next we add our first environmental sensor to the project.

The DHT22 is a digital temperature and humidity sensor that will communicate with our Raspberry Pi. We will print its readout and will write a Python script that reads the DHT every 5 seconds.

##The DHT22 The DHT22 is a simple, inexpensive Temperature and Humidity sensor. For more details, please see our DHT wiki page.

The DHT will emit a digital signal to be read by a Raspberry Pi pin.

##Signals from Sensors A little background on signals. According to Google, a Signal is:

A gesture, action, or sound that is used to convey information or instructions, typically by prearrangement between the parties concerned.

To us, a signal passes information. Our DHT22 generates a signal, the Raspberry pi receives that signal, and then interprets the information about the environment's current temperature and relative humidity.

The DHT makes a digital signal, which is 1's and 0's encoded as high and low voltages. So, when the DHT is turned on, the data pin will generate something like:

Digital signal image

If we connect the data pin to the Raspberry Pi, and this signal can be read using a python library.

Please complete Adafruit's DHT and Raspberry Pi tutorial to connect the DHT and use the Adafruit_Python_DHT Library.

By the end, run their simpletest.py and see what the Pi can readout!

##Creating our own DHT script.

Now, lets write our own script that runs continually; it will print out the temperature and humidity every X seconds.

Please feel free to do this on your own based off of simpletest.py(here). Though, I will walk through the basics again and then set up the delayed intervals.

###Simple Reading

First, we import our Adafruit Library and have our script do a simple reading.

At the top: import Adafruit_DHT

Then variables for the sensor:

dht_sensor = Adafruit_DHT.DHT22  
DHT_PIN = 4

Then a simple reading from the sensor using:

humidity, temperature = Adafruit_DHT.read_retry(dht_sensor, DHT_PIN)

And print our results

print "Humidity: " + humidity + ", Temperature: " + temperature

Run the script to make sure that it works!

sudo python dht_script.py

Mmm, fix that last line with some formatting

print "Humidity: {:0.1f}%, Temperature: {:0.2f}*C".format(humidity, temperature)

Run it again! Pleased?

###Adding a Timing Element

We have essentially just recreated simpletest.py(here, and we want to repeat this reading over time automatically.

For this please be familiar with Python while loops.

We will read from the sensor, print our results, and delay for X seconds. Then repeat, repeat, repeat.

The mechanism to delay is the Python sleep function. It delays the program for a given number of seconds.

At the top, with another import,

from time import sleep

Now, around the read and print functions, surround it with a while loop

while True:
    humidity, temperature  = ...
    print "humidity: ....

The program has no delay yet. At the top, add the variable

DELAY_INTERVAL = 5

And add the delay line to the end of the while loop

    sleep(DELAY_INTERVAL)

Run the program and make sure that it prints out as expected. This program will run forever if you let it! Use CTRL-C to interrupt the program.

See the reference dht_script.py if you are having trouble.

Clone this wiki locally