/idDHTLib

DHT11 & DHT22 interrupt driven library for arduino

Primary LanguageC++

idDHTLib

Interrupt driven DHT11 & DHT22 temperature and humidity sensor Arduino library.

Usage

Instantiate the sensor object

    // for DHT11
    idDHTLib DHTLib(pinNumber);
    
     // for DHT11 and DHT22, sensorType can be idDHTLib::DHT11 or idDHTLib::DHT22
    idDHTLib DHTLib(pinNumber, sensorType);

pinNumber is the digital pin number from the Arduino board and must be an external interrupt capable pin

Board int.0 int.1 int.2 int.3 int.4 int.5
Uno, Ethernet 2 3
Mega2560 2 3 21 20 19 18
Leonardo 3 2 0 1
Due (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)

Start the reading

The sensor reading can be started with one of these methods

//lasts about 18ms
DHTLib.acquire();

//lasts a few uS but need the acquiring() function to be called at intervals < 10ms
//this should be used in cases where the other tasks in the loop() have to respond fast
DHTLib.acquireFastLoop();

//blocks the execution until the reading is complete
DHTLib.acquireAndWait();

Checking for completion

DHTLib.acquiring() //returns false when reading is complete (or not started)

Checking for errors

After the reading is complete

DHTLib.getStatus() 
  • returns one of these values
    • IDDHTLIB_OK - the reading was successful
    • IDDHTLIB_ERROR_CHECKSUM
    • IDDHTLIB_ERROR_TIMEOUT
    • IDDHTLIB_ERROR_ACQUIRING
    • IDDHTLIB_ERROR_DELTA
    • IDDHTLIB_ERROR_NOTSTARTED

Sensor data

After the reading is complete and the status was IDDHTLIB_OK

DHTLib.getCelsius() // float 
DHTLib.getFahrenheit() // float 
DHTLib.getKelvin() // float 
DHTLib.getDewPoint() // double, 5x faster than dewPointSlow(), delta max = 0.6544 wrt dewPointSlow()
DHTLib.getDewPointSlow() // double
DHTLib.getHumidity() // float 

Basic example

idDHTLib DHTLib(2, idDHTLib::DHT11);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int result = DHTLib.acquireAndWait();
  if (result == IDDHTLIB_OK) {
    Serial.print("Humidity (%): ");
    Serial.println(DHTLib.getHumidity(), 2);
  
    Serial.print("Temperature (oC): ");
    Serial.println(DHTLib.getCelsius(), 2);
  
    Serial.print("Temperature (oF): ");
    Serial.println(DHTLib.getFahrenheit(), 2);
  
    Serial.print("Temperature (K): ");
    Serial.println(DHTLib.getKelvin(), 2);
  
    Serial.print("Dew Point (oC): ");
    Serial.println(DHTLib.getDewPoint());
  
    Serial.print("Dew Point Slow (oC): ");
    Serial.println(DHTLib.getDewPointSlow());
  } else {
    Serial.println("Error");
  }
  delay(2000);
}

Changelog

  • v 0.0.1
    • fork from idDHT11 lib
    • change names to idDHTLib
    • added DHT22 functionality
  • v 0.0.2
    • Optimization on shift var (pylon from Arduino Forum)
  • v 0.0.3
    • Timing correction to finally work properly on DHT22 (Dessimat0r from Arduino forum)
  • v 1.0.0
    • autoformat code with Arduino IDE code formatting standards (kcsoft)
    • remove the interrupt number from the constructor by using digitalPinToInterrupt (kcsoft)
    • fix type for us and timeout when no interrupt is triggered (kcsoft)
    • removed the callback parameter from the constructor, added sensor type (DHT11, DHT22) as optional param (kcsoft)
    • removed temp/humid calculation from the isr (kcsoft)
    • new function acquireFastLoop to remove delay when start acquiring (kcsoft)
    • update README.md file (kcsoft)

Datasheet

DHT11
DHT22

Based on

idDHT11 library
DHTLib library
Code proposed on Arduino forum

License

GPL v3