The new library for your DHT temperature and humidity sensor.
DHTNew features:
- Consistent API
- Fast and clean implementation
- Reliable (welcome to no NAN's land !)
- Arduino and ESP8266 friendly
- Decoupled sensor reading
- Precise error reporting
#include <DHTNew.h>
DHT dht(2, DHT_MODEL_DHT22); // set your pin and model here
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// wait for the minimum sampling period
delay(dht.getMinimumSamplingPeriod());
// print values (this triggers a sensor reading)
Serial.print("Temperature: ");
Serial.println("dht.readTemperature()");
Serial.print("Humidity: ");
Serial.println(dht.readHumidity());
}
}
#include <DHTNew.h>
DHT dht(2, DHT_MODEL_DHT22); // set your pin and model here
unsigned long dhtReadings = 0;
unsigned long dhtErrors = 0;
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// make a reading attempt (i.e. returns true every getMinimumSamplingPeriod() ms minimum)
if (dht.read()) {
dhtReadings++;
Serial.println("Reading done");
// report errors
if (dht.getError() != DHT_ERROR_NONE) {
dhtErrors++;
Serial.print("Error: ");
Serial.println(dht.getErrorString());
return;
}
// print values
Serial.print("Temperature: ");
Serial.println(dht.getTemperature());
Serial.print("Humidity: ");
Serial.println(dht.getHumidity());
// print error rate
Serial.print("Error rate: ")
Serial.print(dhtErrors);
Serial.print("/");
Serial.println(dhtReadings);
}
}
Although this library has been rewritten from the ground-up, I got inspiration from many sources:
Many thanks to them !
And of course datasheets...
- Sparkfun's DHT22
- Adafruit's DHT22
- Aosong's DHT22: very clean one!
- D-Robotics UK's DHT11