capteur-dht11.ino
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 Pro Mini (ATmega328P, 5 V/16 MHz or 3.3 V/8 MHz variants) wired to a USB-serial adapter (FTDI) or an ICSP programmer. 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
Select « Arduino Pro Mini » in the IDE (5 V/16 MHz or 3.3 V/8 MHz per your model), ATmega328P processor, the right port, then Upload. The adapter must be wired DTR-DTR, RX-TX, TX-RX, VCC-VCC, GND-GND.
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(" %"));
}