Erriez DHT22 library for Arduino  1.2.1
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
Public Member Functions | List of all members
DHT22 Class Reference

DHT22 sensor class. More...

#include <ErriezDHT22.h>

Public Member Functions

 DHT22 (uint8_t pin)
 Constructor DHT22 sensor. More...
 
void begin (uint8_t numSamples=0)
 Initialize sensor. More...
 
bool available ()
 Check if new temperature or humidity read is allowed. More...
 
bool readSensorData ()
 Read data from sensor. More...
 
int16_t readTemperature ()
 Read temperature from sensor. More...
 
int16_t readHumidity ()
 Read humidity from sensor. More...
 

Detailed Description

DHT22 sensor class.

According to the datasheet, the AM2302/AM2303 is a low cost consumer temperature sensor. It may not be used in safety critical applications, emergency stop devices or any other occasion that failure of AM2302/AM2303 may cause personal injury.

The AM2302/AM2303 is factory calibrated. However, deviation may occur with aging.

The temperature/humidity read interval in this library is cached for 2 seconds to prevent heating-up the internal chip with continues reading.

Global interrupts are disabled during a synchronous sensor read transfer. This is required to sample the data bit lengths at maximum speed on low-end devices without any application interrupts. The read calls are protected with a timeout.

The application is responsible for checking ~0 values after a read which means that the read failed or a timeout occurred. Multiple reads by the application with an average calculation is recommended.

Definition at line 80 of file ErriezDHT22.h.

Constructor & Destructor Documentation

◆ DHT22()

DHT22::DHT22 ( uint8_t  pin)
explicit

Constructor DHT22 sensor.

Parameters
pinData pin sensor.

Definition at line 39 of file ErriezDHT22.cpp.

Member Function Documentation

◆ available()

bool DHT22::available ( )

Check if new temperature or humidity read is allowed.

The application should call this function and check if a new temperature and humidity can be read to prevent too fast sensor reads.

Return values
trueAvailable, interval between sensor reads >= 2000 ms and sensor read was successful.
falseNot available, interval between sensor reads too short, or read failed.

Definition at line 101 of file ErriezDHT22.cpp.

◆ begin()

void DHT22::begin ( uint8_t  numSamples = 0)

Initialize sensor.

Parameters
numSamplesNumber of samples to calculate temperature and humidity average. This allocates sizeof(int16_t) * number of samples. Value 0 (default) will disable average calculation.
 Call this function from setup().\n

 - Connect an external 3k3..10k pull-up resistor between the DAT and VCC pins only when:\n
     - using a AM2302/AM2303 sensor without a DT22 breakout PCB\n
        AND\n
     - the MCU IO pin has no built-in or external pull-up resistor.\n
 - The DHT22 breakout PCB contains a 3k3 pull-up resistor between DAT and VCC.\n
 - Please refer to the MCU datasheet or board schematic for more information about IO pin\n
   pull-up resistors.

Definition at line 75 of file ErriezDHT22.cpp.

◆ readHumidity()

int16_t DHT22::readHumidity ( )

Read humidity from sensor.

Return values
HumiditySigned humidity with last digit after the point.
~0Invalid conversion: Sensor read error occurred.

Definition at line 164 of file ErriezDHT22.cpp.

◆ readSensorData()

bool DHT22::readSensorData ( )

Read data from sensor.

5 Bytes data will be read when interval between previous read >= 2000 ms.

The sensor data is read until a valid conversion has been performed. A valid conversion consists of:

  • A valid start condition
  • A successful sensor read (5 Bytes data)
  • A correct checksum
    Return values
    trueLast conversion was successful.
    falseLast conversion was unsuccessful.

Definition at line 212 of file ErriezDHT22.cpp.

◆ readTemperature()

int16_t DHT22::readTemperature ( )

Read temperature from sensor.

Returns the actual temperature, or a cached temperature when read interval is too short.

Return values
TemperatureSigned temperature with last digit after the point.
~0Invalid conversion: Sensor read occurred.

Definition at line 121 of file ErriezDHT22.cpp.


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