Skip to content

Reading Temperature and Humidity with DHT

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

(TODO better topic and concluding sentences)

We have used the pins for the Pi! But, thus far we can only read things like input from buttons. Next we will add our first environmental sensor to the project. ##The DHT22 The DHT22 is a simple, inexpensive Temperature and Humidity sensor. For more details, please see our DHT wiki page.

##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 Sensor generates a signal, the Raspberry pi receives that signal and then interprets the information about our environment's current temperature and relative humidity.

Our 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

So, we connect the data pin to the Raspberry Pi, and read the signal using a python library. (TODO maybe more evident of main tutorial) Here is Adafruit's DHT and Raspberry Pi tutorial to get the wiring set up and use the Adafruit_Python_DHT Library.

At the end you should run their simpletest.py and see what the Pi can readout!

##Creating our own DHT script. (TODO More Background on script) Now, lets write our own script that runs continually; it will print out the temperature and humidity every X seconds. (TODO show that it can be done without my help) Please feel free to do this on your own based off of simpletest.py. 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

(TODO background on importance, what are we accomplishing)

For this please be familiar with Python while loops. TODO:is this satisfactory to just assume? maybe add a misc wiki page for intro to 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 our other 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 script if you are having trouble.

Clone this wiki locally