dht11.ino

ESP8266 (ESP-12E module) · File : dht11.ino · Language : Arduino · Use case : Relever température et humidité avec un DHT11

The beginner sensor par excellence on NodeMCU: one 1-wire frame every 2 s, decoded by the Adafruit library. The sketch turns the numbers into a binary verdict on the serial monitor.

Hardware required

An ESP-12E module (ESP8266EX, 2.4 GHz Wi-Fi) mounted on a NodeMCU / Wemos D1 mini board, powered over micro-USB. 1 DHT11 (3-pin module) + 10 kΩ resistor if bare.

Wiring

VDD 3.3 V or 5 V (modules), DATA on D2 with 10 kΩ pull-up to VDD, GND. D2 = GPIO4, a boot-safe pin.

How it works

readTemperature triggers the full transaction (start + 40 bits); the DHT11 is capped at 0.5 Hz, hence the millis() guard. NaNs cover lost frames, common on long wires. In the Arduino IDE, install the « esp8266 » package, select « NodeMCU 1.0 (ESP-12E Module) » then Upload (hold GPIO0 LOW if flashing fails). The A0 ADC reads 0-1 V (NodeMCU divider: 0-3.3 V).

Going further

Publish the values over Wi-Fi (web server sketch) or show them on an SSD1306 OLED shared on I2C.

dht11.ino

// DHT11 sur D2 (GPIO4) — bibliotheque DHT sensor library (Adafruit).
#include <DHT.h>
#define DHTPIN D2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
unsigned long precedent = 0;

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println("DHT11 pret (lecture toutes les 2 s)");
}

void loop() {
  if (millis() - precedent < 2000) return;
  precedent = millis();

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Echec : verifier cabling et alim 3,3-5 V");
    return;
  }
  // Indice de confort tres approximatif, pour donner du sens au chiffre
  Serial.printf("T=%.1f C  HR=%.0f %%  (confort : %s)\n", t, h,
                (t >= 18 && t <= 26 && h >= 30 && h <= 70) ? "correct" : "aerer");
}

← Back to the board