Monitoring environmental conditions like temperature and humidity is crucial for many projects, including home automation, weather stations, and IoT applications. The DHT11 and DHT22 sensors are popular choices for measuring these parameters due to their ease of use, low cost, and reliability.

In this guide, we’ll learn how to interface the DHT11/DHT22 sensor with a Raspberry Pi and retrieve data using Python.
METHOD1:
Hardware Requirements
Raspberry Pi (5, 4, 3B+, Zero, etc.)
DHT11 or DHT22 Sensor
10kΩ Resistor (for pull-up)
Jumper Wires
Breadboard (optional)
Installing Required Libraries
Before running the Python script, install the Adafruit_DHT library to interact with the sensor:
sudo apt update
sudo apt install python3-pip
pip3 install Adafruit_DHT
Wiring the DHT11/DHT22 to Raspberry Pi
DHT11/22 pin1 VCC —Raspberry pi 3.3v or 5v
DHT11/22 pin2 OUT/DATA —Raspberry pi GPIO 4
DHT11/22 pin3 GND— Raspberry pi Ground

Python Script
Now, open Thonny and create a new .py file and paste below script there to test the sensor.
import Adafruit_DHT
SENSOR = Adafruit_DHT.DHT22 # Change to DHT11 if using DHT11
PIN = 4 # GPIO pin connected to Data pin of sensor
humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)if humidity is not None and temperature is not None:
print(f”Temperature: {temperature:.2f}°C”)
print(f”Humidity: {humidity:.2f}%”)
else:
print(“Failed to retrieve data. Check connections!”)
Troubleshooting & Common Issues
🔸 Incorrect Data or No Output?
1- Ensure the correct GPIO pin is specified in the script.
2- Use a 10kΩ pull-up resistor between the Data pin and VCC if using only sensor.
3- Check if the sensor is powered properly (3.3V or 5V).
🔸 Library Not Found Error?
1- Run pip3 install Adafruit_DHT
again to ensure the package is installed.
🔸 Script Not Executing?
1- Ensure Python is installed (python3 --version
).
2- Run the script using python3 filename.py
.
METHOD2:
Try below second Method, if above method does not work.
After Successfully Booting OS Using Raspberry pi imager and make sure it is up to date.Now we need to configure config.txt file. Enter below command to open the file
sudo nano /boot/firmware/config.txt
and add the below line of code in last line of file
dtoverlay=dht11,gpiopin=17
Now Reboot pi to save the changes
sudo reboot now
Testing
Write below line in terminal to test if it works. The output readings will be multiple of 1000.
cat /sys/bus/iio/devices/iio:device0/in_temp_input
Python script
To use the library in python script , use below code and you are done working with DHT sensors.
#blogs from collegeroadonline/Projectszone
from time import sleep
def read_bus(file):
f = open(file,”rt”)
value = int(f.readline())
f.close
return value
def dht11_val():
t = h = 0
try:
t = read_bus(“/sys/bus/iio/devices/iio:device0/in_temp_input”)/1000
h = read_bus(“/sys/bus/iio/devices/iio:device0/in_humidityrelative_input”)/1000
except Exception as e:
print(e)
t = h = “N/A”
return t, h
while True:
(temp, hum) = dht11_val()
if temp != “N/A” and hum != “N/A”:
print(“Temperature %(t)0.2f°C, Humidity: %(h)0.2f%%” % {“t”: temp, “h”: hum})
sleep(1)
Second Method worked for me. I hope the above information will help you understand and work with Raspberry pi and DHT sensors.