Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
The Adafruit_DHT library is a popular Python library used to interface with DHT sensors, such as the DHT11 and DHT22, which are commonly used for measuring temperature and humidity. These sensors are inexpensive and easy to use, making them ideal for Raspberry Pi projects. In this article, we'll guide you through the process of setting up and using the Adafruit_DHT library on a Raspberry Pi.
Before you begin, ensure you have the following:
Connect your DHT sensor to the Raspberry Pi as follows:
Open a terminal on your Raspberry Pi.
Update your package list and install necessary packages:
sudo apt-get update
sudo apt-get install python3-pip python3-dev
Install the Adafruit_DHT library using pip:
sudo pip3 install Adafruit_DHT
Create a Python script to read data from the DHT sensor:
import Adafruit_DHT
# Set sensor type : Options are DHT11, DHT22, or AM2302
sensor = Adafruit_DHT.DHT22
# Set GPIO pin where the sensor is connected
pin = 4
# Use read_retry method. This will retry up to 15 times to get a sensor reading
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Check if reading was successful
if humidity is not None and temperature is not None:
print(f'Temperature: {temperature:.1f}°C')
print(f'Humidity: {humidity:.1f}%')
else:
print('Failed to get reading. Try again!')
read_dht.py
.Run the script using Python 3:
python3 read_dht.py
If everything is set up correctly, you should see the temperature and humidity readings printed in the terminal.
Using the Adafruit_DHT library with a Raspberry Pi is a straightforward way to measure temperature and humidity with DHT sensors. This setup is perfect for home automation, weather stations, or any project requiring environmental data.