dht_temp.py
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-b-plus. Prints every 2 s, ideal for a greenhouse or wine cellar logger.
Hardware
A Raspberry Pi Model B+ (BCM2835, 40-pin GPIO header, micro-USB power, 3.5 mm 4-pole audio jack). 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). Install gpiozero with sudo apt install python3-gpiozero if missing. The 40-pin pinout is in the official schematic and on the Raspberry Pi website.
Variants
DHT22 is more accurate (±0.5 °C) than DHT11; log to CSV for history. No built-in Wi-Fi: use Ethernet or a USB dongle; 5 V/1.2 A micro-USB supply.
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)