capteur-dht11.py

ODROID-C4 (Hardkernel) · File : capteur-dht11.py · Language : Python · Use case : Lire un capteur DHT11/DHT22 depuis Linux (température/humidité)

The Adafruit CircuitPython driver runs on stock Linux: it bit-bangs the DHT 1-wire protocol through libgpiod. The script prints temperature and humidity every 2 s, shrugging off invalid frames without crashing.

Hardware

An ODROID-C4 (Amlogic S905X3, 4 Cortex-A55, 4 GB) running Armbian or Ubuntu Hardkernel, with gigabit Ethernet, an IR receiver and a 40-pin header. 1 DHT11/DHT22 sensor + 10 kΩ resistor (3-pin module: already built in).

Wiring

VDD to 3.3 V, DATA to the script pin (GPIO4) with a 10 kΩ pull-up to 3.3 V, GND to GND.

How it works

adafruit_dht drives the protocol through the kernel (use_pulseio=False): start pulse, microsecond timing, decoding. Periodic failures are normal on the DHT11.

Running it

On Armbian (mainline kernel): apt install gpiod python3-gpiod; the C4 exposes two chips, gpio and gpio_ao (gpiodetect) — the blue LED is also drivable via /sys/class/leds.

Variants

Log to CSV with the cron script, or expose readings over the network with the built-in web server on port 8080.

capteur-dht11.py

#!/usr/bin/env python3
# DHT11/DHT22 via adafruit-circuitpython-dht (pilote en noyau ou bit-bang).
# Installation :
#   sudo apt install python3-dev python3-venv libgpiod2
#   python3 -m venv ~/.venv && source ~/.venv/bin/activate
#   pip install adafruit-circuitpython-dht
import time
import board
import adafruit_dht

# Broche physique 7 (GPIO4 sur connecteur standard 40 broches) :
capteur = adafruit_dht.DHT11(board.D4, use_pulseio=False)
# DHT22 : adafruit_dht.DHT22(board.D4, use_pulseio=False)

print("Ctrl-C pour arreter")
while True:
    try:
        t = capteur.temperature      # degres Celsius
        h = capteur.humidity         # % HR
        print(f"T = {t:0.1f} C | HR = {h:0.1f} %")
    except RuntimeError as erreur:
        # Le DHT rate parfois une trame : normal, on recommence.
        print("trame invalide:", erreur.args[0])
    time.sleep(2.0)

← Back to the board