dht_temp.py

Raspberry Pi 3 Model B+ (2018) — Board Sheet · File : dht_temp.py · Language : Python · Use case : Relever température et humidité d'une pièce avec un capteur DHT11/DHT22

The 3-pin DHT module sends temperature and humidity over a software bit-banged 1-Wire bus from GPIO4 (pin 7). read_retry re-attempts reads, which can be finicky on small-CPU boards like the model-3b-plus. Prints every 2 s, ideal for a greenhouse or wine cellar logger.

Hardware

A Raspberry Pi 3 Model B+ (quad-core BCM2837B0 at 1.4 GHz, 5 GHz Wi-Fi, Bluetooth 4.2, shielded metal case SoC, 40-pin GPIO). One DHT11 or DHT22 sensor, a 10k pull-up resistor, dupont wires.

Wiring

VCC to 3.3 V, GND to GND, DATA to GPIO4 (pin 7) with a 10k pull-up to 3.3 V. Confirm on the official J8 schematic.

How it works

The script sends a start pulse; the sensor answers with 40 bits. Software timing depends on CPU load, hence the retries.

Running it

Copy the script to the board (scp), then run it with python3 (or bash). Raspberry Pi OS ships gpiozero; otherwise sudo apt install python3-gpiozero. The 40-pin pinout is documented on the official Raspberry Pi site.

Variants

DHT22 is more accurate (±0.5 °C) than DHT11; log to CSV for history. 5 GHz Wi-Fi available: test both bands; the shielded case dissipates well but watch cpuinfo.

dht_temp.py

# dht_temp.py — lit temperature et humidite (DHT11/DHT22).
# Deps: pip3 install Adafruit_DHT (ou depot officiel)
import time
import Adafruit_DHT

SENSOR = Adafruit_DHT.DHT22   # DHT11 sur les petits modules bleus
PIN = 4                        # GPIO4, broche 7

while True:
    humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)
    if humidity is not None and temperature is not None:
        print(f"T={temperature:0.1f}C  H={humidity:0.1f}%")
    else:
        print("Lecture rate: verifier cablage, pull-up 10k et alimentation")
    time.sleep(2)

← Back to the board