Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use the Adafruit_DHT Library with Raspberry Pi to Read Temperature and Humidity

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.

Prerequisites

Before you begin, ensure you have the following:

  1. A Raspberry Pi with an operating system installed (Raspberry Pi OS recommended).
  2. A DHT11 or DHT22 sensor.
  3. Jumper wires and a breadboard for connections.
  4. Internet connectivity for installing libraries.

Wiring the Sensor

Connect your DHT sensor to the Raspberry Pi as follows:

  • Connect the VCC pin on the sensor to the 3.3V pin on the Raspberry Pi.
  • Connect the GND pin on the sensor to a GND pin on the Raspberry Pi.
  • Connect the data pin on the sensor to GPIO4 (pin 7) on the Raspberry Pi.

Installing the Adafruit_DHT Library

  1. Open a terminal on your Raspberry Pi.

  2. Update your package list and install necessary packages:

    sudo apt-get update
    sudo apt-get install python3-pip python3-dev
  3. Install the Adafruit_DHT library using pip:

    sudo pip3 install Adafruit_DHT

Writing the Python Script

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!')

Running the Script

  1. Save the script as read_dht.py.
  2. 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.

Troubleshooting

  • Ensure your wiring is correct and that the sensor is properly connected.
  • Make sure you have the correct sensor type specified in the script.
  • Verify that the GPIO pin number matches your wiring.

Conclusion

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.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.