In this tutorial, we're going to cover how
to use the DHT11 (or DHT22) temperature and
humidity sensor with the Raspberry Pi.
The DHT11 and DHT22 are fundamentally the
same from a wiring and software perspective.
However, the DHT22 is a more accurate sensor.
In this project we'll be using the DHT11.
If you have a DHT22, just follow along and
everything will work the same.
You'll just get more accurate readings!
Both sensors will work up to 65 feet (20 meters)
from the Raspberry Pi.
This is great for remote mounting.
I should also note that these sensors come
as separate components or as modules.
We're going to use a module.
The difference is that the module includes
the 10K Ohm pull up resistor on the board.
If you decide to use the standalone sensor,
we have a wiring diagram for that on TheGeekPub.com
you can follow.
You don't need a breadboard for this project,
but I feel like it makes things much simpler
to understand, especially if you use of these
breakout cables.
It makes the Pi's pinout much easier to follow.
There's a link to these in the description.
Let's wire the DHT11 up to the Raspberry Pi.
Connect the positive pin the 5V terminal on
the Pi.
Connect the sensor's ground wire to ground
on the Pi.
Connect the sensor's signal pin to physical
pin 7 which is GPIO pin 4.
If you wired everything up correctly, you'll
see a red LED glowing on the DHT11 module.
Next thing we need to do is install the DHT
python library.
This is done by entering "sudo pip3 install
Adafruit_DHT".
If you get an error see the link the description
for help resolving it.
OK.
Now let's take a look at the code we're going
to use.
This is some very basic code written in Python.
The first section of code imports the DHT
library from Adafruit and the system time
library.
This line defines the sensor object we will
use, and the next line is a variable that
defines the GPIO pin we are using.
"While True" creates an infinite loop for
the next bit of code.
First, we capture the temperature and humidity
to two aptly named variables.
Then we check to see if it worked.
If it worked, we print the temperature and
humidity to the screen.
If it it failed we let you know to check your
cabling.
Since the DHT11 and DHT22 can only be checked
a maximum of once per second, we use the sleep
function two pause three seconds between checks.
To use this code type the following command
"sudo nano mydht11.py" and paste this code
in from the link in the description into the
window.
Press Control-O to write the file, and then
Control-X to exit nano.
To run your code, enter python3 mydht11.py
and press enter.
If you did everything correctly, you'll start
seeing the temperature and humidity reported
on the terminal window every 3 seconds.
There are so many possible uses for these
sensors.
You can use them to capture the temperature
of your Pi when its in an enclosure in the
hot summer heat, or you can use it to check
on the temperature of room in your house when
you're out of town.
It's also very commonly used in creating weather
stations.
Well I hope you enjoyed this video.
Do me a favor and tap that like button.
If you find these videos helpful, maybe consider
supporting my channel.
See you in the next video!
