Loading
Loading
IoT Lab content is currently available in Arabic only. We're working on an English version.
عرض بالعربية →How to sense the surrounding room environment. An introduction to using pre-built libraries.
VCC to 5V. Data pin to pin 2 with a 10K pull-up resistor to VCC (some DHT11 modules come with 3 pins and a built-in resistor). GND to GND.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) { Serial.println("Failed"); return; }
Serial.print("Temp: "); Serial.print(t);
Serial.print(" C, Humidity: "); Serial.println(h);
delay(2000);
}Trying to read the sensor too frequently. DHT11 needs at least 2 seconds between each reading.