dht_temp.py

Raspberry Pi Model A (2013) — 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-a. Prints every 2 s, ideal for a greenhouse or wine cellar logger.

Hardware

A Raspberry Pi Model A (BCM2835, 26-pin GPIO header, a single OTG-mode USB port, no Ethernet: plan for a powered USB hub). 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 P1 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 or USB stick), then run it with python3 (or bash). On older Raspbian, install gpiozero with sudo apt install python3-gpiozero. Power the board at 5 V/700 mA minimum through the OTG port.

Variants

DHT22 is more accurate (±0.5 °C) than DHT11; log to CSV for history. Power through the OTG port at 5 V/700 mA minimum; avoid power-hungry USB devices.

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