/DHTNew

The new library for your DHT temperature and humidity sensor.

Primary LanguageC++MIT LicenseMIT

DHTNew

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

Usage

#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());
  }
}

Complete Usage

#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);
  }
}

Credits

Although this library has been rewritten from the ground-up, I got inspiration from many sources:

Many thanks to them !

And of course datasheets...