0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use DHT11 and DHT22 Sensors with Raspberry Pi

To use DHT11 or DHT22 sensors with a Raspberry Pi, connect the sensor's power, ground, and data pins to the Pi's GPIO pins, then use a Python library like Adafruit_DHT to read temperature and humidity data. The library handles communication, and you can run a simple Python script to get sensor readings.
📐

Syntax

Use the Adafruit_DHT Python library to read data from DHT11 or DHT22 sensors. The main function is read_retry(sensor, pin), where:

  • sensor: specifies the sensor type (Adafruit_DHT.DHT11 or Adafruit_DHT.DHT22).
  • pin: the GPIO pin number connected to the sensor's data pin.

This function returns a tuple with humidity and temperature values.

python
import Adafruit_DHT

# Define sensor type and GPIO pin
sensor = Adafruit_DHT.DHT22  # or Adafruit_DHT.DHT11
pin = 4  # GPIO pin number

# Read humidity and temperature
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:
    print(f"Temp={temperature:.1f}°C  Humidity={humidity:.1f}%")
else:
    print("Failed to get reading. Try again!")
💻

Example

This example shows how to read temperature and humidity from a DHT22 sensor connected to GPIO pin 4 on the Raspberry Pi. It prints the values or an error message if reading fails.

python
import Adafruit_DHT
import time

sensor = Adafruit_DHT.DHT22
pin = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        print(f"Temp={temperature:.1f}°C  Humidity={humidity:.1f}%")
    else:
        print("Failed to get reading. Try again!")
    time.sleep(2)
Output
Temp=23.4°C Humidity=45.7% Temp=23.5°C Humidity=45.9% Temp=23.4°C Humidity=45.8% ...
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure the sensor's power (3.3V or 5V), ground, and data pins are connected correctly to the Raspberry Pi.
  • Wrong GPIO pin number: Use the correct BCM GPIO number in the code, not the physical pin number.
  • Missing pull-up resistor: Some sensors require a 4.7kΩ pull-up resistor on the data line for reliable readings.
  • Using the wrong sensor type: Specify DHT11 or DHT22 correctly in the code.
  • Running without root permissions: Sometimes running the script with sudo helps access GPIO pins.
python
import Adafruit_DHT

# Wrong sensor type example (will fail)
sensor = Adafruit_DHT.DHT11  # but connected DHT22 sensor
pin = 4

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is None or temperature is None:
    print("Reading failed due to wrong sensor type or wiring.")

# Correct sensor type
sensor = Adafruit_DHT.DHT22
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
    print(f"Temp={temperature:.1f}°C  Humidity={humidity:.1f}%")
Output
Reading failed due to wrong sensor type or wiring. Temp=23.4°C Humidity=45.7%
📊

Quick Reference

StepDescription
Connect sensor power3.3V or 5V pin on Raspberry Pi to sensor VCC
Connect sensor groundGND pin on Raspberry Pi to sensor GND
Connect data pinGPIO pin (e.g., GPIO4) to sensor data pin with optional 4.7kΩ pull-up resistor
Install libraryRun 'pip install Adafruit_DHT' to install Python library
Write codeUse 'read_retry(sensor, pin)' to get temperature and humidity
Run scriptExecute Python script, optionally with sudo for GPIO access

Key Takeaways

Connect DHT11 or DHT22 sensor power, ground, and data pins correctly to Raspberry Pi GPIO.
Use the Adafruit_DHT Python library and specify the correct sensor type and GPIO pin.
Include a pull-up resistor on the data line if readings are unstable.
Run your Python script with appropriate permissions to access GPIO pins.
Check wiring and sensor type carefully to avoid common reading errors.