3.2.2. dht — DHT temperature and humidity sensor

DHT is temperature and humidity sensor. The two most popular versions are DHT11 (blue) and DHT22 (white).

../../../_images/DHT.png

Both versions are available in bare sensor and module variants. The bare sensor has four pins, while the module only has three. According to the datasheet, the data pin should be connected to VCC via a 5k pull-up resistor. The pull-up resistor is only needed for the bare sensor, as the module already has a built-in pull-up resistor.

../../../_images/dht_wiring.png

Source code: src/drivers/sensors/dht.h, src/drivers/sensors/dht.c

Example code: examples/dht/main.c


Functions

int dht_module_init(void)

Initialize the dht module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int dht_init(struct dht_driver_t *self_p, struct pin_device_t *pin_p)

Initialize given driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • pin_p: Data line pin device.

int dht_read(struct dht_driver_t *self_p, float *temperature_p, float *humidty_p)

Read temperature and humidity from the DHT21/22 device.

CAUTION: This function disables interrupts for up to 5 ms, which may cause problems for other timing critical functionality.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • temperature_p: Temperature in degrees Celsius, or NULL.
  • humidity_p: Humidity in relative humidty RH, or NULL.

int dht_read_11(struct dht_driver_t *self_p, float *temperature_p, float *humidty_p)

Read temperature and humidity from the DHT11 device.

CAUTION: This function disables interrupts for up to 5 ms, which may cause problems for other timing critical functionality.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • temperature_p: Temperature in degrees Celsius, or NULL.
  • humidity_p: Humidity in relative humidty RH, or NULL.

struct dht_driver_t
#include <dht.h>

The DHT driver struct.

Public Members

struct pin_device_t *pin_p
struct log_object_t log