DHT22 Raspberry Pi: How to Connect and Monitor Temperature and Humidity
Table of Contents
- Introduction
- Materials Needed
- Wiring and Connection
- Setting Up the Software
- Monitoring Temperature and Humidity
Introduction
The DHT22 is a low-cost temperature and humidity sensor that can be easily connected to a Raspberry Pi. In this article, we will guide you on how to connect and monitor temperature and humidity using the DHT22 sensor and a Raspberry Pi. Whether you are monitoring the temperature and humidity of your home, garden, or workspace, this tutorial will provide you with all the information you need.
Materials Needed
- Raspberry Pi (any model will do)
- DHT22 sensor
- Breadboard
- Jumper wires
Wiring and Connection
Connect the DHT22 sensor to the Raspberry Pi as follows:
- Connect the VCC pin of the DHT22 to the 3.3V pin of the Raspberry Pi
- Connect the GND pin of the DHT22 to the ground pin of the Raspberry Pi
- Connect the data pin of the DHT22 to GPIO pin 4 of the Raspberry Pi
Setting Up the Software
Before we can start monitoring temperature and humidity, we need to set up the software on the Raspberry Pi:
Step 1: Update the Raspberry Pi
Open the terminal on the Raspberry Pi and run the following commands:
sudo apt-get update
sudo apt-get upgrade
Step 2: Install Python Libraries
Install the required Python libraries:
sudo pip install Adafruit_DHT
Monitoring Temperature and Humidity
With the wiring and software properly set up, we are now ready to monitor temperature and humidity using the DHT22 sensor:
Step 1: Create a Python Script
Create a new file called dht22.py
and paste the following code:
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temperature={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
Step 2: Run the Script
Open the terminal on the Raspberry Pi and run the following command:
sudo python dht22.py
The output will display the temperature and humidity readings from the DHT22 sensor.
Conclusion
By following this tutorial, you now know how to connect and monitor temperature and humidity using the DHT22 sensor and a Raspberry Pi. Whether you are monitoring the temperature and humidity of your home, garden, or workspace, this tutorial provides all the information you need. Have fun experimenting!