← Back to list

How to install DHT11 library in Raspberry Pi?

On Raspberry Pi you typically “install a DHT11 library” as a Python package.

Ampheo · 2026-01-04 07:34 · 0 claps · 1.1 min read
#dht11 #raspberry-pi #dht-22 #pull-up-resistor #dht11-library
Open on Medium ↗
Wiki topics: 📟 · Gadgets & IoT 📚 · Books & Reading

How to install DHT11 library in Raspberry Pi?

On Raspberry Pi you typically “install a DHT11 library” as a Python package.

Option A (most common): Adafruit CircuitPython DHT (recommended)

This works for **DHT11/DHT22**, and is the path most tutorials use.

  1. Update + install prerequisites
sudo apt update
sudo apt -y upgrade
sudo apt install -y python3-pip python3-venv libgpiod2

Adafruit specifically calls out installing libgpiod2 alongside the DHT library.

  1. (Bookworm and later) use a virtual environment Adafruit notes that on newer Raspberry Pi OS versions you should install Python modules in a venv.
mkdir -p ~/dht && cd ~/dht
python3 -m venv .venv --system-site-packages
source .venv/bin/activate
  1. Install Blinka + DHT library
pip3 install --upgrade adafruit-blinka
pip3 install adafruit-circuitpython-dht

The DHT package install command is documented in Adafruit’s library docs.

  1. Quick test script (DHT11 on GPIO4 example)
import time
import board
import adafruit_dht

dht = adafruit_dht.DHT11(board.D4)  # GPIO4 (pin 7)
while True:
    try:
        print("Temp:", dht.temperature, "C  Humidity:", dht.humidity, "%")
    except RuntimeError as e:
        print("Read error:", e)
    time.sleep(2)

Adafruit recommends retrying after ~2 seconds because DHT reads can fail occasionally.

Hardware note: DHT11 needs a pull-up resistor on the data line (commonly 1k–5k).

Option B (often more reliable timing): pigpio-dht

If you get flaky readings (common on some setups), pigpio-dht is another Python library:

sudo apt install -y pigpio python3-pigpio
sudo systemctl enable --now pigpiod
pip3 install pigpio-dht

pigpio-dht installation is pip install pigpio-dht.


메타데이터
post_id
462f1d7697fd
slug
how-to-install-dht11-library-in-raspberry-pi-462f1d7697fd
url
https://medium.com/@pqshedy33/how-to-install-dht11-library-in-raspberry-pi-462f1d7697fd
canonical_url
https://medium.com/@pqshedy33/how-to-install-dht11-library-in-raspberry-pi-462f1d7697fd
author_url
https://medium.com/@pqshedy33
status
ok
fetched_at
2026-06-26 21:52:29