DEV Community

Hedy
Hedy

Posted on

How to use DHT22 on Raspberry Pi?

Here’s a reliable, start-to-finish way to wire + read a DHT22 (AM2302) on a Raspberry Pi.

1) Wiring (most important part)
A) Pick a GPIO pin (example: GPIO4 / BCM4)

Adafruit’s common example uses GPIO4.

Raspberry Pi pins (GPIO4):

  • BCM GPIO4 = physical pin 7
  • 3V3 = physical pin 1 (or 17)
  • GND = physical pin 6 (or many others)

B) Connect the sensor

If your sensor is the 3-pin module labeled + out -:

  • + → 3.3V
  • - → GND
  • out → GPIO4

If your sensor is the bare 4-pin DHT22 (typical order):

  • VCC → 3.3V
  • DATA → GPIO4
  • NC → (leave unconnected)
  • GND → GND (Always confirm your exact pin order from the sensor/breakout.)

C) Add the pull-up resistor (DHT22 needs it)

Connect a 4.7kΩ–10kΩ resistor from DATA to VCC (DATA ↔ 3.3V).

Adafruit even notes: if 4.7k doesn’t work, try 10k.

Some DHT22 breakout boards already include this pull-up resistor, so you may not need to add one externally.

D) Important safety note (Pi GPIO is 3.3V)

Do not feed a 5V signal into a Pi GPIO. People commonly damage Pis by mixing 5V and GPIO.

Safest setup: power the DHT22 from 3.3V.

2) Software Method 1 (easy): Adafruit CircuitPython library
Install packages

sudo apt update
sudo apt install -y python3-pip libgpiod2
pip3 install adafruit-circuitpython-dht
Enter fullscreen mode Exit fullscreen mode

(Adafruit’s guide installs adafruit-circuitpython-dht and libgpiod2.)

Optional but often helpful: make sure your user can access GPIO (Adafruit mentions being in the gpio group).

sudo usermod -aG gpio $USER
# log out/in (or reboot)
Enter fullscreen mode Exit fullscreen mode

Example Python code (GPIO4)

Create dht22_read.py:

import time
import board
import adafruit_dht

# GPIO4 = board.D4 (BCM numbering)
dht = adafruit_dht.DHT22(board.D4, use_pulseio=False)

while True:
    try:
        t_c = dht.temperature
        h = dht.humidity
        print(f"Temp: {t_c:.1f} C  Humidity: {h:.1f}%")
    except RuntimeError as e:
        # DHTs are flaky; retry after a pause
        print("Read error:", e)
    time.sleep(2.0)  # wait at least ~2 seconds between reads
Enter fullscreen mode Exit fullscreen mode

Run it:

python3 dht22_read.py
Enter fullscreen mode Exit fullscreen mode

Notes:

Adafruit recommends catching RuntimeError and waiting at least 2 seconds before retrying.

3) Software Method 2 (often more reliable): pigpio + pigpio-dht
Install and start pigpio daemon

sudo apt update
sudo apt install -y pigpio python3-pigpio
sudo systemctl start pigpiod
sudo systemctl enable pigpiod
Enter fullscreen mode Exit fullscreen mode

(systemd enable/start commands are commonly used for pigpiod.)

Install the DHT helper library

pip3 install pigpio-dht
Enter fullscreen mode Exit fullscreen mode

Example Python code

from pigpio_dht import DHT22

gpio = 4  # BCM GPIO4
sensor = DHT22(gpio)

result = sensor.read()
print(result)  # e.g. {'temp_c': 20, 'temp_f': 68.0, 'humidity': 35, 'valid': True}
Enter fullscreen mode Exit fullscreen mode

DHT22 read rate is slow (about once every ~2 seconds).

4) Quick troubleshooting checklist

  • Always: DATA pull-up resistor (4.7k–10k) unless your module already has it.
  • Use 3.3V, not 5V, for safest Pi GPIO operation.
  • If you get lots of checksum/timeouts:
    • shorten wires
    • try a different GPIO
    • try 10k pull-up instead of 4.7k
    • switch to the pigpio method

Top comments (0)