-
Notifications
You must be signed in to change notification settings - Fork 2
Reading Temperature and Humidity with DHT
We have used the pins for the Pi! But, thus far we have just read things like resistor values 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 reading signals. According to Google's definition, a Signal is:
A gesture, action, or sound that is used to convey information or instructions, typically by prearrangement between the parties concerned.
And its relevance to us is passing of information. Here, our Sensor generates a signal, and Raspberry pi receives that signal and interprets the desired information, 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 connected to power, the data pin on the DHT will generate something that looks like:
So, we will get the data pin to the Raspberry Pi, and read out the signal using a premade library.
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 have 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, and will print out the temperature and humidity every X seconds.
Feel free to do this on your own based off of simpletest.py. I will walk through the basics again and set up the timing interval.
First, we should import our Adafruit Library and have our script do a simple reading
At the top:
import Adafruit_DHT
Then create variables for sensor:
dht_sensor = Adafruit_DHT.DHT22
dht_pin = 4
Then we can do the 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, probably 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 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?
In short, we will read from the sensor, print our results, and delay for X seconds. Then repeat, repeat, repeat.
The mechanism we will use to delay is the Python sleep function. It will delay 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:
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. See the reference script if you are having trouble.
Wiki for Rhewlab Environmental Sensing Project
- Setting up Raspberry Pi
- Getting started with GPIO
- Reading Temperature and Humidity with DHT
- Adding a RTC to the Raspberry Pi
- Datalogging with Python
- Connecting to ADC with Raspi
- Finishing up
Digital
Analog
