capteur-dht11.ino

Arduino UNO R3 · File : capteur-dht11.ino · Language : Arduino · Use case : Lire température et humidité avec un capteur DHT11

The DHT11 sensor delivers temperature and humidity over a single data wire. The sketch polls it every 2 seconds (the sensor limit), checks frame errors and prints readings to the serial monitor.

Hardware

An Arduino Uno R3 (ATmega328P, 5 V, 16 MHz) powered over USB or a 7-12 V barrel jack. 1 DHT11 sensor (3 or 4-pin package) + 10 kΩ resistor.

Wiring

VDD to 5V, GND to GND, DATA to pin 4 with a 10 kΩ pull-up to 5V. On a 3-pin module the resistor is already fitted.

How it works

readTemperature/readHumidity trigger the sensor 1-wire protocol. A NaN means a corrupted frame: retry next cycle without blocking the loop.

Running it

In the Arduino IDE, select the « Arduino Uno » board and the matching serial port, then click Upload. Open the serial monitor at 9600 baud to follow the messages.

Variants

Switch to a DHT22 (±0.5 °C accuracy) by changing TYPE_DHT, or send readings to a server through a serial module (ESP8266, GNSS...).

capteur-dht11.ino

// Temperature + humidite avec un DHT11 (ou DHT22 en changeant le type).
#include <DHT.h>

#define TYPE_DHT DHT11 // DHT22 pour la version precise
const int PIN_DHT = 4;
DHT dht(PIN_DHT, TYPE_DHT);

unsigned long derniereLecture = 0;
const unsigned long PERIODE = 2000; // le DHT11 ne doit pas etre interroge plus d'1 fois/s

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println(F("DHT pret"));
}

void loop() {
  if (millis() - derniereLecture < PERIODE) return;
  derniereLecture = millis();

  float h = dht.readHumidity();
  float t = dht.readTemperature(); // Celsius

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Echec de lecture DHT (verifier cablage/pull-up)"));
    return;
  }

  Serial.print(F("Temperature : "));
  Serial.print(t, 1);
  Serial.print(F(" C | Humidite : "));
  Serial.print(h, 1);
  Serial.println(F(" %"));
}

← Back to the board