0
0
Iot-protocolsProgramBeginner · 2 min read

Raspberry Pi Program to Read DHT11 Sensor Data

Use the Adafruit_DHT Python library on Raspberry Pi with Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, pin) to read DHT11 sensor data; for example, humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4) reads from GPIO pin 4.
📋

Examples

InputDHT11 sensor connected to GPIO pin 4
OutputTemp=24.0*C Humidity=60.0%
InputDHT11 sensor connected to GPIO pin 17
OutputTemp=22.0*C Humidity=55.0%
InputNo sensor connected or sensor error
OutputFailed to get reading. Try again!
🧠

How to Think About It

To read the DHT11 sensor on Raspberry Pi, you first import the sensor library, then specify the sensor type and the GPIO pin number where the sensor data wire is connected. You call a function that tries to read humidity and temperature values multiple times until it succeeds or fails. Finally, you print the values or an error message if reading fails.
📐

Algorithm

1
Import the DHT sensor library.
2
Set the sensor type to DHT11 and specify the GPIO pin number.
3
Call the read function to get humidity and temperature values.
4
Check if the reading was successful.
5
If successful, print temperature and humidity.
6
If failed, print an error message.
💻

Code

python
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4  # GPIO pin where the sensor is connected

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!")
Output
Temp=24.0*C Humidity=60.0%
🔍

Dry Run

Let's trace reading from a DHT11 sensor connected to GPIO pin 4 with example values humidity=60.0 and temperature=24.0.

1

Import library and set sensor

sensor = Adafruit_DHT.DHT11, pin = 4

2

Call read_retry function

humidity, temperature = 60.0, 24.0

3

Check if values are valid

humidity=60.0 is not None and temperature=24.0 is not None

4

Print results

Output: Temp=24.0*C Humidity=60.0%

StepHumidityTemperatureOutput
1--Set sensor and pin
260.024.0Read sensor values
360.024.0Check values valid
460.024.0Print Temp=24.0*C Humidity=60.0%
💡

Why This Works

Step 1: Importing the sensor library

The Adafruit_DHT library provides functions to communicate with the DHT11 sensor easily.

Step 2: Reading sensor data

The read_retry function tries multiple times to get a stable reading of humidity and temperature from the sensor.

Step 3: Checking and printing results

If the sensor returns valid numbers, the program prints them formatted; otherwise, it shows an error message.

🔄

Alternative Approaches

Using gpiozero library with DHT11
python
from gpiozero import CPUTemperature
from time import sleep

# gpiozero does not support DHT11 directly, so use other libraries or custom code
print('gpiozero does not support DHT11 directly; use Adafruit_DHT instead.')
gpiozero library does not support DHT11 sensor directly, so Adafruit_DHT is preferred for simplicity.
Using manual bit-banging with RPi.GPIO
python
# Manual reading requires complex timing and bit reading, not recommended for beginners.
print('Manual reading of DHT11 is complex; use Adafruit_DHT library for ease.')
Manual reading is complicated and error-prone; using a library is easier and more reliable.

Complexity: O(1) time, O(1) space

Time Complexity

Reading the sensor is a fixed-time operation with a few retries, so it runs in constant time.

Space Complexity

The program uses a small fixed amount of memory for variables and library overhead.

Which Approach is Fastest?

Using the Adafruit_DHT library is fastest and simplest compared to manual bit-banging or unsupported libraries.

ApproachTimeSpaceBest For
Adafruit_DHT libraryO(1)O(1)Easy and reliable sensor reading
Manual bit-bangingO(1)O(1)Learning sensor protocol but complex
gpiozero libraryN/AN/ANot suitable for DHT11 directly
💡
Always connect the DHT11 sensor data pin to a GPIO pin with a 10k pull-up resistor for stable readings.
⚠️
Beginners often forget to install the Adafruit_DHT library or use the wrong GPIO pin number.