DHT22
An interrupt-driven library for the DHT22 temperature and humidity sensor
Public Types | Public Member Functions | List of all members
DHT22 Class Reference

An interrupt-driven library for the DHT22 sensor. More...

#include <DHT22.h>

Public Types

enum  Result {
  None, Ok, ChecksumMismatch, WakeUpError,
  DataError
}
 Possible result values. More...
 
enum  Status { WakingUp, Reading, Done, Invalid }
 The status of the library state machine. More...
 

Public Member Functions

 DHT22 (int pin)
 Construct a DHT22 library instance. More...
 
void onFallingEdge ()
 Signals the library that the data pin has gone low. More...
 
bool startRead ()
 Wakes up the sensor and starts waiting for data. More...
 
Result blockingRead ()
 Starts reading, if necessary, and waits for result. More...
 
int16_t getTemp ()
 The last temperature reading. More...
 
uint16_t getHumidity ()
 The last humidity reading. More...
 
Status state ()
 The sensor status. More...
 
Result lastResult ()
 The result of the last reading. More...
 

Detailed Description

An interrupt-driven library for the DHT22 sensor.

This class provides an interrupt-driven interface to the DHT22 sensor. The user must attach an ISR to the falling edge of the DHT22 data pin and invoke onFallingEdge() in the ISR.

Reading can be started with startRead(), which returns immediately, or blockingRead(), which waits for the reading to complete.

If state() returns Done, the data has been read and can be accessed with getTemp() and getHumidity(). Be aware of the measuring units!! If state() returns Invalid, lastResult() can give some information on the cause.

Usage example:

DHT22 dht(2);
void dhtISR() {
dht.onFallingEdge();
}
void setup() {
Serial.begin(9600);
// On Arduino Uno, interrupt 0 is attached to pin 2
attachInterrupt(0, &dhtISR, FALLING);
// let the sensor warm up for 2 seconds
delay(2000);
}
void loop() {
if (dht.blockingRead() == DHT22::Ok) {
Serial.print("Temperature: ");
Serial.print(dht.getTemp()/10.0);
Serial.println("C");
Serial.print("Humidity: ");
Serial.print(dht.getHumidity()/10.0);
Serial.println("%");
}
// Minimum interval between reads.
delay(2000);
}

Member Enumeration Documentation

Possible result values.

Enumerator
None 

Data has never been read.

Ok 

Yay!!

ChecksumMismatch 

The data reading was currupted.

WakeUpError 

There was a protocol timing error during the sensor wake-up.

DataError 

There was a protocol timing error during data transmission.

The status of the library state machine.

Enumerator
WakingUp 

The sensor is waking up.

Reading 

The sensor is transmitting data.

Done 

Data was successfully read.

Invalid 

Data reading failed or was never attempted.

Constructor & Destructor Documentation

DHT22::DHT22 ( int  pin)

Construct a DHT22 library instance.

Parameters
pinThe data pin of the sensor

Member Function Documentation

DHT22::Result DHT22::blockingRead ( )

Starts reading, if necessary, and waits for result.

Returns
The reading result
uint16_t DHT22::getHumidity ( )

The last humidity reading.

Returns
The relative humidity in per mil
int16_t DHT22::getTemp ( )

The last temperature reading.

Returns
The temperature in tents of Celsius degree
DHT22::Result DHT22::lastResult ( )

The result of the last reading.

void DHT22::onFallingEdge ( )

Signals the library that the data pin has gone low.

bool DHT22::startRead ( )

Wakes up the sensor and starts waiting for data.

Returns
Whether the reading was started
DHT22::Status DHT22::state ( )

The sensor status.


The documentation for this class was generated from the following files: