![]() |
Erriez DHT22 library for Arduino
1.2.1
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
|
#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... | |
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.
|
explicit |
Constructor DHT22 sensor.
| pin | Data pin sensor. |
Definition at line 39 of file ErriezDHT22.cpp.
| 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.
| true | Available, interval between sensor reads >= 2000 ms and sensor read was successful. |
| false | Not available, interval between sensor reads too short, or read failed. |
Definition at line 101 of file ErriezDHT22.cpp.
| void DHT22::begin | ( | uint8_t | numSamples = 0 | ) |
Initialize sensor.
| numSamples | Number 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.
| int16_t DHT22::readHumidity | ( | ) |
Read humidity from sensor.
| Humidity | Signed humidity with last digit after the point. |
| ~0 | Invalid conversion: Sensor read error occurred. |
Definition at line 164 of file ErriezDHT22.cpp.
| 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:
| true | Last conversion was successful. |
| false | Last conversion was unsuccessful. |
Definition at line 212 of file ErriezDHT22.cpp.
| int16_t DHT22::readTemperature | ( | ) |
Read temperature from sensor.
Returns the actual temperature, or a cached temperature when read interval is too short.
| Temperature | Signed temperature with last digit after the point. |
| ~0 | Invalid conversion: Sensor read occurred. |
Definition at line 121 of file ErriezDHT22.cpp.
1.8.13